-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
index.ts
76 lines (65 loc) · 2.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* eslint-disable no-console */
import { execute, parse, subscribe, validate } from 'graphql';
import { ApolloServer } from '@apollo/server';
import {
GatewayApolloConfig,
GatewayExecutor,
GatewayInterface,
GatewayLoadResult,
GatewaySchemaLoadOrUpdateCallback,
GatewayUnsubscriber,
} from '@apollo/server-gateway-interface';
import { ApolloServerPluginLandingPageGraphQLPlayground } from '@apollo/server-plugin-landing-page-graphql-playground';
import { startStandaloneServer } from '@apollo/server/standalone';
import { envelop, useEngine, useSchema } from '@envelop/core';
import { makeExecutableSchema } from '@graphql-tools/schema';
const schema = makeExecutableSchema({
typeDefs: /* GraphQL */ `
type Query {
hello: String!
}
`,
resolvers: {
Query: {
hello: () => 'World',
},
},
});
const getEnveloped = envelop({
plugins: [useEngine({ parse, validate, subscribe, execute }), useSchema(schema)],
});
const executor: GatewayExecutor = async requestContext => {
const { schema, execute, contextFactory } = getEnveloped({ req: requestContext.request.http });
return execute({
schema,
document: requestContext.document,
contextValue: await contextFactory(),
variableValues: requestContext.request.variables,
operationName: requestContext.operationName,
});
};
// Apollo Server 4 requires a custom gateway to enable custom executors:
// https://www.apollographql.com/docs/apollo-server/migration/#executor
class Gateway implements GatewayInterface {
private schemaCallback?: GatewaySchemaLoadOrUpdateCallback;
async load(options: { apollo: GatewayApolloConfig }): Promise<GatewayLoadResult> {
// Apollo expects this schema to be called before this function resolves:
// https://github.com/apollographql/apollo-server/issues/7340#issuecomment-1407071706
this.schemaCallback?.({ apiSchema: schema, coreSupergraphSdl: '' });
return { executor };
}
onSchemaLoadOrUpdate(callback: GatewaySchemaLoadOrUpdateCallback): GatewayUnsubscriber {
this.schemaCallback = callback;
return () => {};
}
async stop() {}
}
const server = new ApolloServer({
gateway: new Gateway(),
plugins: [ApolloServerPluginLandingPageGraphQLPlayground({ endpoint: '/graphql' })],
});
(async () => {
await startStandaloneServer(server, {
listen: { port: 3000 },
});
})();