-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
graphql-js-validation.ts
336 lines (315 loc) · 13.6 KB
/
graphql-js-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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import { validate, GraphQLSchema, DocumentNode, ASTNode, ValidationRule } from 'graphql';
import { validateSDL } from 'graphql/validation/validate';
import { parseImportLine, processImport } from '@graphql-tools/import';
import { existsSync } from 'fs';
import { join, dirname } from 'path';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { getLocation, requireGraphQLSchemaFromContext, requireSiblingsOperations } from '../utils';
import { GraphQLESTreeNode } from '../estree-parser';
function extractRuleName(stack?: string): string | null {
const match = (stack || '').match(/validation[/\\\\]rules[/\\\\](.*?)\.js:/) || [];
return match[1] || null;
}
export function validateDoc(
sourceNode: GraphQLESTreeNode<ASTNode>,
context: GraphQLESLintRuleContext,
schema: GraphQLSchema | null,
documentNode: DocumentNode,
rules: ReadonlyArray<ValidationRule>,
ruleName: string | null = null
): void {
if (documentNode?.definitions?.length > 0) {
try {
const validationErrors = schema ? validate(schema, documentNode, rules) : validateSDL(documentNode, null, rules as any);
for (const error of validationErrors) {
const validateRuleName = ruleName || `[${extractRuleName(error.stack)}]`;
context.report({
loc: getLocation({ start: error.locations[0] }),
message: ruleName ? error.message : `${validateRuleName} ${error.message}`,
});
}
} catch (e) {
context.report({
node: sourceNode,
message: e.message,
});
}
}
}
const isGraphQLImportFile = rawSDL => {
const trimmedRawSDL = rawSDL.trimLeft();
return trimmedRawSDL.startsWith('# import') || trimmedRawSDL.startsWith('#import');
};
const validationToRule = (
name: string,
ruleName: string,
docs: Partial<GraphQLESLintRule['meta']['docs']>,
getDocumentNode?: (context: GraphQLESLintRuleContext) => DocumentNode | null
): Record<typeof name, GraphQLESLintRule<any, true>> => {
let ruleFn: null | ValidationRule = null;
try {
ruleFn = require(`graphql/validation/rules/${ruleName}Rule`)[`${ruleName}Rule`];
} catch (e) {
try {
ruleFn = require(`graphql/validation/rules/${ruleName}`)[`${ruleName}Rule`];
} catch (e) {
ruleFn = require('graphql/validation')[`${ruleName}Rule`];
}
}
const requiresSchema = docs.requiresSchema ?? true;
return {
[name]: {
meta: {
docs: {
...docs,
graphQLJSRuleName: ruleName,
category: 'Validation',
recommended: true,
requiresSchema,
url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/${name}.md`,
description: `${docs.description}\n\n> This rule is a wrapper around a \`graphql-js\` validation function. [You can find its source code here](https://github.com/graphql/graphql-js/blob/main/src/validation/rules/${ruleName}Rule.ts).`,
},
},
create(context) {
return {
Document(node) {
if (!ruleFn) {
// eslint-disable-next-line no-console
console.warn(
`You rule "${name}" depends on a GraphQL validation rule ("${ruleName}") but it's not available in the "graphql-js" version you are using. Skipping...`
);
return;
}
const schema = requiresSchema ? requireGraphQLSchemaFromContext(name, context) : null;
let documentNode: DocumentNode;
const isRealFile = existsSync(context.getFilename());
if (isRealFile && getDocumentNode) {
documentNode = getDocumentNode(context);
}
validateDoc(node, context, schema, documentNode || node.rawNode(), [ruleFn], ruleName);
},
};
},
},
};
};
const importFiles = (context: GraphQLESLintRuleContext) => {
const code = context.getSourceCode().text;
if (!isGraphQLImportFile(code)) {
return null;
}
// Import documents because file contains '#import' comments
return processImport(context.getFilename());
};
export const GRAPHQL_JS_VALIDATIONS = Object.assign(
{},
validationToRule('executable-definitions', 'ExecutableDefinitions', {
description: `A GraphQL document is only valid for execution if all definitions are either operation or fragment definitions.`,
}),
validationToRule('fields-on-correct-type', 'FieldsOnCorrectType', {
description:
'A GraphQL document is only valid if all fields selected are defined by the parent type, or are an allowed meta field such as `__typename`.',
}),
validationToRule('fragments-on-composite-type', 'FragmentsOnCompositeTypes', {
description: `Fragments use a type condition to determine if they apply, since fragments can only be spread into a composite type (object, interface, or union), the type condition must also be a composite type.`,
}),
validationToRule('known-argument-names', 'KnownArgumentNames', {
description: `A GraphQL field is only valid if all supplied arguments are defined by that field.`,
}),
validationToRule('known-directives', 'KnownDirectives', {
description: `A GraphQL document is only valid if all \`@directives\` are known by the schema and legally positioned.`,
}),
validationToRule(
'known-fragment-names',
'KnownFragmentNames',
{
description: `A GraphQL document is only valid if all \`...Fragment\` fragment spreads refer to fragments defined in the same document.`,
examples: [
{
title: 'Incorrect (fragment not defined in the document)',
code: /* GraphQL */ `
query {
user {
id
...UserFields
}
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
fragment UserFields on User {
firstName
lastName
}
query {
user {
id
...UserFields
}
}
`,
},
{
title: 'Correct (existing import to UserFields fragment)',
code: /* GraphQL */ `
#import '../UserFields.gql'
query {
user {
id
...UserFields
}
}
`,
},
{
title:
"False positive case\n\nFor extracting documents from code under the hood we use [graphql-tag-pluck](https://graphql-tools.com/docs/graphql-tag-pluck) that [don't support string interpolation](https://stackoverflow.com/questions/62749847/graphql-codegen-dynamic-fields-with-interpolation/62751311#62751311) for this moment.",
code: `
const USER_FIELDS = gql\`
fragment UserFields on User {
id
}
\`
const GET_USER = /* GraphQL */ \`
# eslint @graphql-eslint/known-fragment-names: 'error'
query User {
user {
...UserFields
}
}
# Will give false positive error 'Unknown fragment "UserFields"'
\${USER_FIELDS}
\``,
},
],
},
importFiles
),
validationToRule('known-type-names', 'KnownTypeNames', {
description: `A GraphQL document is only valid if referenced types (specifically variable definitions and fragment conditions) are defined by the type schema.`,
}),
validationToRule('lone-anonymous-operation', 'LoneAnonymousOperation', {
description: `A GraphQL document is only valid if when it contains an anonymous operation (the query short-hand) that it contains only that one operation definition.`,
}),
validationToRule('lone-schema-definition', 'LoneSchemaDefinition', {
description: `A GraphQL document is only valid if it contains only one schema definition.`,
requiresSchema: false,
}),
validationToRule('no-fragment-cycles', 'NoFragmentCycles', {
description: `A GraphQL fragment is only valid when it does not have cycles in fragments usage.`,
}),
validationToRule(
'no-undefined-variables',
'NoUndefinedVariables',
{
description: `A GraphQL operation is only valid if all variables encountered, both directly and via fragment spreads, are defined by that operation.`,
},
importFiles
),
validationToRule(
'no-unused-fragments',
'NoUnusedFragments',
{
description: `A GraphQL document is only valid if all fragment definitions are spread within operations, or spread within other fragments spread within operations.`,
requiresSiblings: true,
},
context => {
const siblings = requireSiblingsOperations('no-unused-fragments', context);
const documents = [...siblings.getOperations(), ...siblings.getFragments()]
.filter(({ document }) => isGraphQLImportFile(document.loc.source.body))
.map(({ filePath, document }) => ({
filePath,
code: document.loc.source.body,
}));
const getParentNode = (filePath: string): DocumentNode | null => {
for (const { filePath: docFilePath, code } of documents) {
const isFileImported = code
.split('\n')
.filter(isGraphQLImportFile)
.map(line => parseImportLine(line.replace('#', '')))
.some(o => filePath === join(dirname(docFilePath), o.from));
if (!isFileImported) {
continue;
}
// Import first file that import this file
const document = processImport(docFilePath);
// Import most top file that import this file
return getParentNode(docFilePath) || document;
}
return null;
};
return getParentNode(context.getFilename());
}
),
validationToRule(
'no-unused-variables',
'NoUnusedVariables',
{
description: `A GraphQL operation is only valid if all variables defined by an operation are used, either directly or within a spread fragment.`,
},
importFiles
),
validationToRule('overlapping-fields-can-be-merged', 'OverlappingFieldsCanBeMerged', {
description: `A selection set is only valid if all fields (including spreading any fragments) either correspond to distinct response names or can be merged without ambiguity.`,
}),
validationToRule('possible-fragment-spread', 'PossibleFragmentSpreads', {
description: `A fragment spread is only valid if the type condition could ever possibly be true: if there is a non-empty intersection of the possible parent types, and possible types which pass the type condition.`,
}),
validationToRule('possible-type-extension', 'PossibleTypeExtensions', {
description: `A type extension is only valid if the type is defined and has the same kind.`,
requiresSchema: false,
}),
validationToRule('provided-required-arguments', 'ProvidedRequiredArguments', {
description: `A field or directive is only valid if all required (non-null without a default value) field arguments have been provided.`,
}),
validationToRule('scalar-leafs', 'ScalarLeafs', {
description: `A GraphQL document is valid only if all leaf fields (fields without sub selections) are of scalar or enum types.`,
}),
validationToRule('one-field-subscriptions', 'SingleFieldSubscriptions', {
description: `A GraphQL subscription is valid only if it contains a single root field.`,
}),
validationToRule('unique-argument-names', 'UniqueArgumentNames', {
description: `A GraphQL field or directive is only valid if all supplied arguments are uniquely named.`,
}),
validationToRule('unique-directive-names', 'UniqueDirectiveNames', {
description: `A GraphQL document is only valid if all defined directives have unique names.`,
requiresSchema: false,
}),
validationToRule('unique-directive-names-per-location', 'UniqueDirectivesPerLocation', {
description: `A GraphQL document is only valid if all non-repeatable directives at a given location are uniquely named.`,
}),
validationToRule('unique-enum-value-names', 'UniqueEnumValueNames', {
description: `A GraphQL enum type is only valid if all its values are uniquely named.`,
requiresSchema: false,
}),
validationToRule('unique-field-definition-names', 'UniqueFieldDefinitionNames', {
description: `A GraphQL complex type is only valid if all its fields are uniquely named.`,
requiresSchema: false,
}),
validationToRule('unique-input-field-names', 'UniqueInputFieldNames', {
description: `A GraphQL input object value is only valid if all supplied fields are uniquely named.`,
requiresSchema: false,
}),
validationToRule('unique-operation-types', 'UniqueOperationTypes', {
description: `A GraphQL document is only valid if it has only one type per operation.`,
requiresSchema: false,
}),
validationToRule('unique-type-names', 'UniqueTypeNames', {
description: `A GraphQL document is only valid if all defined types have unique names.`,
requiresSchema: false,
}),
validationToRule('unique-variable-names', 'UniqueVariableNames', {
description: `A GraphQL operation is only valid if all its variables are uniquely named.`,
}),
validationToRule('value-literals-of-correct-type', 'ValuesOfCorrectType', {
description: `A GraphQL document is only valid if all value literals are of the type expected at their position.`,
}),
validationToRule('variables-are-input-types', 'VariablesAreInputTypes', {
description: `A GraphQL operation is only valid if all the variables it defines are of input types (scalar, enum, or input object).`,
}),
validationToRule('variables-in-allowed-position', 'VariablesInAllowedPosition', {
description: `Variables passed to field arguments conform to type.`,
})
) as Record<string, GraphQLESLintRule>;