UNPKG

@envelop/apollo-federation

Version:

This plugin integrates Apollo Federation Gateway into Envelop.

52 lines (51 loc) 2.37 kB
import { getOperationAST, print, printSchema, } from 'graphql'; import { InMemoryLRUCache } from '@apollo/utils.keyvaluecache'; import { getDocumentString } from '@envelop/core'; import { newCachePolicy } from './new-cache-policy.js'; export const useApolloFederation = (options) => { const { gateway, cache = new InMemoryLRUCache(), logger = console, metrics = Object.create(null), overallCachePolicy = newCachePolicy(), } = options; let schemaHash; return { onPluginInit({ setSchema }) { if (gateway.schema) { setSchema(gateway.schema); } else { logger.warn(`ApolloGateway doesn't have the schema loaded. Please make sure ApolloGateway is loaded with .load() method. Otherwise this plugin might not work consistently, especially if you are using ApolloServer.`); gateway.load(); } gateway.onSchemaLoadOrUpdate(({ apiSchema, coreSupergraphSdl = printSchema(apiSchema) }) => { setSchema(apiSchema); schemaHash = coreSupergraphSdl || printSchema(apiSchema); }); }, onExecute({ args, setExecuteFn }) { const documentStr = getDocumentString(args.document, print); const operation = getOperationAST(args.document, args.operationName ?? undefined); if (!operation) { throw new Error(`Operation ${args.operationName || ''} cannot be found in ${documentStr}`); } setExecuteFn(function federationExecutor() { return gateway.executor({ document: args.document, request: { query: documentStr, operationName: args.operationName ?? undefined, variables: args.variableValues ?? undefined, }, overallCachePolicy, operationName: args.operationName ?? null, cache, context: args.contextValue, queryHash: documentStr, logger, metrics, source: documentStr, operation, schema: args.schema, schemaHash, }); }); }, }; };