-
Notifications
You must be signed in to change notification settings - Fork 242
/
possibleTypeExtensions.test.ts
198 lines (180 loc) 路 5.33 KB
/
possibleTypeExtensions.test.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
Kind,
DocumentNode,
GraphQLSchema,
specifiedDirectives,
extendSchema,
} from 'graphql';
import { validateSDL } from 'graphql/validation/validate';
import gql from 'graphql-tag';
import { buildMapsFromServiceList } from '../../../compose';
import {
typeSerializer,
graphqlErrorSerializer,
} from 'apollo-federation-integration-testsuite';
import federationDirectives from '../../../../directives';
import { ServiceDefinition } from '../../../types';
import { PossibleTypeExtensions } from '../possibleTypeExtensions';
expect.addSnapshotSerializer(graphqlErrorSerializer);
expect.addSnapshotSerializer(typeSerializer);
// simulate the first half of the composition process
const createDefinitionsDocumentForServices = (
serviceList: ServiceDefinition[],
): {
definitions: DocumentNode;
extensions: DocumentNode;
} => {
const { typeDefinitionsMap, typeExtensionsMap } = buildMapsFromServiceList(
serviceList,
);
return {
definitions: {
kind: Kind.DOCUMENT,
definitions: Object.values(typeDefinitionsMap).flat(),
},
extensions: {
kind: Kind.DOCUMENT,
definitions: Object.values(typeExtensionsMap).flat(),
},
};
};
describe('PossibleTypeExtensionsType', () => {
let schema: GraphQLSchema;
// create a blank schema for each test
beforeEach(() => {
schema = new GraphQLSchema({
query: undefined,
directives: [...specifiedDirectives, ...federationDirectives],
});
});
it('does not error with matching enums across services', () => {
const serviceList = [
{
typeDefs: gql`
extend type Product {
sku: ID
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
type Product {
id: ID!
}
`,
name: 'serviceB',
},
];
const { definitions, extensions } = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitions, schema, [PossibleTypeExtensions]);
schema = extendSchema(schema, definitions, { assumeValidSDL: true });
errors.push(...validateSDL(extensions, schema, [PossibleTypeExtensions]));
expect(errors).toHaveLength(0);
});
it('errors when there is an extension with no base', () => {
const serviceList = [
{
typeDefs: gql`
extend type Product {
id: ID!
}
`,
name: 'serviceA',
},
];
const { definitions, extensions } = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitions, schema, [PossibleTypeExtensions]);
schema = extendSchema(schema, definitions, { assumeValidSDL: true });
errors.push(...validateSDL(extensions, schema, [PossibleTypeExtensions]));
expect(errors).toHaveLength(1);
expect(errors).toMatchInlineSnapshot(`
Array [
Object {
"code": "EXTENSION_WITH_NO_BASE",
"message": "[serviceA] Product -> \`Product\` is an extension type, but \`Product\` is not defined in any service",
},
]
`);
});
it('errors when trying to extend a type with a different `Kind`', () => {
const serviceList = [
{
typeDefs: gql`
extend type Product {
sku: ID
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
input Product {
id: ID!
}
`,
name: 'serviceB',
},
];
const { definitions, extensions } = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitions, schema, [PossibleTypeExtensions]);
schema = extendSchema(schema, definitions, { assumeValidSDL: true });
errors.push(...validateSDL(extensions, schema, [PossibleTypeExtensions]));
expect(errors).toMatchInlineSnapshot(`
Array [
Object {
"code": "EXTENSION_OF_WRONG_KIND",
"message": "[serviceA] Product -> \`Product\` was originally defined as a InputObjectTypeDefinition and can only be extended by a InputObjectTypeExtension. serviceA defines Product as a ObjectTypeExtension",
},
]
`);
});
it('does not error', () => {
const serviceList = [
{
typeDefs: gql`
extend interface Product {
name: String
}
extend type Book implements Product {
sku: ID!
name: String
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
type Book {
id: ID!
}
interface Product {
sku: ID!
}
`,
name: 'serviceB',
},
];
const { definitions, extensions } = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitions, schema, [PossibleTypeExtensions]);
schema = extendSchema(schema, definitions, { assumeValidSDL: true });
errors.push(...validateSDL(extensions, schema, [PossibleTypeExtensions]));
schema = extendSchema(schema, extensions, { assumeValidSDL: true });
expect(schema.getType('Book')).toMatchInlineSnapshot(`
type Book implements Product {
id: ID!
sku: ID!
name: String
}
`);
expect(errors).toHaveLength(0);
});
});