-
Notifications
You must be signed in to change notification settings - Fork 468
/
validation.ts
277 lines (248 loc) · 8.81 KB
/
validation.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import {
specifiedRules,
NoUnusedFragmentsRule,
GraphQLError,
FieldNode,
ValidationContext,
GraphQLSchema,
DocumentNode,
OperationDefinitionNode,
TypeInfo,
FragmentDefinitionNode,
visit,
visitWithTypeInfo,
visitInParallel,
getLocation,
InlineFragmentNode,
Kind,
isObjectType
} from "graphql";
import { TextEdit } from "vscode-languageserver";
import { ToolError, logError } from "./logger";
import { ValidationRule } from "graphql/validation/ValidationContext";
import { positionFromSourceLocation } from "../utilities/source";
import {
buildExecutionContext,
ExecutionContext
} from "graphql/execution/execute";
import { hasClientDirective, simpleCollectFields } from "../utilities/graphql";
import { Debug } from "../utilities";
export interface CodeActionInfo {
message: string;
edits: TextEdit[];
}
const specifiedRulesToBeRemoved = [NoUnusedFragmentsRule];
export const defaultValidationRules: ValidationRule[] = [
NoAnonymousQueries,
NoTypenameAlias,
NoMissingClientDirectives,
...specifiedRules.filter(rule => !specifiedRulesToBeRemoved.includes(rule))
];
export function getValidationErrors(
schema: GraphQLSchema,
document: DocumentNode,
fragments?: { [fragmentName: string]: FragmentDefinitionNode },
rules: ValidationRule[] = defaultValidationRules
) {
const typeInfo = new TypeInfo(schema);
// The 4th argument to `ValidationContext` is an `onError` callback. This was
// introduced by https://github.com/graphql/graphql-js/pull/2074 and first
// published in graphql@14.5.0. It is meant to replace the `getErrors` method
// which was previously used. Since we support versions of graphql older than
// that, it's possible that this callback will not be invoked and we'll need
// to resort to using `getErrors`. Therefore, although we'll collect errors
// via this callback, if `getErrors` is present on the context we create,
// we'll go ahead and use that instead.
const errors: GraphQLError[] = [];
const onError = (err: GraphQLError) => errors.push(err);
const context = new ValidationContext(schema, document, typeInfo, onError);
if (fragments) {
(context as any)._fragments = fragments;
}
const visitors = rules.map(rule => rule(context));
// Visit the whole document with each instance of all provided rules.
visit(document, visitWithTypeInfo(typeInfo, visitInParallel(visitors)));
// @ts-ignore
// `getErrors` is gone in `graphql@15`, but we still support older versions.
if (typeof context.getErrors === "function") return context.getErrors();
// If `getErrors` doesn't exist, we must be on a `graphql@15` or higher,
// so we'll use the errors we collected via the `onError` callback.
return errors;
}
export function validateQueryDocument(
schema: GraphQLSchema,
document: DocumentNode
) {
try {
const validationErrors = getValidationErrors(schema, document);
if (validationErrors && validationErrors.length > 0) {
for (const error of validationErrors) {
logError(error);
}
return Debug.error("Validation of GraphQL query document failed");
}
} catch (e) {
console.error(e);
throw e;
}
}
export function NoAnonymousQueries(context: ValidationContext) {
return {
OperationDefinition(node: OperationDefinitionNode) {
if (!node.name) {
context.reportError(
new GraphQLError("Apollo does not support anonymous operations", [
node
])
);
}
return false;
}
};
}
export function NoTypenameAlias(context: ValidationContext) {
return {
Field(node: FieldNode) {
const aliasName = node.alias && node.alias.value;
if (aliasName == "__typename") {
context.reportError(
new GraphQLError(
"Apollo needs to be able to insert __typename when needed, please do not use it as an alias",
[node]
)
);
}
}
};
}
function hasClientSchema(schema: GraphQLSchema): boolean {
const query = schema.getQueryType();
const mutation = schema.getMutationType();
const subscription = schema.getSubscriptionType();
return Boolean(
(query && query.clientSchema) ||
(mutation && mutation.clientSchema) ||
(subscription && subscription.clientSchema)
);
}
export function NoMissingClientDirectives(context: ValidationContext) {
const root = context.getDocument();
const schema = context.getSchema();
// early return if we don't have any client fields on the schema
if (!hasClientSchema(schema)) return {};
// this isn't really execution context, but it does group the fragments and operations
// together correctly
// XXX we have a simplified version of this in @apollo/gateway that we could probably use
// intead of this
const executionContext = buildExecutionContext(
schema,
root,
Object.create(null),
Object.create(null),
undefined,
undefined,
undefined
);
function visitor(
node: FieldNode | InlineFragmentNode | FragmentDefinitionNode
) {
// In cases where we are looking at a FragmentDefinition, there is no parent type
// but instead, the FragmentDefinition contains the type that we can read from the
// schema
const parentType =
node.kind === Kind.FRAGMENT_DEFINITION
? schema.getType(node.typeCondition.name.value)
: context.getParentType();
const fieldDef = context.getFieldDef();
// if we don't have a type to check then we can early return
if (!parentType) return;
// here we collect all of the fields on a type that are marked "local"
const clientFields =
parentType &&
isObjectType(parentType) &&
parentType.clientSchema &&
parentType.clientSchema.localFields;
// XXXX in the case of a fragment spread, the directive could be on the fragment definition
let clientDirectivePresent = hasClientDirective(node);
let message = "@client directive is missing on ";
let selectsClientFieldSet = false;
switch (node.kind) {
case Kind.FIELD:
// fields are simple because we can just see if the name exists in the local fields
// array on the parent type
selectsClientFieldSet = Boolean(
clientFields && clientFields.includes(fieldDef!.name)
);
message += `local field "${node.name.value}"`;
break;
case Kind.INLINE_FRAGMENT:
case Kind.FRAGMENT_DEFINITION:
// XXX why isn't this type checking below?
if (Array.isArray(executionContext)) break;
const fields = simpleCollectFields(
executionContext as ExecutionContext,
node.selectionSet,
Object.create(null),
Object.create(null)
);
// once we have a list of fields on the fragment, we can compare them
// to the list of types. The fields within a fragment need to be a
// subset of the overall local fields types
const fieldNames = Object.entries(fields).map(([name]) => name);
selectsClientFieldSet = fieldNames.every(
field => clientFields && clientFields.includes(field)
);
message += `fragment ${
"name" in node ? `"${node.name.value}" ` : ""
}around local fields "${fieldNames.join(",")}"`;
break;
}
// if the field's parent is part of the client schema and that type
// includes a field with the same name as this node, we can see
// if it has an @client directive to resolve locally
if (selectsClientFieldSet && !clientDirectivePresent) {
let extensions: { [key: string]: any } | null = null;
const name = "name" in node && node.name;
// TODO support code actions for inline fragments, fragment spreads, and fragment definitions
if (name && name.loc) {
let { source, end: locToInsertDirective } = name.loc;
if (
"arguments" in node &&
node.arguments &&
node.arguments.length !== 0
) {
// must insert directive after field arguments
const endOfArgs = source.body.indexOf(")", locToInsertDirective);
locToInsertDirective = endOfArgs + 1;
}
const codeAction: CodeActionInfo = {
message: `Add @client directive to "${name.value}"`,
edits: [
TextEdit.insert(
positionFromSourceLocation(
source,
getLocation(source, locToInsertDirective)
),
" @client"
)
]
};
extensions = { codeAction };
}
context.reportError(
new GraphQLError(message, [node], null, null, null, null, extensions)
);
}
// if we have selected a client field, no need to continue to recurse
if (selectsClientFieldSet) {
return false;
}
return;
}
return {
InlineFragment: visitor,
FragmentDefinition: visitor,
Field: visitor
// TODO support directives on FragmentSpread
};
}