@graphql-eslint/eslint-plugin 3.14.4-alpha-20230112232227-647789a → 3.14.4-alpha-20230113031340-573c921

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.
Files changed (43) hide show
  1. package/cjs/rules/graphql-js-validation.js +10 -19
  2. package/cjs/rules/no-deprecated.js +1 -3
  3. package/cjs/rules/no-one-place-fragments.js +2 -4
  4. package/cjs/rules/no-root-type.js +1 -3
  5. package/cjs/rules/no-scalar-result-type-on-mutation.js +1 -3
  6. package/cjs/rules/no-unreachable-types.js +1 -3
  7. package/cjs/rules/no-unused-fields.js +3 -5
  8. package/cjs/rules/relay-arguments.js +1 -3
  9. package/cjs/rules/relay-edge-types.js +1 -3
  10. package/cjs/rules/relay-page-info.js +1 -3
  11. package/cjs/rules/require-description.js +5 -7
  12. package/cjs/rules/require-field-of-type-query-in-mutation-result.js +1 -3
  13. package/cjs/rules/require-id-when-available.js +2 -4
  14. package/cjs/rules/selection-set-depth.js +5 -2
  15. package/cjs/rules/strict-id-in-types.js +1 -3
  16. package/cjs/rules/unique-fragment-name.js +3 -5
  17. package/cjs/rules/unique-operation-name.js +1 -5
  18. package/cjs/schema.js +2 -1
  19. package/cjs/utils.js +6 -20
  20. package/esm/rules/graphql-js-validation.js +10 -19
  21. package/esm/rules/no-deprecated.js +1 -3
  22. package/esm/rules/no-one-place-fragments.js +2 -4
  23. package/esm/rules/no-root-type.js +1 -3
  24. package/esm/rules/no-scalar-result-type-on-mutation.js +1 -3
  25. package/esm/rules/no-unreachable-types.js +1 -3
  26. package/esm/rules/no-unused-fields.js +3 -5
  27. package/esm/rules/relay-arguments.js +1 -3
  28. package/esm/rules/relay-edge-types.js +1 -3
  29. package/esm/rules/relay-page-info.js +1 -3
  30. package/esm/rules/require-description.js +5 -7
  31. package/esm/rules/require-field-of-type-query-in-mutation-result.js +1 -3
  32. package/esm/rules/require-id-when-available.js +2 -4
  33. package/esm/rules/selection-set-depth.js +5 -2
  34. package/esm/rules/strict-id-in-types.js +1 -3
  35. package/esm/rules/unique-fragment-name.js +3 -5
  36. package/esm/rules/unique-operation-name.js +1 -5
  37. package/esm/schema.js +2 -1
  38. package/esm/utils.js +6 -20
  39. package/package.json +1 -1
  40. package/typings/rules/unique-fragment-name.d.cts +1 -2
  41. package/typings/rules/unique-fragment-name.d.ts +1 -2
  42. package/typings/utils.d.cts +2 -2
  43. package/typings/utils.d.ts +2 -2
@@ -71,12 +71,10 @@ const getMissingFragments = (node) => {
71
71
  const { fragmentDefs, fragmentSpreads } = getFragmentDefsAndFragmentSpreads(node);
72
72
  return [...fragmentSpreads].filter(name => !fragmentDefs.has(name));
73
73
  };
