UNPKG

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