-
Notifications
You must be signed in to change notification settings - Fork 242
/
matchingUnions.test.ts
132 lines (116 loc) 路 3.31 KB
/
matchingUnions.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
import {
GraphQLSchema,
specifiedDirectives,
Kind,
DocumentNode,
} from 'graphql';
import { validateSDL } from 'graphql/validation/validate';
import gql from 'graphql-tag';
import {
typeSerializer,
graphqlErrorSerializer,
} from 'apollo-federation-integration-testsuite';
import { UniqueUnionTypes } from '..';
import { ServiceDefinition } from '../../../types';
import { buildMapsFromServiceList } from '../../../compose';
import federationDirectives from '../../../../directives';
expect.addSnapshotSerializer(graphqlErrorSerializer);
expect.addSnapshotSerializer(typeSerializer);
function createDocumentsForServices(
serviceList: ServiceDefinition[],
): DocumentNode[] {
const { typeDefinitionsMap, typeExtensionsMap } = buildMapsFromServiceList(
serviceList,
);
return [
{
kind: Kind.DOCUMENT,
definitions: Object.values(typeDefinitionsMap).flat(),
},
{
kind: Kind.DOCUMENT,
definitions: Object.values(typeExtensionsMap).flat(),
},
];
}
describe('MatchingUnions', () => {
let schema: GraphQLSchema;
// create a blank schema for each test
beforeEach(() => {
schema = new GraphQLSchema({
query: undefined,
directives: [...specifiedDirectives, ...federationDirectives],
});
});
it('enforces unique union names on non-identical union types', () => {
const [definitions] = createDocumentsForServices([
{
typeDefs: gql`
union ProductOrError = Product | Error
type Error {
code: Int!
message: String!
}
type Product @key(fields: "sku") {
sku: ID!
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
union ProductOrError = Product
type Error {
code: Int!
message: String!
}
extend type Product @key(fields: "sku") {
sku: ID! @external
colors: [String]
}
`,
name: 'serviceB',
},
]);
const errors = validateSDL(definitions, schema, [UniqueUnionTypes]);
expect(errors).toHaveLength(1);
expect(errors[0]).toMatchInlineSnapshot(`
Object {
"code": "VALUE_TYPE_UNION_TYPES_MISMATCH",
"message": "[serviceA] ProductOrError -> The union \`ProductOrError\` is defined in services \`serviceA\` and \`serviceB\`, however their types do not match. Union types with the same name must also consist of identical types. The type Error is mismatched.",
}
`);
});
it('permits duplicate union names for identical union types', () => {
const [definitions] = createDocumentsForServices([
{
typeDefs: gql`
union ProductOrError = Product | Error
type Error {
code: Int!
message: String!
}
type Product @key(fields: "sku") {
sku: ID!
}
`,
name: 'serviceA',
},
{
typeDefs: gql`
union ProductOrError = Product | Error
type Error {
code: Int!
message: String!
}
type Product @key(fields: "sku") {
sku: ID!
}
`,
name: 'serviceB',
},
]);
const errors = validateSDL(definitions, schema, [UniqueUnionTypes]);
expect(errors).toHaveLength(0);
});
});