UNPKG

2.03 kBJavaScriptView Raw
1import { ApolloGateway } from '@apollo/gateway';
2import { print, Kind } from 'graphql';
3import { makeRemoteExecutableSchema, delegateToSchema } from 'graphql-tools';
4import { fetchache, Request } from 'fetchache';
5
6const handler = {
7 async getMeshSource({ config, hooks, cache }) {
8 const gateway = new ApolloGateway({
9 fetcher: (info, init) => fetchache(typeof info === 'string' ? new Request(info, init) : info, cache),
10 ...config,
11 });
12 const { schema, executor } = await gateway.load();
13 const fetcher = ({ query, operationName, variables, context }) => executor({
14 document: query,
15 request: {
16 query: print(query),
17 variables,
18 },
19 operationName,
20 cache,
21 context,
22 queryHash: print(query) + '_' + JSON.stringify(variables),
23 logger: console,
24 metrics: {},
25 source: print(query),
26 operation: query.definitions.find(definition => { var _a; return definition.kind === Kind.OPERATION_DEFINITION && ((_a = definition.name) === null || _a === void 0 ? void 0 : _a.value) === operationName; }),
27 });
28 const remoteSchemaOptions = {
29 schema,
30 fetcher,
31 };
32 const remoteSchema = makeRemoteExecutableSchema(remoteSchemaOptions);
33 hooks.on('buildSdkFn', ({ fieldName, replaceFn, schema }) => {
34 replaceFn((args, context, info) => {
35 const delegationOptions = {
36 operation: info.operation.operation,
37 fieldName,
38 schema,
39 args,
40 info,
41 context,
42 };
43 return delegateToSchema(delegationOptions);
44 });
45 });
46 hooks.on('destroy', () => gateway.stop());
47 return {
48 schema: remoteSchema,
49 };
50 },
51};
52
53export default handler;
54//# sourceMappingURL=index.esm.js.map