-
-
Notifications
You must be signed in to change notification settings - Fork 454
/
schemaPredicates.ts
256 lines (231 loc) · 6.72 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 {
isNullableType,
isListType,
isNonNullType,
GraphQLSchema,
GraphQLAbstractType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
} from 'graphql';
import { warn, invariant } from '../helpers/help';
import {
KeyingConfig,
UpdatesConfig,
ResolverConfig,
OptimisticMutationConfig,
} from '../types';
export const isFieldNullable = (
schema: GraphQLSchema,
typename: string,
fieldName: string
): boolean => {
const field = getField(schema, typename, fieldName);
return !!field && isNullableType(field.type);
};
export const isListNullable = (
schema: GraphQLSchema,
typename: string,
fieldName: string
): boolean => {
const field = getField(schema, typename, fieldName);
if (!field) return false;
const ofType = isNonNullType(field.type) ? field.type.ofType : field.type;
return isListType(ofType) && isNullableType(ofType.ofType);
};
export const isFieldAvailableOnType = (
schema: GraphQLSchema,
typename: string,
fieldName: string
): boolean => {
return !!getField(schema, typename, fieldName);
};
export const isInterfaceOfType = (
schema: GraphQLSchema,
typeCondition: null | string,
typename: string | void
): boolean => {
if (!typename || !typeCondition) return false;
if (typename === typeCondition) return true;
const abstractType = schema.getType(typeCondition);
const objectType = schema.getType(typename);
if (abstractType instanceof GraphQLObjectType) {
return abstractType === objectType;
}
expectAbstractType(abstractType, typeCondition);
expectObjectType(objectType, typename);
return schema.isPossibleType(abstractType, objectType);
};
const getField = (
schema: GraphQLSchema,
typename: string,
fieldName: string
) => {
const object = schema.getType(typename);
expectObjectType(object, typename);
const field = object.getFields()[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(
x: any,
typename: string
): asserts x is GraphQLObjectType {
invariant(
x instanceof GraphQLObjectType,
'Invalid Object type: The type `' +
typename +
'` is not an object in the defined schema, ' +
'but the GraphQL document is traversing it.',
3
);
}
function expectAbstractType(
x: any,
typename: string
): asserts x is GraphQLAbstractType {
invariant(
x instanceof GraphQLInterfaceType || x instanceof GraphQLUnionType,
'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: GraphQLSchema,
keys: KeyingConfig
): void {
if (process.env.NODE_ENV !== 'production') {
const types = Object.keys(schema.getTypeMap());
Object.keys(keys).forEach(key => {
if (types.indexOf(key) === -1) {
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: GraphQLSchema,
updates: UpdatesConfig
): void {
if (process.env.NODE_ENV === 'production') {
return;
}
/* eslint-disable prettier/prettier */
const schemaMutations = schema.getMutationType()
? Object.keys((schema.getMutationType() as GraphQLObjectType).toConfig().fields)
: [];
const schemaSubscriptions = schema.getSubscriptionType()
? Object.keys((schema.getSubscriptionType() as GraphQLObjectType).toConfig().fields)
: [];
const givenMutations = updates.Mutation
? Object.keys(updates.Mutation)
: [];
const givenSubscriptions = updates.Subscription
? Object.keys(updates.Subscription)
: [];
/* eslint-enable prettier/prettier */
for (const givenMutation of givenMutations) {
if (schemaMutations.indexOf(givenMutation) === -1) {
warn(
'Invalid mutation field: `' +
givenMutation +
'` is not in the defined schema, but the `updates.Mutation` option is referencing it.',
21
);
}
}
for (const givenSubscription of givenSubscriptions) {
if (schemaSubscriptions.indexOf(givenSubscription) === -1) {
warn(
'Invalid subscription field: `' +
givenSubscription +
'` 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
);
}
export function expectValidResolversConfig(
schema: GraphQLSchema,
resolvers: ResolverConfig
): void {
if (process.env.NODE_ENV === 'production') {
return;
}
const validTypes = Object.keys(schema.getTypeMap());
for (const key in resolvers) {
if (key === 'Query') {
const queryType = schema.getQueryType();
if (queryType) {
const validQueries = Object.keys(queryType.toConfig().fields);
for (const resolverQuery in resolvers.Query) {
if (validQueries.indexOf(resolverQuery) === -1) {
warnAboutResolver('Query.' + resolverQuery);
}
}
} else {
warnAboutResolver('Query');
}
} else {
if (validTypes.indexOf(key) === -1) {
warnAboutResolver(key);
} else {
const validTypeProperties = Object.keys(
(schema.getType(key) as GraphQLObjectType).getFields()
);
const resolverProperties = Object.keys(resolvers[key]);
for (const resolverProperty of resolverProperties) {
if (validTypeProperties.indexOf(resolverProperty) === -1) {
warnAboutResolver(key + '.' + resolverProperty);
}
}
}
}
}
}
export function expectValidOptimisticMutationsConfig(
schema: GraphQLSchema,
optimisticMutations: OptimisticMutationConfig
): void {
if (process.env.NODE_ENV === 'production') {
return;
}
const validMutations = schema.getMutationType()
? Object.keys(
(schema.getMutationType() as GraphQLObjectType).toConfig().fields
)
: [];
for (const mutation in optimisticMutations) {
if (validMutations.indexOf(mutation) === -1) {
warn(
`Invalid optimistic mutation field: \`${mutation}\` is not a mutation field in the defined schema, but the \`optimistic\` option is referencing it.`,
24
);
}
}
}