@graphql-eslint/eslint-plugin 3.5.0 → 3.7.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.
@@ -4,7 +4,7 @@
4
4
 
5
5
  - Category: `Schema`
6
6
  - Rule name: `@graphql-eslint/strict-id-in-types`
7
- - Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
7
+ - Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
8
8
  - Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
9
9
 
10
10
  Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.
package/index.js CHANGED
@@ -3157,28 +3157,26 @@ const rule$k = {
3157
3157
  },
3158
3158
  };
3159
3159
 
3160
- const shouldIgnoreNode = ({ node, exceptions }) => {
3161
- const rawNode = node.rawNode();
3162
- if (exceptions.types && exceptions.types.includes(rawNode.name.value)) {
3163
- return true;
3164
- }
3165
- if (exceptions.suffixes && exceptions.suffixes.some(suffix => rawNode.name.value.endsWith(suffix))) {
3166
- return true;
3167
- }
3168
- return false;
3169
- };
3160
+ const RULE_ID$4 = 'strict-id-in-types';
3170
3161
  const rule$l = {
3171
3162
  meta: {
3172
3163
  type: 'suggestion',
3173
3164
  docs: {
3174
- description: 'Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.',
3165
+ description: `Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.`,
3175
3166
  category: 'Schema',
3176
3167
  recommended: true,
3177
- url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/strict-id-in-types.md',
3168
+ url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/${RULE_ID$4}.md`,
3169
+ requiresSchema: true,
3178
3170
  examples: [
3179
3171
  {
3180
3172
  title: 'Incorrect',
3181
- usage: [{ acceptedIdNames: ['id', '_id'], acceptedIdTypes: ['ID'], exceptions: { suffixes: ['Payload'] } }],
3173
+ usage: [
3174
+ {
3175
+ acceptedIdNames: ['id', '_id'],
3176
+ acceptedIdTypes: ['ID'],
3177
+ exceptions: { suffixes: ['Payload'] },
3178
+ },
3179
+ ],
3182
3180
  code: /* GraphQL */ `
3183
3181
  # Incorrect field name
3184
3182
  type InvalidFieldName {
@@ -3280,6 +3278,9 @@ const rule$l = {
3280
3278
  },
3281
3279
  },
3282
3280
  },
3281
+ messages: {
3282
+ [RULE_ID$4]: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }}; Accepted type(s): {{ acceptedTypesString }}.`,
3283
+ },
3283
3284
  },
3284
3285
  create(context) {
3285
3286
  const options = {
@@ -3288,9 +3289,18 @@ const rule$l = {
3288
3289
  exceptions: {},
3289
3290
  ...context.options[0],
3290
3291
  };
3292
+ const schema = requireGraphQLSchemaFromContext(RULE_ID$4, context);
3293
+ const rootTypeNames = [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
3294
+ .filter(Boolean)
3295
+ .map(type => type.name);
3296
+ const selector = `ObjectTypeDefinition[name.value!=/^(${rootTypeNames.join('|')})$/]`;
3291
3297
  return {
3292
- ObjectTypeDefinition(node) {
3293
- if (shouldIgnoreNode({ node, exceptions: options.exceptions })) {
3298
+ [selector](node) {
3299
+ var _a, _b;
3300
+ const typeName = node.name.value;
3301
+ const shouldIgnoreNode = ((_a = options.exceptions.types) === null || _a === void 0 ? void 0 : _a.includes(typeName)) ||
3302
+ ((_b = options.exceptions.suffixes) === null || _b === void 0 ? void 0 : _b.some(suffix => typeName.endsWith(suffix)));
3303
+ if (shouldIgnoreNode) {
3294
3304
  return;
3295
3305
  }
3296
3306
  const validIds = node.fields.filter(field => {
@@ -3303,18 +3313,17 @@ const rule$l = {
3303
3313
  }
3304
3314
  return isValidIdName && isValidIdType;
3305
3315
  });
3306
- const typeName = node.name.value;
3307
3316
  // Usually, there should be only one unique identifier field per type.
3308
3317
  // Some clients allow multiple fields to be used. If more people need this,
3309
3318
  // we can extend this rule later.
3310
3319
  if (validIds.length !== 1) {
3311
3320
  context.report({
3312
3321
  loc: getLocation(node.name.loc, typeName),
3313
- message: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }} ; Accepted type(s): {{ acceptedTypesString }}`,
3322
+ messageId: RULE_ID$4,
3314
3323
  data: {
3315
3324
  typeName,
3316
- acceptedNamesString: options.acceptedIdNames.join(','),
3317
- acceptedTypesString: options.acceptedIdTypes.join(','),
3325
+ acceptedNamesString: options.acceptedIdNames.join(', '),
3326
+ acceptedTypesString: options.acceptedIdTypes.join(', '),
3318
3327
  },
3319
3328
  });
