-
-
Notifications
You must be signed in to change notification settings - Fork 455
/
schemaPredicates.ts
256 lines (235 loc) · 7.11 KB
/
schemaPredicates.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { InlineFragmentNode, FragmentDefinitionNode } from 'graphql';
import { warn, invariant } from '../helpers/help';
import { getTypeCondition } from './node';
import { SchemaIntrospector, SchemaObject } from './schema';
import {
KeyingConfig,
UpdateResolver,
ResolverConfig,
OptimisticMutationConfig,
} from '../types';
const BUILTIN_FIELD_RE = /^__/;
export const isFieldNullable = (
schema: SchemaIntrospector,
typename: string,
fieldName: string
): boolean => {
if (BUILTIN_FIELD_RE.test(fieldName)) return true;
const field = getField(schema, typename, fieldName);
return !!field && field.type.kind !== 'NON_NULL';
};
export const isListNullable = (
schema: SchemaIntrospector,
typename: string,
fieldName: string
): boolean => {
const field = getField(schema, typename, fieldName);
if (!field) return false;
const ofType =
field.type.kind === 'NON_NULL' ? field.type.ofType : field.type;
return ofType.kind === 'LIST' && ofType.ofType.kind !== 'NON_NULL';
};
export const isFieldAvailableOnType = (
schema: SchemaIntrospector,
typename: string,
fieldName: string
): boolean => {
if (BUILTIN_FIELD_RE.test(fieldName)) return true;
return !!getField(schema, typename, fieldName);
};
export const isInterfaceOfType = (
schema: SchemaIntrospector,
node: InlineFragmentNode | FragmentDefinitionNode,
typename: string | void
): boolean => {
if (!typename) return false;
const typeCondition = getTypeCondition(node);
if (!typeCondition || typename === typeCondition) return true;
if (
schema.types![typeCondition] &&
schema.types![typeCondition].kind === 'OBJECT'
)
return typeCondition === typename;
expectAbstractType(schema, typeCondition!);
expectObjectType(schema, typename!);
return schema.isSubType(typeCondition, typename);
};
const getField = (
schema: SchemaIntrospector,
typename: string,
fieldName: string
) => {
expectObjectType(schema, typename);
const object = schema.types![typename] as SchemaObject;
const field = object.fields[fieldName];
if (!field) {
warn(
'Invalid field: The field `' +
fieldName +
'` does not exist on `' +
typename +
'`, ' +
'but the GraphQL document expects it to exist.\n' +
'Traversal will continue, however this may lead to undefined behavior!',
4
);
}
return field;
};
function expectObjectType(schema: SchemaIntrospector, typename: string) {
invariant(
schema.types![typename] && schema.types![typename].kind === 'OBJECT',
'Invalid Object type: The type `' +
typename +
'` is not an object in the defined schema, ' +
'but the GraphQL document is traversing it.',
3
);
}
function expectAbstractType(schema: SchemaIntrospector, typename: string) {
invariant(
schema.types![typename] &&
(schema.types![typename].kind === 'INTERFACE' ||
schema.types![typename].kind === 'UNION'),
'Invalid Abstract type: The type `' +
typename +
'` is not an Interface or Union type in the defined schema, ' +
'but a fragment in the GraphQL document is using it as a type condition.',
5
);
}
export function expectValidKeyingConfig(
schema: SchemaIntrospector,
keys: KeyingConfig
): void {
if (process.env.NODE_ENV !== 'production') {
for (const key in keys) {
if (!schema.types![key]) {
warn(
'Invalid Object type: The type `' +
key +
'` is not an object in the defined schema, but the `keys` option is referencing it.',
20
);
}
}
}
}
export function expectValidUpdatesConfig(
schema: SchemaIntrospector,
updates: Record<string, Record<string, UpdateResolver>>
): void {
if (process.env.NODE_ENV === 'production') {
return;
}
if (schema.mutation) {
const mutationFields = (schema.types![schema.mutation] as SchemaObject)
.fields;
const givenMutations = updates[schema.mutation] || {};
for (const fieldName in givenMutations) {
if (mutationFields[fieldName] === undefined) {
warn(
'Invalid mutation field: `' +
fieldName +
'` is not in the defined schema, but the `updates.Mutation` option is referencing it.',
21
);
}
}
}
if (schema.subscription) {
const subscriptionFields = (schema.types![
schema.subscription
] as SchemaObject).fields;
const givenSubscription = updates[schema.subscription] || {};
for (const fieldName in givenSubscription) {
if (subscriptionFields[fieldName] === undefined) {
warn(
'Invalid subscription field: `' +
fieldName +
'` is not in the defined schema, but the `updates.Subscription` option is referencing it.',
22
);
}
}
}
}
function warnAboutResolver(name: string): void {
warn(
`Invalid resolver: \`${name}\` is not in the defined schema, but the \`resolvers\` option is referencing it.`,
23
);
}
function warnAboutAbstractResolver(
name: string,
kind: 'UNION' | 'INTERFACE'
): void {
warn(
`Invalid resolver: \`${name}\` does not match to a concrete type in the schema, but the \`resolvers\` option is referencing it. Implement the resolver for the types that ${
kind === 'UNION' ? 'make up the union' : 'implement the interface'
} instead.`,
26
);
}
export function expectValidResolversConfig(
schema: SchemaIntrospector,
resolvers: ResolverConfig
): void {
if (process.env.NODE_ENV === 'production') {
return;
}
for (const key in resolvers) {
if (key === 'Query') {
if (schema.query) {
const validQueries = (schema.types![schema.query] as SchemaObject)
.fields;
for (const resolverQuery in resolvers.Query) {
if (!validQueries[resolverQuery]) {
warnAboutResolver('Query.' + resolverQuery);
}
}
} else {
warnAboutResolver('Query');
}
} else {
if (!schema.types![key]) {
warnAboutResolver(key);
} else if (
schema.types![key].kind === 'INTERFACE' ||
schema.types![key].kind === 'UNION'
) {
warnAboutAbstractResolver(
key,
schema.types![key].kind as 'INTERFACE' | 'UNION'
);
} else {
const validTypeProperties = (schema.types![key] as SchemaObject).fields;
for (const resolverProperty in resolvers[key]) {
if (!validTypeProperties[resolverProperty]) {
warnAboutResolver(key + '.' + resolverProperty);
}
}
}
}
}
}
export function expectValidOptimisticMutationsConfig(
schema: SchemaIntrospector,
optimisticMutations: OptimisticMutationConfig
): void {
if (process.env.NODE_ENV === 'production') {
return;
}
if (schema.mutation) {
const validMutations = (schema.types![schema.mutation] as SchemaObject)
.fields;
for (const mutation in optimisticMutations) {
if (!validMutations[mutation]) {
warn(
`Invalid optimistic mutation field: \`${mutation}\` is not a mutation field in the defined schema, but the \`optimistic\` option is referencing it.`,
24
);
}
}
}
}