-
Notifications
You must be signed in to change notification settings - Fork 242
/
keysMatchBaseService.test.ts
121 lines (108 loc) 路 3.36 KB
/
keysMatchBaseService.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
import gql from 'graphql-tag';
import { composeServices } from '../../../compose';
import { keysMatchBaseService as validateKeysMatchBaseService } from '../';
import { graphqlErrorSerializer } from 'apollo-federation-integration-testsuite';
import { assertCompositionSuccess } from '../../../utils';
expect.addSnapshotSerializer(graphqlErrorSerializer);
describe('keysMatchBaseService', () => {
it('returns no errors with proper @key usage', () => {
const serviceA = {
typeDefs: gql`
type Product @key(fields: "sku") {
sku: String!
upc: String!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
extend type Product @key(fields: "sku") {
sku: String! @external
price: Int!
}
`,
name: 'serviceB',
};
const serviceList = [serviceA, serviceB];
const compositionResult = composeServices(serviceList);
assertCompositionSuccess(compositionResult);
const { schema } = compositionResult;
const validationErrors = validateKeysMatchBaseService({
schema,
serviceList,
});
expect(validationErrors).toHaveLength(0);
});
it('requires a @key to be specified on the originating type', () => {
const serviceA = {
typeDefs: gql`
type Product {
sku: String!
upc: String!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
extend type Product @key(fields: "sku") {
sku: String! @external
price: Int!
}
`,
name: 'serviceB',
};
const serviceList = [serviceA, serviceB];
const compositionResult = composeServices(serviceList);
assertCompositionSuccess(compositionResult);
const { schema } = compositionResult;
const validationErrors = validateKeysMatchBaseService({
schema,
serviceList,
});
expect(validationErrors).toHaveLength(1);
expect(validationErrors[0]).toMatchInlineSnapshot(`
Object {
"code": "KEY_MISSING_ON_BASE",
"message": "[serviceA] Product -> appears to be an entity but no @key directives are specified on the originating type.",
}
`);
});
it('requires extending services to use a @key specified by the originating type', () => {
const serviceA = {
typeDefs: gql`
type Product @key(fields: "sku upc") {
sku: String!
upc: String!
}
`,
name: 'serviceA',
};
const serviceB = {
typeDefs: gql`
extend type Product @key(fields: "sku") {
sku: String! @external
price: Int!
}
`,
name: 'serviceB',
};
const serviceList = [serviceA, serviceB];
const compositionResult = composeServices(serviceList);
assertCompositionSuccess(compositionResult);
const { schema } = compositionResult;
const validationErrors = validateKeysMatchBaseService({
schema,
serviceList,
});
expect(validationErrors).toHaveLength(1);
expect(validationErrors[0]).toMatchInlineSnapshot(`
Object {
"code": "KEY_NOT_SPECIFIED",
"message": "[serviceB] Product -> extends from serviceA but specifies an invalid @key directive. Valid @key directives are specified by the originating type. Available @key directives for this type are:
@key(fields: \\"sku upc\\")",
}
`);
});
});