3320
3329
  }
@@ -3740,6 +3749,8 @@ function loadGraphQLConfig(options) {
3740
3749
  const onDiskConfig = options.skipGraphQLConfig
3741
3750
  ? null
3742
3751
  : graphqlConfig.loadConfigSync({
3752
+ // load config relative to the file being linted
3753
+ rootDir: options.filePath ? path.dirname(options.filePath) : undefined,
3743
3754
  throwOnEmpty: false,
3744
3755
  throwOnMissing: false,
3745
3756
  extensions: [addCodeFileLoaderExtension],
package/index.mjs CHANGED
@@ -3151,28 +3151,26 @@ const rule$k = {
3151
3151
  },
3152
3152
  };
3153
3153
 
3154
- const shouldIgnoreNode = ({ node, exceptions }) => {
3155
- const rawNode = node.rawNode();
3156
- if (exceptions.types && exceptions.types.includes(rawNode.name.value)) {
3157
- return true;
3158
- }
3159
- if (exceptions.suffixes && exceptions.suffixes.some(suffix => rawNode.name.value.endsWith(suffix))) {
3160
- return true;
3161
- }
3162
- return false;
3163
- };
3154
+ const RULE_ID$4 = 'strict-id-in-types';
3164
3155
  const rule$l = {
3165
3156
  meta: {
3166
3157
  type: 'suggestion',
3167
3158
  docs: {
3168
- description: 'Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.',
3159
+ description: `Requires output types to have one unique identifier unless they do not have a logical one. Exceptions can be used to ignore output types that do not have unique identifiers.`,
3169
3160
  category: 'Schema',
3170
3161
  recommended: true,
3171
- url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/strict-id-in-types.md',
3162
+ url: `https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/${RULE_ID$4}.md`,
3163
+ requiresSchema: true,
3172
3164
  examples: [
3173
3165
  {
3174
3166
  title: 'Incorrect',
3175
- usage: [{ acceptedIdNames: ['id', '_id'], acceptedIdTypes: ['ID'], exceptions: { suffixes: ['Payload'] } }],
3167
+ usage: [
3168
+ {
3169
+ acceptedIdNames: ['id', '_id'],
3170
+ acceptedIdTypes: ['ID'],
3171
+ exceptions: { suffixes: ['Payload'] },
3172
+ },
3173
+ ],
3176
3174
  code: /* GraphQL */ `
3177
3175
  # Incorrect field name
3178
3176
  type InvalidFieldName {
@@ -3274,6 +3272,9 @@ const rule$l = {
3274
3272
  },
3275
3273
  },
3276
3274
  },
3275
+ messages: {
3276
+ [RULE_ID$4]: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }}; Accepted type(s): {{ acceptedTypesString }}.`,
3277
+ },
3277
3278
  },
3278
3279
  create(context) {
3279
3280
  const options = {
@@ -3282,9 +3283,18 @@ const rule$l = {
3282
3283
  exceptions: {},
3283
3284
  ...context.options[0],
3284
3285
  };
3286
+ const schema = requireGraphQLSchemaFromContext(RULE_ID$4, context);
3287
+ const rootTypeNames = [schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
3288
+ .filter(Boolean)
3289
+ .map(type => type.name);
3290
+ const selector = `ObjectTypeDefinition[name.value!=/^(${rootTypeNames.join('|')})$/]`;
3285
3291
  return {
3286
- ObjectTypeDefinition(node) {
3287
- if (shouldIgnoreNode({ node, exceptions: options.exceptions })) {
3292
+ [selector](node) {
3293
+ var _a, _b;
3294
+ const typeName = node.name.value;
3295
+ const shouldIgnoreNode = ((_a = options.exceptions.types) === null || _a === void 0 ? void 0 : _a.includes(typeName)) ||
3296
+ ((_b = options.exceptions.suffixes) === null || _b === void 0 ? void 0 : _b.some(suffix => typeName.endsWith(suffix)));
3297
+ if (shouldIgnoreNode) {
3288
3298
  return;
3289
3299
  }
3290
3300
  const validIds = node.fields.filter(field => {
@@ -3297,18 +3307,17 @@ const rule$l = {
3297
3307
  }
3298
3308
  return isValidIdName && isValidIdType;
3299
3309
  });
3300
- const typeName = node.name.value;
3301
3310
  // Usually, there should be only one unique identifier field per type.
3302
3311
  // Some clients allow multiple fields to be used. If more people need this,
3303
3312
  // we can extend this rule later.
3304
3313
  if (validIds.length !== 1) {
3305
3314
  context.report({
3306
3315
  loc: getLocation(node.name.loc, typeName),
3307
- message: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }} ; Accepted type(s): {{ acceptedTypesString }}`,
3316
+ messageId: RULE_ID$4,
3308
3317
  data: {
3309
3318
  typeName,
3310
- acceptedNamesString: options.acceptedIdNames.join(','),
3311
- acceptedTypesString: options.acceptedIdTypes.join(','),
3319
+ acceptedNamesString: options.acceptedIdNames.join(', '),
3320
+ acceptedTypesString: options.acceptedIdTypes.join(', '),
3312
3321
  },
3313
3322
  });
3314
3323
  }
@@ -3734,6 +3743,8 @@ function loadGraphQLConfig(options) {
3734
3743
  const onDiskConfig = options.skipGraphQLConfig
3735
3744
  ? null
3736
3745
  : loadConfigSync({
3746
+ // load config relative to the file being linted
3747
+ rootDir: options.filePath ? dirname(options.filePath) : undefined,
3737
3748
  throwOnEmpty: false,
3738
3749
  throwOnMissing: false,
3739
3750
  extensions: [addCodeFileLoaderExtension],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-eslint/eslint-plugin",
3
- "version": "3.5.0",
3
+ "version": "3.7.0",
4
4
  "description": "GraphQL plugin for ESLint",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -9,8 +9,8 @@
9
9
  "dependencies": {
10
10
  "@babel/code-frame": "7.16.7",
11
11
  "@graphql-tools/code-file-loader": "7.2.3",
12
- "@graphql-tools/graphql-tag-pluck": "7.1.4",
13
- "@graphql-tools/utils": "8.5.5",
12
+ "@graphql-tools/graphql-tag-pluck": "7.1.5",
13
+ "@graphql-tools/utils": "8.6.1",
14
14
  "chalk": "4.1.2",
15
15
  "graphql-config": "4.1.0",
16
16
  "graphql-depth-limit": "1.1.0",
package/rules/index.d.ts CHANGED
@@ -53,11 +53,7 @@ export declare const rules: {
53
53
  maxDepth: number;
54
54
  ignore?: string[];
55
55
  }], false>;
56
- 'strict-id-in-types': import("..").GraphQLESLintRule<[{
57
- acceptedIdNames?: string[];
58
- acceptedIdTypes?: string[];
59
- exceptions?: import("./strict-id-in-types").ExceptionRule;
60
- }], false>;
56
+ 'strict-id-in-types': import("..").GraphQLESLintRule<[import("./strict-id-in-types").StrictIdInTypesRuleConfig], false>;
61
57
  'unique-fragment-name': import("..").GraphQLESLintRule<any[], false>;
62
58
  'unique-operation-name': import("..").GraphQLESLintRule<any[], false>;
63
59
  };
@@ -1,12 +1,11 @@
1
1
  import { GraphQLESLintRule } from '../types';
2
- export interface ExceptionRule {
3
- types?: string[];
4
- suffixes?: string[];
5
- }
6
- declare type StrictIdInTypesRuleConfig = {
2
+ export declare type StrictIdInTypesRuleConfig = {
7
3
  acceptedIdNames?: string[];
8
4
  acceptedIdTypes?: string[];
9
- exceptions?: ExceptionRule;
5
+ exceptions?: {
6
+ types?: string[];
7
+ suffixes?: string[];
8
+ };
10
9
  };
11
10
  declare const rule: GraphQLESLintRule<[StrictIdInTypesRuleConfig]>;
12
11
  export default rule;