@graphql-eslint/eslint-plugin 3.2.0-alpha-4ca7218.0 → 3.3.0-alpha-b07557f.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/rules/require-id-when-available.md +16 -1
- package/graphql-config.d.ts +2 -1
- package/index.js +246 -176
- package/index.mjs +246 -176
- package/package.json +3 -2
- package/rules/index.d.ts +2 -129
- package/rules/match-document-filename.d.ts +3 -3
- package/rules/naming-convention.d.ts +1 -1
- package/types.d.ts +1 -0
- package/utils.d.ts +2 -7
package/index.mjs
CHANGED
@@ -4,10 +4,10 @@ import { statSync, existsSync, readFileSync } from 'fs';
|
|
4
4
|
import { dirname, extname, basename, relative, resolve } from 'path';
|
5
5
|
import { asArray, parseGraphQLSDL } from '@graphql-tools/utils';
|
6
6
|
import lowerCase from 'lodash.lowercase';
|
7
|
-
import depthLimit from 'graphql-depth-limit';
|
8
|
-
import { parseCode } from '@graphql-tools/graphql-tag-pluck';
|
9
7
|
import { loadConfigSync, GraphQLConfig } from 'graphql-config';
|
10
8
|
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
|
9
|
+
import depthLimit from 'graphql-depth-limit';
|
10
|
+
import { parseCode } from '@graphql-tools/graphql-tag-pluck';
|
11
11
|
import { RuleTester, Linter } from 'eslint';
|
12
12
|
import { codeFrameColumns } from '@babel/code-frame';
|
13
13
|
|
@@ -168,6 +168,47 @@ const configs = {
|
|
168
168
|
'operations-all': operationsAllConfig,
|
169
169
|
};
|
170
170
|
|
171
|
+
let graphQLConfig;
|
172
|
+
function loadCachedGraphQLConfig(options) {
|
173
|
+
// We don't want cache config on test environment
|
174
|
+
// Otherwise schema and documents will be same for all tests
|
175
|
+
if (process.env.NODE_ENV !== 'test' && graphQLConfig) {
|
176
|
+
return graphQLConfig;
|
177
|
+
}
|
178
|
+
graphQLConfig = loadGraphQLConfig(options);
|
179
|
+
return graphQLConfig;
|
180
|
+
}
|
181
|
+
function loadGraphQLConfig(options) {
|
182
|
+
const onDiskConfig = options.skipGraphQLConfig
|
183
|
+
? null
|
184
|
+
: loadConfigSync({
|
185
|
+
throwOnEmpty: false,
|
186
|
+
throwOnMissing: false,
|
187
|
+
extensions: [addCodeFileLoaderExtension],
|
188
|
+
});
|
189
|
+
const configOptions = options.projects
|
190
|
+
? { projects: options.projects }
|
191
|
+
: {
|
192
|
+
schema: (options.schema || ''),
|
193
|
+
documents: options.documents || options.operations,
|
194
|
+
extensions: options.extensions,
|
195
|
+
include: options.include,
|
196
|
+
exclude: options.exclude,
|
197
|
+
};
|
198
|
+
graphQLConfig =
|
199
|
+
onDiskConfig ||
|
200
|
+
new GraphQLConfig({
|
201
|
+
config: configOptions,
|
202
|
+
filepath: 'virtual-config',
|
203
|
+
}, [addCodeFileLoaderExtension]);
|
204
|
+
return graphQLConfig;
|
205
|
+
}
|
206
|
+
const addCodeFileLoaderExtension = api => {
|
207
|
+
api.loaders.schema.register(new CodeFileLoader());
|
208
|
+
api.loaders.documents.register(new CodeFileLoader());
|
209
|
+
return { name: 'graphql-eslint-loaders' };
|
210
|
+
};
|
211
|
+
|
171
212
|
function requireSiblingsOperations(ruleName, context) {
|
172
213
|
if (!context.parserServices) {
|
173
214
|
throw new Error(`Rule '${ruleName}' requires 'parserOptions.operations' to be set and loaded. See http://bit.ly/graphql-eslint-operations for more info`);
|
@@ -186,6 +227,32 @@ function requireGraphQLSchemaFromContext(ruleName, context) {
|
|
186
227
|
}
|
187
228
|
return context.parserServices.schema;
|
188
229
|
}
|
230
|
+
const schemaToExtendCache = new Map();
|
231
|
+
function getGraphQLSchemaToExtend(context) {
|
232
|
+
// If parserOptions.schema not set or not loaded, there is no reason to make partial schema aka schemaToExtend
|
233
|
+
if (!context.parserServices.hasTypeInfo) {
|
234
|
+
return null;
|
235
|
+
}
|
236
|
+
const filename = context.getPhysicalFilename();
|
237
|
+
if (!schemaToExtendCache.has(filename)) {
|
238
|
+
const { schema, schemaOptions } = context.parserOptions;
|
239
|
+
const gqlConfig = loadGraphQLConfig({ schema });
|
240
|
+
const projectForFile = gqlConfig.getProjectForFile(filename);
|
241
|
+
let schemaToExtend;
|
242
|
+
try {
|
243
|
+
schemaToExtend = projectForFile.loadSchemaSync(projectForFile.schema, 'GraphQLSchema', {
|
244
|
+
...schemaOptions,
|
245
|
+
ignore: filename,
|
246
|
+
});
|
247
|
+
}
|
248
|
+
catch (_a) {
|
249
|
+
// If error throws just ignore it because maybe schema is located in 1 file
|
250
|
+
schemaToExtend = null;
|
251
|
+
}
|
252
|
+
schemaToExtendCache.set(filename, schemaToExtend);
|
253
|
+
}
|
254
|
+
return schemaToExtendCache.get(filename);
|
255
|
+
}
|
189
256
|
function requireReachableTypesFromContext(ruleName, context) {
|
190
257
|
const schema = requireGraphQLSchemaFromContext(ruleName, context);
|
191
258
|
return context.parserServices.reachableTypes(schema);
|
@@ -279,14 +346,6 @@ const TYPES_KINDS = [
|
|
279
346
|
Kind.INPUT_OBJECT_TYPE_DEFINITION,
|
280
347
|
Kind.UNION_TYPE_DEFINITION,
|
281
348
|
];
|
282
|
-
var CaseStyle;
|
283
|
-
(function (CaseStyle) {
|
284
|
-
CaseStyle["camelCase"] = "camelCase";
|
285
|
-
CaseStyle["pascalCase"] = "PascalCase";
|
286
|
-
CaseStyle["snakeCase"] = "snake_case";
|
287
|
-
CaseStyle["upperCase"] = "UPPER_CASE";
|
288
|
-
CaseStyle["kebabCase"] = "kebab-case";
|
289
|
-
})(CaseStyle || (CaseStyle = {}));
|
290
349
|
const pascalCase = (str) => lowerCase(str)
|
291
350
|
.split(' ')
|
292
351
|
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
|
@@ -297,15 +356,15 @@ const camelCase = (str) => {
|
|
297
356
|
};
|
298
357
|
const convertCase = (style, str) => {
|
299
358
|
switch (style) {
|
300
|
-
case
|
359
|
+
case 'camelCase':
|
301
360
|
return camelCase(str);
|
302
|
-
case
|
361
|
+
case 'PascalCase':
|
303
362
|
return pascalCase(str);
|
304
|
-
case
|
363
|
+
case 'snake_case':
|
305
364
|
return lowerCase(str).replace(/ /g, '_');
|
306
|
-
case
|
365
|
+
case 'UPPER_CASE':
|
307
366
|
return lowerCase(str).replace(/ /g, '_').toUpperCase();
|
308
|
-
case
|
367
|
+
case 'kebab-case':
|
309
368
|
return lowerCase(str).replace(/ /g, '-');
|
310
369
|
}
|
311
370
|
};
|
@@ -328,17 +387,32 @@ function getLocation(loc, fieldName = '', offset) {
|
|
328
387
|
};
|
329
388
|
}
|
330
389
|
|
331
|
-
function validateDocument(sourceNode, context, schema, documentNode, rule) {
|
390
|
+
function validateDocument(sourceNode, context, schema = null, documentNode, rule, isSchemaToExtend = false) {
|
332
391
|
if (documentNode.definitions.length === 0) {
|
333
392
|
return;
|
334
393
|
}
|
335
394
|
try {
|
336
|
-
const validationErrors = schema
|
395
|
+
const validationErrors = schema && !isSchemaToExtend
|
337
396
|
? validate(schema, documentNode, [rule])
|
338
|
-
: validateSDL(documentNode,
|
397
|
+
: validateSDL(documentNode, schema, [rule]);
|
339
398
|
for (const error of validationErrors) {
|
399
|
+
/*
|
400
|
+
* TODO: Fix ESTree-AST converter because currently it's incorrectly convert loc.end
|
401
|
+
* Example: loc.end always equal loc.start
|
402
|
+
* {
|
403
|
+
* token: {
|
404
|
+
* type: 'Name',
|
405
|
+
* loc: { start: { line: 4, column: 13 }, end: { line: 4, column: 13 } },
|
406
|
+
* value: 'veryBad',
|
407
|
+
* range: [ 40, 47 ]
|
408
|
+
* }
|
409
|
+
* }
|
410
|
+
*/
|
411
|
+
const { line, column } = error.locations[0];
|
412
|
+
const ancestors = context.getAncestors();
|
413
|
+
const token = ancestors[0].tokens.find(token => token.loc.start.line === line && token.loc.start.column === column);
|
340
414
|
context.report({
|
341
|
-
loc: getLocation({ start: error.locations[0] }),
|
415
|
+
loc: getLocation({ start: error.locations[0] }, token === null || token === void 0 ? void 0 : token.value),
|
342
416
|
message: error.message,
|
343
417
|
});
|
344
418
|
}
|
@@ -427,18 +501,24 @@ const validationToRule = (ruleId, ruleName, docs, getDocumentNode) => {
|
|
427
501
|
},
|
428
502
|
},
|
429
503
|
create(context) {
|
504
|
+
if (!ruleFn) {
|
505
|
+
// eslint-disable-next-line no-console
|
506
|
+
console.warn(`You rule "${ruleId}" depends on a GraphQL validation rule "${ruleName}" but it's not available in the "graphql-js" version you are using. Skipping...`);
|
507
|
+
return {};
|
508
|
+
}
|
509
|
+
let schema;
|
510
|
+
if (docs.requiresSchemaToExtend) {
|
511
|
+
schema = getGraphQLSchemaToExtend(context);
|
512
|
+
}
|
513
|
+
if (docs.requiresSchema) {
|
514
|
+
schema = requireGraphQLSchemaFromContext(ruleId, context);
|
515
|
+
}
|
430
516
|
return {
|
431
517
|
Document(node) {
|
432
|
-
if (!ruleFn) {
|
433
|
-
// eslint-disable-next-line no-console
|
434
|
-
console.warn(`You rule "${ruleId}" depends on a GraphQL validation rule "${ruleName}" but it's not available in the "graphql-js" version you are using. Skipping...`);
|
435
|
-
return;
|
436
|
-
}
|
437
|
-
const schema = docs.requiresSchema ? requireGraphQLSchemaFromContext(ruleId, context) : null;
|
438
518
|
const documentNode = getDocumentNode
|
439
519
|
? getDocumentNode({ ruleId, context, schema, node: node.rawNode() })
|
440
520
|
: node.rawNode();
|
441
|
-
validateDocument(node, context, schema, documentNode, ruleFn);
|
521
|
+
validateDocument(node, context, schema, documentNode, ruleFn, docs.requiresSchemaToExtend);
|
442
522
|
},
|
443
523
|
};
|
444
524
|
},
|
@@ -588,7 +668,8 @@ const GRAPHQL_JS_VALIDATIONS = Object.assign({}, validationToRule('executable-de
|
|
588
668
|
}), validationToRule('possible-type-extension', 'PossibleTypeExtensions', {
|
589
669
|
category: 'Schema',
|
590
670
|
description: `A type extension is only valid if the type is defined and has the same kind.`,
|
591
|
-
recommended: false,
|
671
|
+
recommended: false,
|
672
|
+
requiresSchemaToExtend: true,
|
592
673
|
}), validationToRule('provided-required-arguments', 'ProvidedRequiredArguments', {
|
593
674
|
category: ['Schema', 'Operations'],
|
594
675
|
description: `A field or directive is only valid if all required (non-null without a default value) field arguments have been provided.`,
|
@@ -1077,13 +1158,7 @@ const rule$2 = {
|
|
1077
1158
|
const MATCH_EXTENSION = 'MATCH_EXTENSION';
|
1078
1159
|
const MATCH_STYLE = 'MATCH_STYLE';
|
1079
1160
|
const ACCEPTED_EXTENSIONS = ['.gql', '.graphql'];
|
1080
|
-
const CASE_STYLES = [
|
1081
|
-
CaseStyle.camelCase,
|
1082
|
-
CaseStyle.pascalCase,
|
1083
|
-
CaseStyle.snakeCase,
|
1084
|
-
CaseStyle.upperCase,
|
1085
|
-
CaseStyle.kebabCase,
|
1086
|
-
];
|
1161
|
+
const CASE_STYLES = ['camelCase', 'PascalCase', 'snake_case', 'UPPER_CASE', 'kebab-case'];
|
1087
1162
|
const schemaOption = {
|
1088
1163
|
oneOf: [{ $ref: '#/definitions/asString' }, { $ref: '#/definitions/asObject' }],
|
1089
1164
|
};
|
@@ -1107,7 +1182,7 @@ const rule$3 = {
|
|
1107
1182
|
},
|
1108
1183
|
{
|
1109
1184
|
title: 'Correct',
|
1110
|
-
usage: [{ query:
|
1185
|
+
usage: [{ query: 'snake_case' }],
|
1111
1186
|
code: /* GraphQL */ `
|
1112
1187
|
# user_by_id.gql
|
1113
1188
|
query UserById {
|
@@ -1121,7 +1196,7 @@ const rule$3 = {
|
|
1121
1196
|
},
|
1122
1197
|
{
|
1123
1198
|
title: 'Correct',
|
1124
|
-
usage: [{ fragment: { style:
|
1199
|
+
usage: [{ fragment: { style: 'kebab-case', suffix: '.fragment' } }],
|
1125
1200
|
code: /* GraphQL */ `
|
1126
1201
|
# user-fields.fragment.gql
|
1127
1202
|
fragment user_fields on User {
|
@@ -1132,7 +1207,7 @@ const rule$3 = {
|
|
1132
1207
|
},
|
1133
1208
|
{
|
1134
1209
|
title: 'Correct',
|
1135
|
-
usage: [{ mutation: { style:
|
1210
|
+
usage: [{ mutation: { style: 'PascalCase', suffix: 'Mutation' } }],
|
1136
1211
|
code: /* GraphQL */ `
|
1137
1212
|
# DeleteUserMutation.gql
|
1138
1213
|
mutation DELETE_USER {
|
@@ -1152,7 +1227,7 @@ const rule$3 = {
|
|
1152
1227
|
},
|
1153
1228
|
{
|
1154
1229
|
title: 'Incorrect',
|
1155
|
-
usage: [{ query:
|
1230
|
+
usage: [{ query: 'PascalCase' }],
|
1156
1231
|
code: /* GraphQL */ `
|
1157
1232
|
# user-by-id.gql
|
1158
1233
|
query UserById {
|
@@ -1167,10 +1242,10 @@ const rule$3 = {
|
|
1167
1242
|
],
|
1168
1243
|
configOptions: [
|
1169
1244
|
{
|
1170
|
-
query:
|
1171
|
-
mutation:
|
1172
|
-
subscription:
|
1173
|
-
fragment:
|
1245
|
+
query: 'kebab-case',
|
1246
|
+
mutation: 'kebab-case',
|
1247
|
+
subscription: 'kebab-case',
|
1248
|
+
fragment: 'kebab-case',
|
1174
1249
|
},
|
1175
1250
|
],
|
1176
1251
|
},
|
@@ -1187,25 +1262,22 @@ const rule$3 = {
|
|
1187
1262
|
asObject: {
|
1188
1263
|
type: 'object',
|
1189
1264
|
additionalProperties: false,
|
1265
|
+
minProperties: 1,
|
1190
1266
|
properties: {
|
1191
|
-
style: {
|
1192
|
-
|
1193
|
-
},
|
1194
|
-
suffix: {
|
1195
|
-
type: 'string',
|
1196
|
-
},
|
1267
|
+
style: { enum: CASE_STYLES },
|
1268
|
+
suffix: { type: 'string' },
|
1197
1269
|
},
|
1198
1270
|
},
|
1199
1271
|
},
|
1200
1272
|
type: 'array',
|
1273
|
+
minItems: 1,
|
1201
1274
|
maxItems: 1,
|
1202
1275
|
items: {
|
1203
1276
|
type: 'object',
|
1204
1277
|
additionalProperties: false,
|
1278
|
+
minProperties: 1,
|
1205
1279
|
properties: {
|
1206
|
-
fileExtension: {
|
1207
|
-
enum: ACCEPTED_EXTENSIONS,
|
1208
|
-
},
|
1280
|
+
fileExtension: { enum: ACCEPTED_EXTENSIONS },
|
1209
1281
|
query: schemaOption,
|
1210
1282
|
mutation: schemaOption,
|
1211
1283
|
subscription: schemaOption,
|
@@ -1260,7 +1332,7 @@ const rule$3 = {
|
|
1260
1332
|
option = { style: option };
|
1261
1333
|
}
|
1262
1334
|
const expectedExtension = options.fileExtension || fileExtension;
|
1263
|
-
const expectedFilename = convertCase(option.style, docName) + (option.suffix || '') + expectedExtension;
|
1335
|
+
const expectedFilename = (option.style ? convertCase(option.style, docName) : filename) + (option.suffix || '') + expectedExtension;
|
1264
1336
|
const filenameWithExtension = filename + expectedExtension;
|
1265
1337
|
if (expectedFilename !== filenameWithExtension) {
|
1266
1338
|
context.report({
|
@@ -1412,6 +1484,7 @@ const rule$4 = {
|
|
1412
1484
|
],
|
1413
1485
|
},
|
1414
1486
|
},
|
1487
|
+
hasSuggestions: true,
|
1415
1488
|
schema: {
|
1416
1489
|
definitions: {
|
1417
1490
|
asString: {
|
@@ -1486,65 +1559,90 @@ const rule$4 = {
|
|
1486
1559
|
const style = restOptions[kind] || types;
|
1487
1560
|
return typeof style === 'object' ? style : { style };
|
1488
1561
|
}
|
1489
|
-
const checkNode = (selector) => (
|
1490
|
-
const { name } =
|
1491
|
-
if (!
|
1562
|
+
const checkNode = (selector) => (n) => {
|
1563
|
+
const { name: node } = n.kind === Kind.VARIABLE_DEFINITION ? n.variable : n;
|
1564
|
+
if (!node) {
|
1492
1565
|
return;
|
1493
1566
|
}
|
1494
1567
|
const { prefix, suffix, forbiddenPrefixes, forbiddenSuffixes, style } = normalisePropertyOption(selector);
|
1495
|
-
const nodeType = KindToDisplayName[
|
1496
|
-
const nodeName =
|
1497
|
-
const
|
1498
|
-
if (
|
1568
|
+
const nodeType = KindToDisplayName[n.kind] || n.kind;
|
1569
|
+
const nodeName = node.value;
|
1570
|
+
const error = getError();
|
1571
|
+
if (error) {
|
1572
|
+
const { errorMessage, renameToName } = error;
|
1573
|
+
const [leadingUnderscore] = nodeName.match(/^_*/);
|
1574
|
+
const [trailingUnderscore] = nodeName.match(/_*$/);
|
1575
|
+
const suggestedName = leadingUnderscore + renameToName + trailingUnderscore;
|
1499
1576
|
context.report({
|
1500
|
-
loc: getLocation(
|
1577
|
+
loc: getLocation(node.loc, node.value),
|
1501
1578
|
message: `${nodeType} "${nodeName}" should ${errorMessage}`,
|
1579
|
+
suggest: [
|
1580
|
+
{
|
1581
|
+
desc: `Rename to "${suggestedName}"`,
|
1582
|
+
fix: fixer => fixer.replaceText(node, suggestedName),
|
1583
|
+
},
|
1584
|
+
],
|
1502
1585
|
});
|
1503
1586
|
}
|
1504
|
-
function
|
1505
|
-
|
1506
|
-
if (allowLeadingUnderscore) {
|
1507
|
-
name = name.replace(/^_*/, '');
|
1508
|
-
}
|
1509
|
-
if (allowTrailingUnderscore) {
|
1510
|
-
name = name.replace(/_*$/, '');
|
1511
|
-
}
|
1587
|
+
function getError() {
|
1588
|
+
const name = nodeName.replace(/(^_+)|(_+$)/g, '');
|
1512
1589
|
if (prefix && !name.startsWith(prefix)) {
|
1513
|
-
return
|
1590
|
+
return {
|
1591
|
+
errorMessage: `have "${prefix}" prefix`,
|
1592
|
+
renameToName: prefix + name,
|
1593
|
+
};
|
1514
1594
|
}
|
1515
1595
|
if (suffix && !name.endsWith(suffix)) {
|
1516
|
-
return
|
1596
|
+
return {
|
1597
|
+
errorMessage: `have "${suffix}" suffix`,
|
1598
|
+
renameToName: name + suffix,
|
1599
|
+
};
|
1517
1600
|
}
|
1518
1601
|
const forbiddenPrefix = forbiddenPrefixes === null || forbiddenPrefixes === void 0 ? void 0 : forbiddenPrefixes.find(prefix => name.startsWith(prefix));
|
1519
1602
|
if (forbiddenPrefix) {
|
1520
|
-
return
|
1603
|
+
return {
|
1604
|
+
errorMessage: `not have "${forbiddenPrefix}" prefix`,
|
1605
|
+
renameToName: name.replace(new RegExp(`^${forbiddenPrefix}`), ''),
|
1606
|
+
};
|
1521
1607
|
}
|
1522
1608
|
const forbiddenSuffix = forbiddenSuffixes === null || forbiddenSuffixes === void 0 ? void 0 : forbiddenSuffixes.find(suffix => name.endsWith(suffix));
|
1523
1609
|
if (forbiddenSuffix) {
|
1524
|
-
return
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1610
|
+
return {
|
1611
|
+
errorMessage: `not have "${forbiddenSuffix}" suffix`,
|
1612
|
+
renameToName: name.replace(new RegExp(`${forbiddenSuffix}$`), ''),
|
1613
|
+
};
|
1528
1614
|
}
|
1529
1615
|
const caseRegex = StyleToRegex[style];
|
1530
1616
|
if (caseRegex && !caseRegex.test(name)) {
|
1531
|
-
return
|
1617
|
+
return {
|
1618
|
+
errorMessage: `be in ${style} format`,
|
1619
|
+
renameToName: convertCase(style, name),
|
1620
|
+
};
|
1532
1621
|
}
|
1533
1622
|
}
|
1534
1623
|
};
|
1535
|
-
const checkUnderscore = (node) => {
|
1624
|
+
const checkUnderscore = (isLeading) => (node) => {
|
1536
1625
|
const name = node.value;
|
1626
|
+
const renameToName = name.replace(new RegExp(isLeading ? '^_+' : '_+$'), '');
|
1537
1627
|
context.report({
|
1538
1628
|
loc: getLocation(node.loc, name),
|
1539
|
-
message: `${
|
1629
|
+
message: `${isLeading ? 'Leading' : 'Trailing'} underscores are not allowed`,
|
1630
|
+
suggest: [
|
1631
|
+
{
|
1632
|
+
desc: `Rename to "${renameToName}"`,
|
1633
|
+
fix: fixer => fixer.replaceText(node, renameToName),
|
1634
|
+
},
|
1635
|
+
],
|
1540
1636
|
});
|
1541
1637
|
};
|
1542
1638
|
const listeners = {};
|
1543
1639
|
if (!allowLeadingUnderscore) {
|
1544
|
-
listeners['Name[value=/^_/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'] =
|
1640
|
+
listeners['Name[value=/^_/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'] =
|
1641
|
+
checkUnderscore(true);
|
1545
1642
|
}
|
1546
1643
|
if (!allowTrailingUnderscore) {
|
1547
|
-
listeners['Name[value=/_$/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'] =
|
1644
|
+
listeners['Name[value=/_$/]:matches([parent.kind!=Field], [parent.kind=Field][parent.alias])'] =
|
1645
|
+
checkUnderscore(false);
|
1548
1646
|
}
|
1549
1647
|
const selectors = new Set([types && TYPES_KINDS, Object.keys(restOptions)].flat().filter(Boolean));
|
1550
1648
|
for (const selector of selectors) {
|
@@ -2887,9 +2985,22 @@ const rule$j = {
|
|
2887
2985
|
recommended: true,
|
2888
2986
|
},
|
2889
2987
|
messages: {
|
2890
|
-
[REQUIRE_ID_WHEN_AVAILABLE]:
|
2988
|
+
[REQUIRE_ID_WHEN_AVAILABLE]: [
|
2989
|
+
`Field {{ fieldName }} must be selected when it's available on a type. Please make sure to include it in your selection set!`,
|
2990
|
+
`If you are using fragments, make sure that all used fragments {{ checkedFragments }}specifies the field {{ fieldName }}.`,
|
2991
|
+
].join('\n'),
|
2891
2992
|
},
|
2892
2993
|
schema: {
|
2994
|
+
definitions: {
|
2995
|
+
asString: {
|
2996
|
+
type: 'string',
|
2997
|
+
},
|
2998
|
+
asArray: {
|
2999
|
+
type: 'array',
|
3000
|
+
minItems: 1,
|
3001
|
+
uniqueItems: true,
|
3002
|
+
},
|
3003
|
+
},
|
2893
3004
|
type: 'array',
|
2894
3005
|
maxItems: 1,
|
2895
3006
|
items: {
|
@@ -2897,7 +3008,7 @@ const rule$j = {
|
|
2897
3008
|
additionalProperties: false,
|
2898
3009
|
properties: {
|
2899
3010
|
fieldName: {
|
2900
|
-
|
3011
|
+
oneOf: [{ $ref: '#/definitions/asString' }, { $ref: '#/definitions/asArray' }],
|
2901
3012
|
default: DEFAULT_ID_FIELD_NAME,
|
2902
3013
|
},
|
2903
3014
|
},
|
@@ -2905,69 +3016,64 @@ const rule$j = {
|
|
2905
3016
|
},
|
2906
3017
|
},
|
2907
3018
|
create(context) {
|
3019
|
+
requireGraphQLSchemaFromContext('require-id-when-available', context);
|
3020
|
+
const siblings = requireSiblingsOperations('require-id-when-available', context);
|
3021
|
+
const { fieldName = DEFAULT_ID_FIELD_NAME } = context.options[0] || {};
|
3022
|
+
const idNames = Array.isArray(fieldName) ? fieldName : [fieldName];
|
3023
|
+
const isFound = (s) => s.kind === Kind.FIELD && idNames.includes(s.name.value);
|
2908
3024
|
return {
|
2909
3025
|
SelectionSet(node) {
|
2910
3026
|
var _a, _b;
|
2911
|
-
|
2912
|
-
|
2913
|
-
const fieldName = (context.options[0] || {}).fieldName || DEFAULT_ID_FIELD_NAME;
|
2914
|
-
if (!node.selections || node.selections.length === 0) {
|
3027
|
+
const typeInfo = node.typeInfo();
|
3028
|
+
if (!typeInfo.gqlType) {
|
2915
3029
|
return;
|
2916
3030
|
}
|
2917
|
-
const
|
2918
|
-
|
2919
|
-
|
2920
|
-
|
2921
|
-
|
2922
|
-
|
2923
|
-
|
2924
|
-
|
2925
|
-
|
2926
|
-
|
2927
|
-
|
2928
|
-
|
2929
|
-
|
2930
|
-
|
2931
|
-
|
2932
|
-
|
2933
|
-
|
2934
|
-
|
2935
|
-
|
2936
|
-
|
2937
|
-
|
2938
|
-
|
2939
|
-
|
2940
|
-
|
2941
|
-
|
2942
|
-
}
|
2943
|
-
}
|
2944
|
-
const { parent } = node;
|
2945
|
-
const hasIdFieldInInterfaceSelectionSet = parent &&
|
2946
|
-
parent.kind === 'InlineFragment' &&
|
2947
|
-
parent.parent &&
|
2948
|
-
parent.parent.kind === 'SelectionSet' &&
|
2949
|
-
parent.parent.selections.some(s => s.kind === 'Field' && s.name.value === fieldName);
|
2950
|
-
if (!found && !hasIdFieldInInterfaceSelectionSet) {
|
2951
|
-
context.report({
|
2952
|
-
loc: {
|
2953
|
-
start: {
|
2954
|
-
line: node.loc.start.line,
|
2955
|
-
column: node.loc.start.column - 1,
|
2956
|
-
},
|
2957
|
-
end: {
|
2958
|
-
line: node.loc.end.line,
|
2959
|
-
column: node.loc.end.column - 1,
|
2960
|
-
},
|
2961
|
-
},
|
2962
|
-
messageId: REQUIRE_ID_WHEN_AVAILABLE,
|
2963
|
-
data: {
|
2964
|
-
checkedFragments: checkedFragmentSpreads.size === 0 ? '' : `(${Array.from(checkedFragmentSpreads).join(', ')})`,
|
2965
|
-
fieldName,
|
2966
|
-
},
|
2967
|
-
});
|
2968
|
-
}
|
3031
|
+
const rawType = getBaseType(typeInfo.gqlType);
|
3032
|
+
const isObjectType = rawType instanceof GraphQLObjectType;
|
3033
|
+
const isInterfaceType = rawType instanceof GraphQLInterfaceType;
|
3034
|
+
if (!isObjectType && !isInterfaceType) {
|
3035
|
+
return;
|
3036
|
+
}
|
3037
|
+
const fields = rawType.getFields();
|
3038
|
+
const hasIdFieldInType = idNames.some(name => fields[name]);
|
3039
|
+
if (!hasIdFieldInType) {
|
3040
|
+
return;
|
3041
|
+
}
|
3042
|
+
const checkedFragmentSpreads = new Set();
|
3043
|
+
let found = false;
|
3044
|
+
for (const selection of node.selections) {
|
3045
|
+
if (isFound(selection)) {
|
3046
|
+
found = true;
|
3047
|
+
}
|
3048
|
+
else if (selection.kind === Kind.INLINE_FRAGMENT) {
|
3049
|
+
found = (_a = selection.selectionSet) === null || _a === void 0 ? void 0 : _a.selections.some(s => isFound(s));
|
3050
|
+
}
|
3051
|
+
else if (selection.kind === Kind.FRAGMENT_SPREAD) {
|
3052
|
+
const [foundSpread] = siblings.getFragment(selection.name.value);
|
3053
|
+
if (foundSpread) {
|
3054
|
+
checkedFragmentSpreads.add(foundSpread.document.name.value);
|
3055
|
+
found = (_b = foundSpread.document.selectionSet) === null || _b === void 0 ? void 0 : _b.selections.some(s => isFound(s));
|
2969
3056
|
}
|
2970
3057
|
}
|
3058
|
+
if (found) {
|
3059
|
+
break;
|
3060
|
+
}
|
3061
|
+
}
|
3062
|
+
const { parent } = node;
|
3063
|
+
const hasIdFieldInInterfaceSelectionSet = parent &&
|
3064
|
+
parent.kind === Kind.INLINE_FRAGMENT &&
|
3065
|
+
parent.parent &&
|
3066
|
+
parent.parent.kind === Kind.SELECTION_SET &&
|
3067
|
+
parent.parent.selections.some(s => isFound(s));
|
3068
|
+
if (!found && !hasIdFieldInInterfaceSelectionSet) {
|
3069
|
+
context.report({
|
3070
|
+
loc: getLocation(node.loc),
|
3071
|
+
messageId: REQUIRE_ID_WHEN_AVAILABLE,
|
3072
|
+
data: {
|
3073
|
+
checkedFragments: checkedFragmentSpreads.size === 0 ? '' : `(${[...checkedFragmentSpreads].join(', ')}) `,
|
3074
|
+
fieldName: idNames.map(name => `"${name}"`).join(' or '),
|
3075
|
+
},
|
3076
|
+
});
|
2971
3077
|
}
|
2972
3078
|
},
|
2973
3079
|
};
|
@@ -3059,7 +3165,7 @@ const rule$k = {
|
|
3059
3165
|
// eslint-disable-next-line no-console
|
3060
3166
|
console.warn(`Rule "selection-set-depth" works best with siblings operations loaded. For more info: http://bit.ly/graphql-eslint-operations`);
|
3061
3167
|
}
|
3062
|
-
const maxDepth = context.options[0]
|
3168
|
+
const { maxDepth } = context.options[0];
|
3063
3169
|
const ignore = context.options[0].ignore || [];
|
3064
3170
|
const checkFn = depthLimit(maxDepth, { ignore });
|
3065
3171
|
return {
|
@@ -3663,42 +3769,6 @@ function getSiblingOperations(options, gqlConfig) {
|
|
3663
3769
|
return siblingOperations;
|
3664
3770
|
}
|
3665
3771
|
|
3666
|
-
let graphqlConfig;
|
3667
|
-
function loadGraphqlConfig(options) {
|
3668
|
-
// We don't want cache config on test environment
|
3669
|
-
// Otherwise schema and documents will be same for all tests
|
3670
|
-
if (process.env.NODE_ENV !== 'test' && graphqlConfig) {
|
3671
|
-
return graphqlConfig;
|
3672
|
-
}
|
3673
|
-
const onDiskConfig = options.skipGraphQLConfig
|
3674
|
-
? null
|
3675
|
-
: loadConfigSync({
|
3676
|
-
throwOnEmpty: false,
|
3677
|
-
throwOnMissing: false,
|
3678
|
-
extensions: [addCodeFileLoaderExtension],
|
3679
|
-
});
|
3680
|
-
graphqlConfig =
|
3681
|
-
onDiskConfig ||
|
3682
|
-
new GraphQLConfig({
|
3683
|
-
config: options.projects
|
3684
|
-
? { projects: options.projects }
|
3685
|
-
: {
|
3686
|
-
schema: (options.schema || ''),
|
3687
|
-
documents: options.documents || options.operations,
|
3688
|
-
extensions: options.extensions,
|
3689
|
-
include: options.include,
|
3690
|
-
exclude: options.exclude,
|
3691
|
-
},
|
3692
|
-
filepath: 'virtual-config',
|
3693
|
-
}, [addCodeFileLoaderExtension]);
|
3694
|
-
return graphqlConfig;
|
3695
|
-
}
|
3696
|
-
const addCodeFileLoaderExtension = api => {
|
3697
|
-
api.loaders.schema.register(new CodeFileLoader());
|
3698
|
-
api.loaders.documents.register(new CodeFileLoader());
|
3699
|
-
return { name: 'graphql-eslint-loaders' };
|
3700
|
-
};
|
3701
|
-
|
3702
3772
|
let reachableTypesCache;
|
3703
3773
|
function getReachableTypes(schema) {
|
3704
3774
|
// We don't want cache reachableTypes on test environment
|
@@ -3781,7 +3851,7 @@ function parse(code, options) {
|
|
3781
3851
|
return parseForESLint(code, options).ast;
|
3782
3852
|
}
|
3783
3853
|
function parseForESLint(code, options = {}) {
|
3784
|
-
const gqlConfig =
|
3854
|
+
const gqlConfig = loadCachedGraphQLConfig(options);
|
3785
3855
|
const schema = getSchema(options, gqlConfig);
|
3786
3856
|
const parserServices = {
|
3787
3857
|
hasTypeInfo: schema !== null,
|