@graphql-eslint/eslint-plugin 3.18.1-alpha-20230522133807-c37ed49 → 3.19.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,6 +19,7 @@ exports.default = {
19
19
  '@graphql-eslint/require-deprecation-date': 'error',
20
20
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': 'error',
21
21
  '@graphql-eslint/require-nullable-fields-with-oneof': 'error',
22
+ '@graphql-eslint/require-nullable-result-in-root': 'error',
22
23
  '@graphql-eslint/require-type-pattern-with-oneof': 'error',
23
24
  },
24
25
  };
@@ -33,6 +33,7 @@ const require_field_of_type_query_in_mutation_result_js_1 = require("./require-f
33
33
  const require_id_when_available_js_1 = require("./require-id-when-available.js");
34
34
  const require_import_fragment_js_1 = require("./require-import-fragment.js");
35
35
  const require_nullable_fields_with_oneof_js_1 = require("./require-nullable-fields-with-oneof.js");
36
+ const require_nullable_result_in_root_js_1 = require("./require-nullable-result-in-root.js");
36
37
  const require_type_pattern_with_oneof_js_1 = require("./require-type-pattern-with-oneof.js");
37
38
  const selection_set_depth_js_1 = require("./selection-set-depth.js");
38
39
  const strict_id_in_types_js_1 = require("./strict-id-in-types.js");
@@ -68,6 +69,7 @@ exports.rules = {
68
69
  'require-id-when-available': require_id_when_available_js_1.rule,
69
70
  'require-import-fragment': require_import_fragment_js_1.rule,
70
71
  'require-nullable-fields-with-oneof': require_nullable_fields_with_oneof_js_1.rule,
72
+ 'require-nullable-result-in-root': require_nullable_result_in_root_js_1.rule,
71
73
  'require-type-pattern-with-oneof': require_type_pattern_with_oneof_js_1.rule,
72
74
  'selection-set-depth': selection_set_depth_js_1.rule,
73
75
  'strict-id-in-types': strict_id_in_types_js_1.rule,
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.rule = void 0;
4
+ const graphql_1 = require("graphql");
5
+ const utils_js_1 = require("../utils.js");
6
+ const RULE_ID = 'require-nullable-result-in-root';
7
+ exports.rule = {
8
+ meta: {
9
+ type: 'suggestion',
10
+ hasSuggestions: true,
11
+ docs: {
12
+ category: 'Schema',
13
+ description: 'Require nullable fields in root types.',
14
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
15
+ requiresSchema: true,
16
+ examples: [
17
+ {
18
+ title: 'Incorrect',
19
+ code: /* GraphQL */ `
20
+ type Query {
21
+ user: User!
22
+ }
23
+ `,
24
+ },
25
+ {
26
+ title: 'Correct',
27
+ code: /* GraphQL */ `
28
+ type Query {
29
+ foo: User
30
+ baz: [User]!
31
+ bar: [User!]!
32
+ }
33
+ `,
34
+ },
35
+ ],
36
+ },
37
+ messages: {
38
+ [RULE_ID]: 'Unexpected non-null result {{ resultType }} in {{ rootType }}',
39
+ },
40
+ schema: [],
41
+ },
42
+ create(context) {
43
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
44
+ const rootTypeNames = new Set([schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
45
+ .filter(utils_js_1.truthy)
46
+ .map(type => type.name));
47
+ const sourceCode = context.getSourceCode();
48
+ return {
49
+ 'ObjectTypeDefinition,ObjectTypeExtension'(node) {
50
+ if (!rootTypeNames.has(node.name.value))
51
+ return;
52
+ for (const field of node.fields || []) {
53
+ if (field.gqlType.type !== graphql_1.Kind.NON_NULL_TYPE ||
54
+ field.gqlType.gqlType.type !== graphql_1.Kind.NAMED_TYPE)
55
+ continue;
56
+ const name = field.gqlType.gqlType.name.value;
57
+ const type = schema.getType(name);
58
+ const resultType = type ? (0, utils_js_1.getNodeName)(type.astNode) : '';
59
+ context.report({
60
+ node: field.gqlType,
61
+ messageId: RULE_ID,
62
+ data: {
63
+ resultType,
64
+ rootType: (0, utils_js_1.getNodeName)(node),
65
+ },
66
+ suggest: [
67
+ {
68
+ desc: `Make ${resultType} nullable`,
69
+ fix(fixer) {
70
+ const text = sourceCode.getText(field.gqlType);
71
+ return fixer.replaceText(field.gqlType, text.replace('!', ''));
72
+ },
73
+ },
74
+ ],
75
+ });
76
+ }
77
+ },
78
+ };
79
+ },
80
+ };
package/cjs/utils.js CHANGED
@@ -127,6 +127,7 @@ exports.displayNodeName = displayNodeName;
127
127
  function getNodeName(node) {
128
128
  switch (node.kind) {
129
129
  case graphql_1.Kind.OBJECT_TYPE_DEFINITION:
130
+ case graphql_1.Kind.OBJECT_TYPE_EXTENSION:
130
131
  case graphql_1.Kind.INTERFACE_TYPE_DEFINITION:
131
132
  case graphql_1.Kind.ENUM_TYPE_DEFINITION:
132
133
  case graphql_1.Kind.SCALAR_TYPE_DEFINITION:
@@ -17,6 +17,7 @@ export default {
17
17
  '@graphql-eslint/require-deprecation-date': 'error',
18
18
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': 'error',
19
19
  '@graphql-eslint/require-nullable-fields-with-oneof': 'error',
20
+ '@graphql-eslint/require-nullable-result-in-root': 'error',
20
21
  '@graphql-eslint/require-type-pattern-with-oneof': 'error',
21
22
  },
22
23
  };
@@ -30,6 +30,7 @@ import { rule as requireFieldOfTypeQueryInMutationResult } from './require-field
30
30
  import { rule as requireIdWhenAvailable } from './require-id-when-available.js';
31
31
  import { rule as requireImportFragment } from './require-import-fragment.js';
32
32
  import { rule as requireNullableFieldsWithOneof } from './require-nullable-fields-with-oneof.js';
33
+ import { rule as requireNullableResultInRoot } from './require-nullable-result-in-root.js';
33
34
  import { rule as requireTypePatternWithOneof } from './require-type-pattern-with-oneof.js';
34
35
  import { rule as selectionSetDepth } from './selection-set-depth.js';
35
36
  import { rule as strictIdInTypes } from './strict-id-in-types.js';
@@ -65,6 +66,7 @@ export const rules = {
65
66
  'require-id-when-available': requireIdWhenAvailable,
66
67
  'require-import-fragment': requireImportFragment,
67
68
  'require-nullable-fields-with-oneof': requireNullableFieldsWithOneof,
69
+ 'require-nullable-result-in-root': requireNullableResultInRoot,
68
70
  'require-type-pattern-with-oneof': requireTypePatternWithOneof,
69
71
  'selection-set-depth': selectionSetDepth,
70
72
  'strict-id-in-types': strictIdInTypes,
@@ -0,0 +1,77 @@
1
+ import { Kind } from 'graphql';
2
+ import { getNodeName, requireGraphQLSchemaFromContext, truthy } from '../utils.js';
3
+ const RULE_ID = 'require-nullable-result-in-root';
4
+ export const rule = {
5
+ meta: {
6
+ type: 'suggestion',
7
+ hasSuggestions: true,
8
+ docs: {
9
+ category: 'Schema',
10
+ description: 'Require nullable fields in root types.',
11
+ url: `https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/${RULE_ID}.md`,
12
+ requiresSchema: true,
13
+ examples: [
14
+ {
15
+ title: 'Incorrect',
16
+ code: /* GraphQL */ `
17
+ type Query {
18
+ user: User!
19
+ }
20
+ `,
21
+ },
22
+ {
23
+ title: 'Correct',
24
+ code: /* GraphQL */ `
25
+ type Query {
26
+ foo: User
27
+ baz: [User]!
28
+ bar: [User!]!
29
+ }
30
+ `,
31
+ },
32
+ ],
33
+ },
34
+ messages: {
35
+ [RULE_ID]: 'Unexpected non-null result {{ resultType }} in {{ rootType }}',
36
+ },
37
+ schema: [],
38
+ },
39
+ create(context) {
40
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
41
+ const rootTypeNames = new Set([schema.getQueryType(), schema.getMutationType(), schema.getSubscriptionType()]
42
+ .filter(truthy)
43
+ .map(type => type.name));
44
+ const sourceCode = context.getSourceCode();
45
+ return {
46
+ 'ObjectTypeDefinition,ObjectTypeExtension'(node) {
47
+ if (!rootTypeNames.has(node.name.value))
48
+ return;
49
+ for (const field of node.fields || []) {
50
+ if (field.gqlType.type !== Kind.NON_NULL_TYPE ||
51
+ field.gqlType.gqlType.type !== Kind.NAMED_TYPE)
52
+ continue;
53
+ const name = field.gqlType.gqlType.name.value;
54
+ const type = schema.getType(name);
55
+ const resultType = type ? getNodeName(type.astNode) : '';
56
+ context.report({
57
+ node: field.gqlType,
58
+ messageId: RULE_ID,
59
+ data: {
60
+ resultType,
61
+ rootType: getNodeName(node),
62
+ },
63
+ suggest: [
64
+ {
65
+ desc: `Make ${resultType} nullable`,
66
+ fix(fixer) {
67
+ const text = sourceCode.getText(field.gqlType);
68
+ return fixer.replaceText(field.gqlType, text.replace('!', ''));
69
+ },
70
+ },
71
+ ],
72
+ });
73
+ }
74
+ },
75
+ };
76
+ },
77
+ };
package/esm/utils.js CHANGED
@@ -112,6 +112,7 @@ export function displayNodeName(node) {
112
112
  export function getNodeName(node) {
113
113
  switch (node.kind) {
114
114
  case Kind.OBJECT_TYPE_DEFINITION:
115
+ case Kind.OBJECT_TYPE_EXTENSION:
115
116
  case Kind.INTERFACE_TYPE_DEFINITION:
116
117
  case Kind.ENUM_TYPE_DEFINITION:
117
118
  case Kind.SCALAR_TYPE_DEFINITION:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-eslint/eslint-plugin",
3
- "version": "3.18.1-alpha-20230522133807-c37ed49",
3
+ "version": "3.19.0",
4
4
  "description": "GraphQL plugin for ESLint",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -14,7 +14,7 @@
14
14
  "chalk": "^4.1.2",
15
15
  "debug": "^4.3.4",
16
16
  "fast-glob": "^3.2.12",
17
- "graphql-config": "^5.0.0",
17
+ "graphql-config": "^4.4.0",
18
18
  "graphql-depth-limit": "^1.1.0",
19
19
  "lodash.lowercase": "^4.3.0",
20
20
  "tslib": "^2.4.1"
@@ -59,6 +59,7 @@ export declare const configs: {
59
59
  '@graphql-eslint/require-deprecation-date': string;
60
60
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': string;
61
61
  '@graphql-eslint/require-nullable-fields-with-oneof': string;
62
+ '@graphql-eslint/require-nullable-result-in-root': string;
62
63
  '@graphql-eslint/require-type-pattern-with-oneof': string;
63
64
  };
64
65
  };
@@ -59,6 +59,7 @@ export declare const configs: {
59
59
  '@graphql-eslint/require-deprecation-date': string;
60
60
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': string;
61
61
  '@graphql-eslint/require-nullable-fields-with-oneof': string;
62
+ '@graphql-eslint/require-nullable-result-in-root': string;
62
63
  '@graphql-eslint/require-type-pattern-with-oneof': string;
63
64
  };
64
65
  };
@@ -11,6 +11,7 @@ declare const _default: {
11
11
  '@graphql-eslint/require-deprecation-date': string;
12
12
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': string;
13
13
  '@graphql-eslint/require-nullable-fields-with-oneof': string;
14
+ '@graphql-eslint/require-nullable-result-in-root': string;
14
15
  '@graphql-eslint/require-type-pattern-with-oneof': string;
15
16
  };
16
17
  };
@@ -11,6 +11,7 @@ declare const _default: {
11
11
  '@graphql-eslint/require-deprecation-date': string;
12
12
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': string;
13
13
  '@graphql-eslint/require-nullable-fields-with-oneof': string;
14
+ '@graphql-eslint/require-nullable-result-in-root': string;
14
15
  '@graphql-eslint/require-type-pattern-with-oneof': string;
15
16
  };
16
17
  };
@@ -151,6 +151,7 @@ export declare const flatConfigs: {
151
151
  '@graphql-eslint/require-deprecation-date': string;
152
152
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': string;
153
153
  '@graphql-eslint/require-nullable-fields-with-oneof': string;
154
+ '@graphql-eslint/require-nullable-result-in-root': string;
154
155
  '@graphql-eslint/require-type-pattern-with-oneof': string;
155
156
  '@graphql-eslint/description-style': string;
156
157
  '@graphql-eslint/known-argument-names': string;
@@ -151,6 +151,7 @@ export declare const flatConfigs: {
151
151
  '@graphql-eslint/require-deprecation-date': string;
152
152
  '@graphql-eslint/require-field-of-type-query-in-mutation-result': string;
153
153
  '@graphql-eslint/require-nullable-fields-with-oneof': string;
154
+ '@graphql-eslint/require-nullable-result-in-root': string;
154
155
  '@graphql-eslint/require-type-pattern-with-oneof': string;
155
156
  '@graphql-eslint/description-style': string;
156
157
  '@graphql-eslint/known-argument-names': string;
@@ -92,6 +92,7 @@ export declare const rules: {
92
92
  }[], true>;
93
93
  'require-import-fragment': import("../types.js").GraphQLESLintRule;
94
94
  'require-nullable-fields-with-oneof': import("../types.js").GraphQLESLintRule;
95
+ 'require-nullable-result-in-root': import("../types.js").GraphQLESLintRule;
95
96
  'require-type-pattern-with-oneof': import("../types.js").GraphQLESLintRule;
96
97
  'selection-set-depth': import("../types.js").GraphQLESLintRule<{
97
98
  ignore?: string[] | undefined;
@@ -92,6 +92,7 @@ export declare const rules: {
92
92
  }[], true>;
93
93
  'require-import-fragment': import("../types.js").GraphQLESLintRule;
94
94
  'require-nullable-fields-with-oneof': import("../types.js").GraphQLESLintRule;
95
+ 'require-nullable-result-in-root': import("../types.js").GraphQLESLintRule;
95
96
  'require-type-pattern-with-oneof': import("../types.js").GraphQLESLintRule;
96
97
  'selection-set-depth': import("../types.js").GraphQLESLintRule<{
97
98
  ignore?: string[] | undefined;
@@ -0,0 +1,2 @@
1
+ import { GraphQLESLintRule } from '../types.cjs';
2
+ export declare const rule: GraphQLESLintRule;
@@ -0,0 +1,2 @@
1
+ import { GraphQLESLintRule } from '../types.js';
2
+ export declare const rule: GraphQLESLintRule;