-
Notifications
You must be signed in to change notification settings - Fork 242
/
matchingEnums.test.ts
205 lines (190 loc) 路 4.92 KB
/
matchingEnums.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
199
200
201
202
203
204
205
import {
GraphQLEnumType,
Kind,
DocumentNode,
validate,
GraphQLSchema,
specifiedDirectives,
} from 'graphql';
import { validateSDL } from 'graphql/validation/validate';
import gql from 'graphql-tag';
import { composeServices, buildMapsFromServiceList } from '../../../compose';
import {
astSerializer,
typeSerializer,
selectionSetSerializer,
} from 'apollo-federation-integration-testsuite';
import { normalizeTypeDefs } from '../../../normalize';
import federationDirectives from '../../../../directives';
import { ServiceDefinition } from '../../../types';
import { MatchingEnums } from '../matchingEnums';
expect.addSnapshotSerializer(astSerializer);
expect.addSnapshotSerializer(typeSerializer);
expect.addSnapshotSerializer(selectionSetSerializer);
// simulate the first half of the composition process
const createDefinitionsDocumentForServices = (
serviceList: ServiceDefinition[],
): DocumentNode => {
const { typeDefinitionsMap } = buildMapsFromServiceList(serviceList);
return {
kind: Kind.DOCUMENT,
definitions: Object.values(typeDefinitionsMap).flat(),
};
};
describe('matchingEnums', () => {
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`
enum ProductCategory {
BED
BATH
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
enum ProductCategory {
BED
BATH
}
`,
name: 'serviceB',
},
];
const definitionsDocument = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitionsDocument, schema, [MatchingEnums]);
expect(errors).toHaveLength(0);
});
it('errors when enums in separate services dont match', () => {
const serviceList = [
{
typeDefs: gql`
enum ProductCategory {
BED
BATH
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
enum ProductCategory {
BEYOND
}
`,
name: 'serviceB',
},
];
const definitionsDocument = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitionsDocument, schema, [MatchingEnums]);
expect(errors).toMatchInlineSnapshot(`
Array [
[GraphQLError: The \`ProductCategory\` enum does not have identical values in all services. Groups of services with identical values are: [serviceA], [serviceB]],
]
`);
});
it('errors when enums in separate services dont match', () => {
const serviceList = [
{
typeDefs: gql`
type Query {
products: [Product]!
}
type Product @key(fields: "sku") {
sku: String!
upc: String!
type: ProductType
}
enum ProductType {
BOOK
FURNITURE
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
enum ProductType {
FURNITURE
BOOK
DIGITAL
}
`,
name: 'serviceB',
},
{
typeDefs: gql`
enum ProductType {
FURNITURE
BOOK
DIGITAL
}
`,
name: 'serviceC',
},
];
const definitionsDocument = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitionsDocument, schema, [MatchingEnums]);
expect(errors).toMatchInlineSnapshot(`
Array [
[GraphQLError: The \`ProductType\` enum does not have identical values in all services. Groups of services with identical values are: [serviceA], [serviceB, serviceC]],
]
`);
});
it('errors when an enum name is defined as another type in a service', () => {
const serviceList = [
{
typeDefs: gql`
enum ProductType {
BOOK
FURNITURE
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
type ProductType {
id: String
}
`,
name: 'serviceB',
},
{
typeDefs: gql`
enum ProductType {
FURNITURE
BOOK
DIGITAL
}
`,
name: 'serviceC',
},
];
const definitionsDocument = createDefinitionsDocumentForServices(
serviceList,
);
const errors = validateSDL(definitionsDocument, schema, [MatchingEnums]);
expect(errors).toMatchInlineSnapshot(`
Array [
[GraphQLError: [serviceA] ProductType -> ProductType is an enum in [serviceA, serviceC], but not in [serviceB]],
]
`);
});
});