74
- const handleMissingFragments = ({ context, node }) => {
74
+ const handleMissingFragments = ({ ruleId, context, node }) => {
75
75
  const missingFragments = getMissingFragments(node);
76
76
  if (missingFragments.length > 0) {
77
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
78
- if (!siblings)
79
- return null;
77
+ const siblings = (0, utils_js_1.requireSiblingsOperations)(ruleId, context);
80
78
  const fragmentsToAdd = [];
81
79
  for (const fragmentName of missingFragments) {
82
80
  const [foundFragment] = siblings.getFragment(fragmentName).map(source => source.document);
@@ -87,6 +85,7 @@ const handleMissingFragments = ({ context, node }) => {
87
85
  if (fragmentsToAdd.length > 0) {
88
86
  // recall fn to make sure to add fragments inside fragments
89
87
  return handleMissingFragments({
88
+ ruleId,
90
89
  context,
91
90
  node: {
92
91
  kind: graphql_1.Kind.DOCUMENT,
@@ -130,18 +129,12 @@ const validationToRule = ({ ruleId, ruleName, getDocumentNode, schema = [], hasD
130
129
  }
131
130
  return {
132
131
  Document(node) {
133
- let schema = null;
134
- if (docs.requiresSchema) {
135
- schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
136
- if (!schema)
137
- return;
138
- }
139
- const rawNode = node.rawNode();
132
+ const schema = docs.requiresSchema
133
+ ? (0, utils_js_1.requireGraphQLSchemaFromContext)(ruleId, context)
134
+ : null;
140
135
  const documentNode = getDocumentNode
141
- ? getDocumentNode({ context, node: rawNode })
142
- : rawNode;
143
- if (!documentNode)
144
- return;
136
+ ? getDocumentNode({ ruleId, context, node: node.rawNode() })
137
+ : node.rawNode();
145
138
  validateDocument({
146
139
  context,
147
140
  schema,
@@ -329,10 +322,8 @@ exports.GRAPHQL_JS_VALIDATIONS = Object.assign({}, validationToRule({
329
322
  }), validationToRule({
330
323
  ruleId: 'no-unused-fragments',
331
324
  ruleName: 'NoUnusedFragments',
332
- getDocumentNode: ({ context, node }) => {
333
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
334
- if (!siblings)
335
- return null;
325
+ getDocumentNode: ({ ruleId, context, node }) => {
326
+ const siblings = (0, utils_js_1.requireSiblingsOperations)(ruleId, context);
336
327
  const FilePathToDocumentsMap = [
337
328
  ...siblings.getOperations(),
338
329
  ...siblings.getFragments(),
@@ -83,9 +83,7 @@ exports.rule = {
83
83
  schema: [],
84
84
  },
85
85
  create(context) {
86
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
87
- if (!schema)
88
- return {};
86
+ (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
89
87
  function report(node, reason) {
90
88
  const nodeName = node.kind === graphql_1.Kind.ENUM ? node.value : node.name.value;
91
89
  const nodeType = node.kind === graphql_1.Kind.ENUM ? 'enum value' : 'field';
@@ -53,10 +53,8 @@ exports.rule = {
53
53
  schema: [],
54
54
  },
55
55
  create(context) {
56
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
57
- if (!siblings)
58
- return {};
59
- const allDocuments = [...siblings.getOperations(), ...siblings.getFragments()];
56
+ const operations = (0, utils_js_1.requireSiblingsOperations)(RULE_ID, context);
57
+ const allDocuments = [...operations.getOperations(), ...operations.getFragments()];
60
58
  const usedFragmentsMap = Object.create(null);
61
59
  for (const { document, filePath } of allDocuments) {
62
60
  const relativeFilePath = (0, path_1.relative)(utils_js_1.CWD, filePath);
@@ -54,9 +54,7 @@ exports.rule = {
54
54
  schema,
55
55
  },
56
56
  create(context) {
57
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
58
- if (!schema)
59
- return {};
57
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)('no-root-type', context);
60
58
  const disallow = new Set(context.options[0].disallow);
61
59
  const rootTypeNames = [
62
60
  disallow.has('mutation') && schema.getMutationType(),
@@ -35,9 +35,7 @@ exports.rule = {
35
35
  schema: [],
36
36
  },
37
37
  create(context) {
38
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
39
- if (!schema)
40
- return {};
38
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
41
39
  const mutationType = schema.getMutationType();
42
40
  if (!mutationType) {
43
41
  return {};
@@ -130,9 +130,7 @@ exports.rule = {
130
130
  hasSuggestions: true,
131
131
  },
132
132
  create(context) {
133
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
134
- if (!schema)
135
- return {};
133
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
136
134
  const reachableTypes = getReachableTypes(schema);
137
135
  return {
138
136
  [`:matches(${KINDS}) > .name`](node) {
@@ -95,11 +95,9 @@ exports.rule = {
95
95
  hasSuggestions: true,
96
96
  },
97
97
  create(context) {
98
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
99
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
100
- if (!schema || !siblings)
101
- return {};
102
- const usedFields = getUsedFields(schema, siblings);
98
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
99
+ const siblingsOperations = (0, utils_js_1.requireSiblingsOperations)(RULE_ID, context);
100
+ const usedFields = getUsedFields(schema, siblingsOperations);
103
101
  return {
104
102
  FieldDefinition(node) {
105
103
  var _a;
@@ -68,9 +68,7 @@ exports.rule = {
68
68
  schema,
69
69
  },
70
70
  create(context) {
71
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
72
- if (!schema)
73
- return {};
71
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
74
72
  const { includeBoth = true } = context.options[0] || {};
75
73
  return {
76
74
  'FieldDefinition > .gqlType Name[value=/Connection$/]'(node) {
@@ -106,9 +106,7 @@ exports.rule = {
106
106
  schema,
107
107
  },
108
108
  create(context) {
109
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
110
- if (!schema)
111
- return {};
109
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
112
110
  const edgeTypes = getEdgeTypes(schema);
113
111
  const options = {
114
112
  withEdgeSuffix: true,
@@ -45,9 +45,7 @@ exports.rule = {
45
45
  schema: [],
46
46
  },
47
47
  create(context) {
48
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
49
- if (!schema)
50
- return {};
48
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
51
49
  if (process.env.NODE_ENV === 'test' || !hasPageInfoChecked) {
52
50
  const pageInfoType = schema.getType('PageInfo');
53
51
  if (!pageInfoType) {
@@ -152,13 +152,11 @@ exports.rule = {
152
152
  }
153
153
  }
154
154
  if (rootField) {
155
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
156
- if (schema) {
157
- const rootTypeNames = (0, utils_1.getRootTypeNames)(schema);
158
- kinds.add(`:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[
159
- ...rootTypeNames,
160
- ].join(',')})$/] > FieldDefinition`);
161
- }
155
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
156
+ const rootTypeNames = (0, utils_1.getRootTypeNames)(schema);
157
+ kinds.add(`:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[
158
+ ...rootTypeNames,
159
+ ].join(',')})$/] > FieldDefinition`);
162
160
  }
163
161
  const selector = [...kinds].join(',');
164
162
  return {
@@ -45,9 +45,7 @@ exports.rule = {
45
45
  schema: [],
46
46
  },
47
47
  create(context) {
48
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
49
- if (!schema)
50
- return {};
48
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
51
49
  const mutationType = schema.getMutationType();
52
50
  const queryType = schema.getQueryType();
53
51
  if (!mutationType || !queryType) {
@@ -89,10 +89,8 @@ exports.rule = {
89
89
  schema,
90
90
  },
91
91
  create(context) {
92
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
93
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
94
- if (!schema || !siblings)
95
- return {};
92
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
93
+ const siblings = (0, utils_js_1.requireSiblingsOperations)(RULE_ID, context);
96
94
  const { fieldName = DEFAULT_ID_FIELD_NAME } = context.options[0] || {};
97
95
  const idNames = (0, utils_1.asArray)(fieldName);
98
96
  // Check selections only in OperationDefinition,
@@ -78,8 +78,11 @@ exports.rule = {
78
78
  schema,
79
79
  },
80
80
  create(context) {
81
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context, false);
82
- if (!siblings) {
81
+ let siblings = null;
82
+ try {
83
+ siblings = (0, utils_js_1.requireSiblingsOperations)(RULE_ID, context);
84
+ }
85
+ catch (_a) {
83
86
  utils_js_1.logger.warn(`Rule "${RULE_ID}" works best with siblings operations loaded. For more info: https://bit.ly/graphql-eslint-operations`);
84
87
  }
85
88
  const { maxDepth, ignore = [] } = context.options[0];
@@ -116,9 +116,7 @@ exports.rule = {
116
116
  exceptions: {},
117
117
  ...context.options[0],
118
118
  };
119
- const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(context);
120
- if (!schema)
121
- return {};
119
+ const schema = (0, utils_js_1.requireGraphQLSchemaFromContext)(RULE_ID, context);
122
120
  const rootTypeNames = [
123
121
  schema.getQueryType(),
124
122
  schema.getMutationType(),
@@ -5,8 +5,9 @@ const path_1 = require("path");
5
5
  const graphql_1 = require("graphql");
6
6
  const utils_js_1 = require("../utils.js");
7
7
  const RULE_ID = 'unique-fragment-name';
8
- const checkNode = (context, node, ruleId, siblings) => {
8
+ const checkNode = (context, node, ruleId) => {
9
9
  const documentName = node.name.value;
10
+ const siblings = (0, utils_js_1.requireSiblingsOperations)(ruleId, context);
10
11
  const siblingDocuments = node.kind === graphql_1.Kind.FRAGMENT_DEFINITION
11
12
  ? siblings.getFragment(documentName)
12
13
  : siblings.getOperation(documentName);
@@ -81,12 +82,9 @@ exports.rule = {
81
82
  schema: [],
82
83
  },
83
84
  create(context) {
84
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
85
- if (!siblings)
86
- return {};
87
85
  return {
88
86
  FragmentDefinition(node) {
89
- (0, exports.checkNode)(context, node, RULE_ID, siblings);
87
+ (0, exports.checkNode)(context, node, RULE_ID);
90
88
  },
91
89
  };
92
90
  },
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.rule = void 0;
4
- const utils_js_1 = require("../utils.js");
5
4
  const unique_fragment_name_js_1 = require("./unique-fragment-name.js");
6
5
  const RULE_ID = 'unique-operation-name';
7
6
  exports.rule = {
@@ -57,12 +56,9 @@ exports.rule = {
57
56
  schema: [],
58
57
  },
59
58
  create(context) {
60
- const siblings = (0, utils_js_1.requireSiblingsOperations)(context);
61
- if (!siblings)
62
- return {};
63
59
  return {
64
60
  'OperationDefinition[name!=undefined]'(node) {
65
- (0, unique_fragment_name_js_1.checkNode)(context, node, RULE_ID, siblings);
61
+ (0, unique_fragment_name_js_1.checkNode)(context, node, RULE_ID);
66
62
  },
67
63
  };
68
64
  },
package/cjs/schema.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getSchema = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const chalk_1 = tslib_1.__importDefault(require("chalk"));
5
6
  const debug_1 = tslib_1.__importDefault(require("debug"));
6
7
  const fast_glob_1 = tslib_1.__importDefault(require("fast-glob"));
7
8
  const graphql_1 = require("graphql");
@@ -34,7 +35,7 @@ function getSchema(project, schemaOptions) {
34
35
  }
35
36
  catch (error) {
36
37
  if (error instanceof Error) {
37
- error.message = `Error while loading schema: ${error.message}`;
38
+ error.message = chalk_1.default.red(`Error while loading schema: ${error.message}`);
38
39
  }
39
40
  schema = error;
40
41
  }
package/cjs/utils.js CHANGED
@@ -5,35 +5,21 @@ const tslib_1 = require("tslib");
5
5
  const chalk_1 = tslib_1.__importDefault(require("chalk"));
6
6
  const graphql_1 = require("graphql");
7
7
  const lodash_lowercase_1 = tslib_1.__importDefault(require("lodash.lowercase"));
8
- function requireSiblingsOperations(context, shouldReport = true) {
8
+ function requireSiblingsOperations(ruleId, context) {
9
9
  const { siblingOperations } = context.parserServices;
10
10
  if (!siblingOperations.available) {
11
- if (shouldReport) {
12
- context.report({
13
- loc: exports.REPORT_ON_FIRST_CHARACTER,
14
- message: 'Rule requires `parserOptions.operations` to be set and loaded. See https://bit.ly/graphql-eslint-operations for more info',
15
- });
16
- }
17
- return null;
11
+ throw new Error(`Rule \`${ruleId}\` requires \`parserOptions.operations\` to be set and loaded. See https://bit.ly/graphql-eslint-operations for more info`);
18
12
  }
19
13
  return siblingOperations;
20
14
  }
21
15
  exports.requireSiblingsOperations = requireSiblingsOperations;
22
- function requireGraphQLSchemaFromContext(context) {
16
+ function requireGraphQLSchemaFromContext(ruleId, context) {
23
17
  const { schema } = context.parserServices;
24
18
  if (!schema) {
25
- context.report({
26
- loc: exports.REPORT_ON_FIRST_CHARACTER,
27
- message: 'Rule requires `parserOptions.schema` to be set and loaded. See https://bit.ly/graphql-eslint-schema for more info',
28
- });
29
- return null;
19
+ throw new Error(`Rule \`${ruleId}\` requires \`parserOptions.schema\` to be set and loaded. See https://bit.ly/graphql-eslint-schema for more info`);
30
20
  }
31
- if (schema instanceof Error) {
32
- context.report({
33
- loc: exports.REPORT_ON_FIRST_CHARACTER,
34
- message: schema.message,
35
- });
36
- return null;
21
+ else if (schema instanceof Error) {
22
+ throw schema;
37
23
  }
38
24
  return schema;
39
25
  }
@@ -70,12 +70,10 @@ const getMissingFragments = (node) => {
70
70
  const { fragmentDefs, fragmentSpreads } = getFragmentDefsAndFragmentSpreads(node);
71
71
  return [...fragmentSpreads].filter(name => !fragmentDefs.has(name));
72
72
  };
73
- const handleMissingFragments = ({ context, node }) => {
73
+ const handleMissingFragments = ({ ruleId, context, node }) => {
74
74
  const missingFragments = getMissingFragments(node);
75
75
  if (missingFragments.length > 0) {
76
- const siblings = requireSiblingsOperations(context);
77
- if (!siblings)
78
- return null;
76
+ const siblings = requireSiblingsOperations(ruleId, context);
79
77
  const fragmentsToAdd = [];
80
78
  for (const fragmentName of missingFragments) {
81
79
  const [foundFragment] = siblings.getFragment(fragmentName).map(source => source.document);
@@ -86,6 +84,7 @@ const handleMissingFragments = ({ context, node }) => {
86
84
  if (fragmentsToAdd.length > 0) {
87
85
  // recall fn to make sure to add fragments inside fragments
88
86
  return handleMissingFragments({
87
+ ruleId,
89
88
  context,
90
89
  node: {
91
90
  kind: Kind.DOCUMENT,
@@ -129,18 +128,12 @@ const validationToRule = ({ ruleId, ruleName, getDocumentNode, schema = [], hasD
129
128
  }
130
129
  return {
131
130
  Document(node) {
132
- let schema = null;
133
- if (docs.requiresSchema) {
134
- schema = requireGraphQLSchemaFromContext(context);
135
- if (!schema)
136
- return;
137
- }
138
- const rawNode = node.rawNode();
131
+ const schema = docs.requiresSchema
132
+ ? requireGraphQLSchemaFromContext(ruleId, context)
133
+ : null;
139
134
  const documentNode = getDocumentNode
140
- ? getDocumentNode({ context, node: rawNode })
141
- : rawNode;
142
- if (!documentNode)
143
- return;
135
+ ? getDocumentNode({ ruleId, context, node: node.rawNode() })
136
+ : node.rawNode();
144
137
  validateDocument({
145
138
  context,
146
139
  schema,
@@ -328,10 +321,8 @@ export const GRAPHQL_JS_VALIDATIONS = Object.assign({}, validationToRule({
328
321
  }), validationToRule({
329
322
  ruleId: 'no-unused-fragments',
330
323
  ruleName: 'NoUnusedFragments',
331
- getDocumentNode: ({ context, node }) => {
332
- const siblings = requireSiblingsOperations(context);
333
- if (!siblings)
334
- return null;
324
+ getDocumentNode: ({ ruleId, context, node }) => {
325
+ const siblings = requireSiblingsOperations(ruleId, context);
335
326
  const FilePathToDocumentsMap = [
336
327
  ...siblings.getOperations(),
337
328
  ...siblings.getFragments(),
@@ -80,9 +80,7 @@ export const rule = {
80
80
  schema: [],
81
81
  },
82
82
  create(context) {
83
- const schema = requireGraphQLSchemaFromContext(context);
84
- if (!schema)
85
- return {};
83
+ requireGraphQLSchemaFromContext(RULE_ID, context);
86
84
  function report(node, reason) {
87
85
  const nodeName = node.kind === Kind.ENUM ? node.value : node.name.value;
88
86
  const nodeType = node.kind === Kind.ENUM ? 'enum value' : 'field';
@@ -50,10 +50,8 @@ export const rule = {
50
50
  schema: [],
51
51
  },
52
52
  create(context) {
53
- const siblings = requireSiblingsOperations(context);
54
- if (!siblings)
55
- return {};
56
- const allDocuments = [...siblings.getOperations(), ...siblings.getFragments()];
53
+ const operations = requireSiblingsOperations(RULE_ID, context);
54
+ const allDocuments = [...operations.getOperations(), ...operations.getFragments()];
57
55
  const usedFragmentsMap = Object.create(null);
58
56
  for (const { document, filePath } of allDocuments) {
59
57
  const relativeFilePath = relative(CWD, filePath);
@@ -51,9 +51,7 @@ export const rule = {
51
51
  schema,
52
52
  },
53
53
  create(context) {
54
- const schema = requireGraphQLSchemaFromContext(context);
55
- if (!schema)
56
- return {};
54
+ const schema = requireGraphQLSchemaFromContext('no-root-type', context);
57
55
  const disallow = new Set(context.options[0].disallow);
58
56
  const rootTypeNames = [
59
57
  disallow.has('mutation') && schema.getMutationType(),
@@ -32,9 +32,7 @@ export const rule = {
32
32
  schema: [],
33
33
  },
34
34
  create(context) {
35
- const schema = requireGraphQLSchemaFromContext(context);
36
- if (!schema)
37
- return {};
35
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
38
36
  const mutationType = schema.getMutationType();
39
37
  if (!mutationType) {
40
38
  return {};
@@ -126,9 +126,7 @@ export const rule = {
126
126
  hasSuggestions: true,
127
127
  },
128
128
  create(context) {
129
- const schema = requireGraphQLSchemaFromContext(context);
130
- if (!schema)
131
- return {};
129
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
132
130
  const reachableTypes = getReachableTypes(schema);
133
131
  return {
134
132
  [`:matches(${KINDS}) > .name`](node) {
@@ -92,11 +92,9 @@ export const rule = {
92
92
  hasSuggestions: true,
93
93
  },
94
94
  create(context) {
95
- const schema = requireGraphQLSchemaFromContext(context);
96
- const siblings = requireSiblingsOperations(context);
97
- if (!schema || !siblings)
98
- return {};
99
- const usedFields = getUsedFields(schema, siblings);
95
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
96
+ const siblingsOperations = requireSiblingsOperations(RULE_ID, context);
97
+ const usedFields = getUsedFields(schema, siblingsOperations);
100
98
  return {
101
99
  FieldDefinition(node) {
102
100
  var _a;
@@ -65,9 +65,7 @@ export const rule = {
65
65
  schema,
66
66
  },
67
67
  create(context) {
68
- const schema = requireGraphQLSchemaFromContext(context);
69
- if (!schema)
70
- return {};
68
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
71
69
  const { includeBoth = true } = context.options[0] || {};
72
70
  return {
73
71
  'FieldDefinition > .gqlType Name[value=/Connection$/]'(node) {
@@ -103,9 +103,7 @@ export const rule = {
103
103
  schema,
104
104
  },
105
105
  create(context) {
106
- const schema = requireGraphQLSchemaFromContext(context);
107
- if (!schema)
108
- return {};
106
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
109
107
  const edgeTypes = getEdgeTypes(schema);
110
108
  const options = {
111
109
  withEdgeSuffix: true,
@@ -42,9 +42,7 @@ export const rule = {
42
42
  schema: [],
43
43
  },
44
44
  create(context) {
45
- const schema = requireGraphQLSchemaFromContext(context);
46
- if (!schema)
47
- return {};
45
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
48
46
  if (process.env.NODE_ENV === 'test' || !hasPageInfoChecked) {
49
47
  const pageInfoType = schema.getType('PageInfo');
50
48
  if (!pageInfoType) {
@@ -149,13 +149,11 @@ export const rule = {
149
149
  }
150
150
  }
151
151
  if (rootField) {
152
- const schema = requireGraphQLSchemaFromContext(context);
153
- if (schema) {
154
- const rootTypeNames = getRootTypeNames(schema);
155
- kinds.add(`:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[
156
- ...rootTypeNames,
157
- ].join(',')})$/] > FieldDefinition`);
158
- }
152
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
153
+ const rootTypeNames = getRootTypeNames(schema);
154
+ kinds.add(`:matches(ObjectTypeDefinition, ObjectTypeExtension)[name.value=/^(${[
155
+ ...rootTypeNames,
156
+ ].join(',')})$/] > FieldDefinition`);
159
157
  }
160
158
  const selector = [...kinds].join(',');
161
159
  return {
@@ -42,9 +42,7 @@ export const rule = {
42
42
  schema: [],
43
43
  },
44
44
  create(context) {
45
- const schema = requireGraphQLSchemaFromContext(context);
46
- if (!schema)
47
- return {};
45
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
48
46
  const mutationType = schema.getMutationType();
49
47
  const queryType = schema.getQueryType();
50
48
  if (!mutationType || !queryType) {
@@ -86,10 +86,8 @@ export const rule = {
86
86
  schema,
87
87
  },
88
88
  create(context) {
89
- const schema = requireGraphQLSchemaFromContext(context);
90
- const siblings = requireSiblingsOperations(context);
91
- if (!schema || !siblings)
92
- return {};
89
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
90
+ const siblings = requireSiblingsOperations(RULE_ID, context);
93
91
  const { fieldName = DEFAULT_ID_FIELD_NAME } = context.options[0] || {};
94
92
  const idNames = asArray(fieldName);
95
93
  // Check selections only in OperationDefinition,
@@ -74,8 +74,11 @@ export const rule = {
74
74
  schema,
75
75
  },
76
76
  create(context) {
77
- const siblings = requireSiblingsOperations(context, false);
78
- if (!siblings) {
77
+ let siblings = null;
78
+ try {
79
+ siblings = requireSiblingsOperations(RULE_ID, context);
80
+ }
81
+ catch (_a) {
79
82
  logger.warn(`Rule "${RULE_ID}" works best with siblings operations loaded. For more info: https://bit.ly/graphql-eslint-operations`);
80
83
  }
81
84
  const { maxDepth, ignore = [] } = context.options[0];
@@ -113,9 +113,7 @@ export const rule = {
113
113
  exceptions: {},
114
114
  ...context.options[0],
115
115
  };
116
- const schema = requireGraphQLSchemaFromContext(context);
117
- if (!schema)
118
- return {};
116
+ const schema = requireGraphQLSchemaFromContext(RULE_ID, context);
119
117
  const rootTypeNames = [
120
118
  schema.getQueryType(),
121
119
  schema.getMutationType(),
@@ -2,8 +2,9 @@ import { relative } from 'path';
2
2
  import { Kind } from 'graphql';
3
3
  import { CWD, normalizePath, requireSiblingsOperations, VIRTUAL_DOCUMENT_REGEX } from '../utils.js';
4
4
  const RULE_ID = 'unique-fragment-name';
5
- export const checkNode = (context, node, ruleId, siblings) => {
5
+ export const checkNode = (context, node, ruleId) => {
6
6
  const documentName = node.name.value;
7
+ const siblings = requireSiblingsOperations(ruleId, context);
7
8
  const siblingDocuments = node.kind === Kind.FRAGMENT_DEFINITION
8
9
  ? siblings.getFragment(documentName)
9
10
  : siblings.getOperation(documentName);
@@ -77,12 +78,9 @@ export const rule = {
77
78
  schema: [],
78
79
  },
79
80
  create(context) {
80
- const siblings = requireSiblingsOperations(context);
81
- if (!siblings)
82
- return {};
83
81
  return {
84
82
  FragmentDefinition(node) {
85
- checkNode(context, node, RULE_ID, siblings);
83
+ checkNode(context, node, RULE_ID);
86
84
  },
87
85
  };
88
86
  },
@@ -1,4 +1,3 @@
1
- import { requireSiblingsOperations } from '../utils.js';
2
1
  import { checkNode } from './unique-fragment-name.js';
3
2
  const RULE_ID = 'unique-operation-name';
4
3
  export const rule = {
@@ -54,12 +53,9 @@ export const rule = {
54
53
  schema: [],
55
54
  },
56
55
  create(context) {
57
- const siblings = requireSiblingsOperations(context);
58
- if (!siblings)
59
- return {};
60
56
  return {
61
57
  'OperationDefinition[name!=undefined]'(node) {
62
- checkNode(context, node, RULE_ID, siblings);
58
+ checkNode(context, node, RULE_ID);
63
59
  },
64
60
  };
65
61
  },
package/esm/schema.js CHANGED
@@ -1,3 +1,4 @@
1
+ import chalk from 'chalk';
1
2
  import debugFactory from 'debug';
2
3
  import fg from 'fast-glob';
3
4
  import { GraphQLSchema } from 'graphql';
@@ -30,7 +31,7 @@ export function getSchema(project, schemaOptions) {
30
31
  }
31
32
  catch (error) {
32
33
  if (error instanceof Error) {
33
- error.message = `Error while loading schema: ${error.message}`;
34
+ error.message = chalk.red(`Error while loading schema: ${error.message}`);
34
35
  }
35
36
  schema = error;
36
37
  }
package/esm/utils.js CHANGED
@@ -1,34 +1,20 @@
1
1
  import chalk from 'chalk';
2
2
  import { Kind } from 'graphql';
3
3
  import lowerCase from 'lodash.lowercase';
4
- export function requireSiblingsOperations(context, shouldReport = true) {
4
+ export function requireSiblingsOperations(ruleId, context) {
5
5
  const { siblingOperations } = context.parserServices;
6
6
  if (!siblingOperations.available) {
7
- if (shouldReport) {
8
- context.report({
9
- loc: REPORT_ON_FIRST_CHARACTER,
10
- message: 'Rule requires `parserOptions.operations` to be set and loaded. See https://bit.ly/graphql-eslint-operations for more info',
11
- });
12
- }
13
- return null;
7
+ throw new Error(`Rule \`${ruleId}\` requires \`parserOptions.operations\` to be set and loaded. See https://bit.ly/graphql-eslint-operations for more info`);
14
8
  }
15
9
  return siblingOperations;
16
10
  }
17
- export function requireGraphQLSchemaFromContext(context) {
11
+ export function requireGraphQLSchemaFromContext(ruleId, context) {
18
12
  const { schema } = context.parserServices;
19
13
  if (!schema) {
20
- context.report({
21
- loc: REPORT_ON_FIRST_CHARACTER,
22
- message: 'Rule requires `parserOptions.schema` to be set and loaded. See https://bit.ly/graphql-eslint-schema for more info',
23
- });
24
- return null;
14
+ throw new Error(`Rule \`${ruleId}\` requires \`parserOptions.schema\` to be set and loaded. See https://bit.ly/graphql-eslint-schema for more info`);
25
15
  }
26
- if (schema instanceof Error) {
27
- context.report({
28
- loc: REPORT_ON_FIRST_CHARACTER,
29
- message: schema.message,
30
- });
31
- return null;
16
+ else if (schema instanceof Error) {
17
+ throw schema;
32
18
  }
33
19
  return schema;
34
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-eslint/eslint-plugin",
3
- "version": "3.14.4-alpha-20230112232227-647789a",
3
+ "version": "3.14.4-alpha-20230113031340-573c921",
4
4
  "description": "GraphQL plugin for ESLint",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -1,6 +1,5 @@
1
1
  import { ExecutableDefinitionNode } from 'graphql';
2
2
  import { GraphQLESTreeNode } from '../estree-converter/index.cjs';
3
- import { SiblingOperations } from '../siblings.cjs';
4
3
  import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types.cjs';
5
- export declare const checkNode: (context: GraphQLESLintRuleContext, node: GraphQLESTreeNode<ExecutableDefinitionNode>, ruleId: string, siblings: SiblingOperations) => void;
4
+ export declare const checkNode: (context: GraphQLESLintRuleContext, node: GraphQLESTreeNode<ExecutableDefinitionNode>, ruleId: string) => void;
6
5
  export declare const rule: GraphQLESLintRule;
@@ -1,6 +1,5 @@
1
1
  import { ExecutableDefinitionNode } from 'graphql';
2
2
  import { GraphQLESTreeNode } from '../estree-converter/index.js';
3
- import { SiblingOperations } from '../siblings.js';
4
3
  import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types.js';
5
- export declare const checkNode: (context: GraphQLESLintRuleContext, node: GraphQLESTreeNode<ExecutableDefinitionNode>, ruleId: string, siblings: SiblingOperations) => void;
4
+ export declare const checkNode: (context: GraphQLESLintRuleContext, node: GraphQLESTreeNode<ExecutableDefinitionNode>, ruleId: string) => void;
6
5
  export declare const rule: GraphQLESLintRule;
@@ -3,8 +3,8 @@ import { Position } from 'estree';
3
3
  import { ASTNode, GraphQLSchema, Kind } from 'graphql';
4
4
  import { SiblingOperations } from './siblings.cjs';
5
5
  import { GraphQLESLintRuleContext } from './types.cjs';
6
- export declare function requireSiblingsOperations(context: GraphQLESLintRuleContext, shouldReport?: boolean): SiblingOperations | null;
7
- export declare function requireGraphQLSchemaFromContext(context: GraphQLESLintRuleContext): GraphQLSchema | null;
6
+ export declare function requireSiblingsOperations(ruleId: string, context: GraphQLESLintRuleContext): SiblingOperations | never;
7
+ export declare function requireGraphQLSchemaFromContext(ruleId: string, context: GraphQLESLintRuleContext): GraphQLSchema | never;
8
8
  export declare const logger: {
9
9
  error: (...args: unknown[]) => void;
10
10
  warn: (...args: unknown[]) => void;
@@ -3,8 +3,8 @@ import { Position } from 'estree';
3
3
  import { ASTNode, GraphQLSchema, Kind } from 'graphql';
4
4
  import { SiblingOperations } from './siblings.js';
5
5
  import { GraphQLESLintRuleContext } from './types.js';
6
- export declare function requireSiblingsOperations(context: GraphQLESLintRuleContext, shouldReport?: boolean): SiblingOperations | null;
7
- export declare function requireGraphQLSchemaFromContext(context: GraphQLESLintRuleContext): GraphQLSchema | null;
6
+ export declare function requireSiblingsOperations(ruleId: string, context: GraphQLESLintRuleContext): SiblingOperations | never;
7
+ export declare function requireGraphQLSchemaFromContext(ruleId: string, context: GraphQLESLintRuleContext): GraphQLSchema | never;
8
8
  export declare const logger: {
9
9
  error: (...args: unknown[]) => void;
10
10
  warn: (...args: unknown[]) => void;