-
Notifications
You must be signed in to change notification settings - Fork 7
/
federation.ts
130 lines (121 loc) · 3.03 KB
/
federation.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import type {
GraphQLSchema,
DocumentNode,
GraphQLFieldResolver,
} from 'graphql';
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import { ApolloGateway } from '@apollo/gateway';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { gql } from 'graphql-tag';
import type { GraphQLResolverMap } from '@apollo/subgraph/dist/schema-helper';
import {
ValidateDirectiveVisitor,
range,
stringLength,
applyDirectivesToSchema,
} from '../lib/index.js';
/*
When using apollo federation all
directives should be available to all
federated nodes.
*/
type Directive = typeof range;
const buildSchema = (
resolvers: GraphQLResolverMap<{}>,
typeDefs: DocumentNode,
directives: Directive[],
): GraphQLSchema => {
const finalTypeDefs = [
typeDefs,
...ValidateDirectiveVisitor.getMissingCommonTypeDefs(),
...directives.reduce<DocumentNode[]>(
(acc, d) => acc.concat(d.getTypeDefs()),
[],
),
];
const schema = applyDirectivesToSchema(
directives,
buildSubgraphSchema({
resolvers: resolvers as GraphQLResolverMap<unknown>,
typeDefs: finalTypeDefs,
}),
);
return schema;
};
interface ServicesSetup {
directives: Directive[];
port: number;
resolvers: {
[typeName: string]: {
[fieldName: string]: GraphQLFieldResolver<unknown, {}>;
};
};
typeDefs: DocumentNode;
}
const services: ServicesSetup[] = [
{
directives: [range],
port: 4001,
resolvers: {
Query: {
myNumber: (_: unknown, { args }): number => args,
},
},
typeDefs: gql`
type Query {
myNumber(args: Int @range(max: 100, policy: THROW)): Int
@range(min: 2, policy: THROW)
}
`,
},
{
directives: [stringLength],
port: 4002,
resolvers: {
Query: {
myString: (_: unknown, { args }): string => args,
},
},
typeDefs: gql`
type Query {
myString(args: String @stringLength(max: 200, policy: THROW)): String
@stringLength(min: 3, policy: THROW)
}
`,
},
];
const start = async (): Promise<void> => {
const runningString = await Promise.all(
services.map(({ resolvers, typeDefs, port, directives }) =>
startStandaloneServer(
new ApolloServer({
schema: buildSchema(resolvers, typeDefs, directives),
}),
{ listen: { port } },
),
),
);
// eslint-disable-next-line no-console
console.log(runningString.map(({ url }) => url).join('\n'));
const apolloGateway = new ApolloGateway({
serviceList: [
{
name: 'string-service',
url: 'http://localhost:4002',
},
{
name: 'number-service',
url: 'http://localhost:4001',
},
],
});
const server = new ApolloServer({
gateway: apolloGateway,
});
const { url } = await startStandaloneServer(server);
// eslint-disable-next-line no-console
console.log(`🚀 Server ready at ${url}`);
};
// eslint-disable-next-line no-console
start().catch(console.error);