Skip to content

Commit 6cc3717

Browse files
authored
use json-schema-to-ts (#1320)
* refactor * refactor description-style * refactor input-name.ts * match-document-filename and naming-convention * refactor no-root-type and relay-arguments * refactor * refactor * rename to RuleOptions * last * i missed it * pnpm i
1 parent 1f21fc8 commit 6cc3717

35 files changed

+806
-786
lines changed

.changeset/strange-donuts-warn.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
'@graphql-eslint/eslint-plugin': minor
33
---
44

5-
feat: add `lone-executable-definition` to tequire all queries, mutations, subscriptions and fragments to be located in separate files
5+
feat: add `lone-executable-definition` to require all queries, mutations, subscriptions and
6+
fragments to be located in separate files

packages/plugin/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
"@types/graphql-depth-limit": "1.1.3",
5252
"@types/json-schema": "7.0.11",
5353
"@types/lodash.lowercase": "4.3.7",
54-
"graphql": "16.6.0"
54+
"graphql": "16.6.0",
55+
"json-schema-to-ts": "2.6.2"
5556
},
5657
"publishConfig": {
5758
"directory": "dist",

packages/plugin/src/rules/alphabetize.ts

Lines changed: 59 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { GraphQLESLintRule } from '../types';
2323
import { GraphQLESTreeNode } from '../estree-converter';
2424
import { GraphQLESLintRuleListener } from '../testkit';
2525
import { ARRAY_DEFAULT_OPTIONS } from '../utils';
26+
import { FromSchema } from 'json-schema-to-ts';
2627

2728
const RULE_ID = 'alphabetize';
2829

@@ -48,16 +49,64 @@ const argumentsEnum: ('FieldDefinition' | 'Field' | 'DirectiveDefinition' | 'Dir
4849
Kind.DIRECTIVE,
4950
];
5051

51-
export type AlphabetizeConfig = {
52-
fields?: typeof fieldsEnum;
53-
values?: typeof valuesEnum;
54-
selections?: typeof selectionsEnum;
55-
variables?: typeof variablesEnum;
56-
arguments?: typeof argumentsEnum;
57-
definitions?: boolean;
58-
};
52+
const schema = {
53+
type: 'array',
54+
minItems: 1,
55+
maxItems: 1,
56+
items: {
57+
type: 'object',
58+
additionalProperties: false,
59+
minProperties: 1,
60+
properties: {
61+
fields: {
62+
...ARRAY_DEFAULT_OPTIONS,
63+
items: {
64+
enum: fieldsEnum,
65+
},
66+
description: 'Fields of `type`, `interface`, and `input`.',
67+
},
68+
values: {
69+
...ARRAY_DEFAULT_OPTIONS,
70+
items: {
71+
enum: valuesEnum,
72+
},
73+
description: 'Values of `enum`.',
74+
},
75+
selections: {
76+
...ARRAY_DEFAULT_OPTIONS,
77+
items: {
78+
enum: selectionsEnum,
79+
},
80+
description:
81+
'Selections of `fragment` and operations `query`, `mutation` and `subscription`.',
82+
},
83+
variables: {
84+
...ARRAY_DEFAULT_OPTIONS,
85+
items: {
86+
enum: variablesEnum,
87+
},
88+
description: 'Variables of operations `query`, `mutation` and `subscription`.',
89+
},
90+
arguments: {
91+
...ARRAY_DEFAULT_OPTIONS,
92+
items: {
93+
enum: argumentsEnum,
94+
},
95+
description: 'Arguments of fields and directives.',
96+
},
97+
definitions: {
98+
type: 'boolean',
99+
description:
100+
'Definitions – `type`, `interface`, `enum`, `scalar`, `input`, `union` and `directive`.',
101+
default: false,
102+
},
103+
},
104+
},
105+
} as const;
106+
107+
export type RuleOptions = FromSchema<typeof schema>;
59108

60-
export const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {
109+
export const rule: GraphQLESLintRule<RuleOptions> = {
61110
meta: {
62111
type: 'suggestion',
63112
fixable: 'code',
@@ -164,60 +213,7 @@ export const rule: GraphQLESLintRule<[AlphabetizeConfig]> = {
164213
messages: {
165214
[RULE_ID]: '`{{ currName }}` should be before {{ prevName }}.',
166215
},
167-
schema: {
168-
type: 'array',
169-
minItems: 1,
170-
maxItems: 1,
171-
items: {
172-
type: 'object',
173-
additionalProperties: false,
174-
minProperties: 1,
175-
properties: {
176-
fields: {
177-
...ARRAY_DEFAULT_OPTIONS,
178-
items: {
179-
enum: fieldsEnum,
180-
},
181-
description: 'Fields of `type`, `interface`, and `input`.',
182-
},
183-
values: {
184-
...ARRAY_DEFAULT_OPTIONS,
185-
items: {
186-
enum: valuesEnum,
187-
},
188-
description: 'Values of `enum`.',
189-
},
190-
selections: {
191-
...ARRAY_DEFAULT_OPTIONS,
192-
items: {
193-
enum: selectionsEnum,
194-
},
195-
description:
196-
'Selections of `fragment` and operations `query`, `mutation` and `subscription`.',
197-
},
198-
variables: {
199-
...ARRAY_DEFAULT_OPTIONS,
200-
items: {
201-
enum: variablesEnum,
202-
},
203-
description: 'Variables of operations `query`, `mutation` and `subscription`.',
204-
},
205-
arguments: {
206-
...ARRAY_DEFAULT_OPTIONS,
207-
items: {
208-
enum: argumentsEnum,
209-
},
210-
description: 'Arguments of fields and directives.',
211-
},
212-
definitions: {
213-
type: 'boolean',
214-
description:
215-
'Definitions – `type`, `interface`, `enum`, `scalar`, `input`, `union` and `directive`.',
216-
default: false,
217-
},
218-
},
219-
},
220-
},
216+
schema,
221217
},
222218
create(context) {
223219
const sourceCode = context.getSourceCode();

packages/plugin/src/rules/description-style.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
11
import { StringValueNode } from 'graphql';
22
import { GraphQLESLintRule } from '../types';
33
import { GraphQLESTreeNode } from '../estree-converter';
4+
import { FromSchema } from 'json-schema-to-ts';
45

5-
type DescriptionStyleRuleConfig = { style: 'inline' | 'block' };
6+
const schema = {
7+
type: 'array',
8+
maxItems: 1,
9+
items: {
10+
type: 'object',
11+
additionalProperties: false,
12+
minProperties: 1,
13+
properties: {
14+
style: {
15+
enum: ['block', 'inline'],
16+
default: 'block',
17+
},
18+
},
19+
},
20+
} as const;
621

7-
export const rule: GraphQLESLintRule<[DescriptionStyleRuleConfig]> = {
22+
export type RuleOptions = FromSchema<typeof schema>;
23+
24+
export const rule: GraphQLESLintRule<RuleOptions> = {
825
meta: {
926
type: 'suggestion',
1027
hasSuggestions: true,
@@ -36,18 +53,7 @@ export const rule: GraphQLESLintRule<[DescriptionStyleRuleConfig]> = {
3653
url: 'https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/description-style.md',
3754
recommended: true,
3855
},
39-
schema: [
40-
{
41-
type: 'object',
42-
additionalProperties: false,
43-
properties: {
44-
style: {
45-
enum: ['block', 'inline'],
46-
default: 'block',
47-
},
48-
},
49-
},
50-
],
56+
schema,
5157
},
5258
create(context) {
5359
const { style = 'block' } = context.options[0] || {};

packages/plugin/src/rules/graphql-js-validation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { AST } from 'eslint';
2-
import { JSONSchema4 } from 'json-schema';
32
import {
43
Kind,
54
DocumentNode,
@@ -21,6 +20,7 @@ import {
2120
REPORT_ON_FIRST_CHARACTER,
2221
ARRAY_DEFAULT_OPTIONS,
2322
} from '../utils';
23+
import { JSONSchema } from 'json-schema-to-ts';
2424

2525
function validateDocument({
2626
context,
@@ -162,7 +162,7 @@ const validationToRule = (
162162
ruleId: string;
163163
ruleName: string;
164164
getDocumentNode?: GetDocumentNode;
165-
schema?: JSONSchema4 | JSONSchema4[];
165+
schema?: JSONSchema | [];
166166
hasDidYouMeanSuggestions?: boolean;
167167
},
168168
docs: Omit<GraphQLESLintRule['meta']['docs'], 'url'>,

packages/plugin/src/rules/input-name.ts

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,40 @@ import {
88
import { GraphQLESLintRule } from '../types';
99
import { GraphQLESTreeNode } from '../estree-converter';
1010
import { GraphQLESLintRuleListener } from '../testkit';
11+
import { FromSchema } from 'json-schema-to-ts';
1112

12-
type InputNameRuleConfig = {
13-
checkInputType?: boolean;
14-
caseSensitiveInputType?: boolean;
15-
checkQueries?: boolean;
16-
checkMutations?: boolean;
17-
};
13+
const schema = {
14+
type: 'array',
15+
maxItems: 1,
16+
items: {
17+
type: 'object',
18+
additionalProperties: false,
19+
properties: {
20+
checkInputType: {
21+
type: 'boolean',
22+
default: false,
23+
description: 'Check that the input type name follows the convention <mutationName>Input',
24+
},
25+
caseSensitiveInputType: {
26+
type: 'boolean',
27+
default: true,
28+
description: 'Allow for case discrepancies in the input type name',
29+
},
30+
checkQueries: {
31+
type: 'boolean',
32+
default: false,
33+
description: 'Apply the rule to Queries',
34+
},
35+
checkMutations: {
36+
type: 'boolean',
37+
default: true,
38+
description: 'Apply the rule to Mutations',
39+
},
40+
},
41+
},
42+
} as const;
43+
44+
export type RuleOptions = FromSchema<typeof schema>;
1845

1946
type ObjectTypeNode = GraphQLESTreeNode<ObjectTypeDefinitionNode | ObjectTypeExtensionNode>;
2047

@@ -25,7 +52,7 @@ const isQueryType = (node: ObjectTypeNode): boolean =>
2552
const isMutationType = (node: ObjectTypeNode): boolean =>
2653
isObjectType(node) && node.name.value === 'Mutation';
2754

28-
export const rule: GraphQLESLintRule<[InputNameRuleConfig]> = {
55+
export const rule: GraphQLESLintRule<RuleOptions> = {
2956
meta: {
3057
type: 'suggestion',
3158
hasSuggestions: true,
@@ -64,38 +91,10 @@ export const rule: GraphQLESLintRule<[InputNameRuleConfig]> = {
6491
},
6592
],
6693
},
67-
schema: [
68-
{
69-
type: 'object',
70-
additionalProperties: false,
71-
properties: {
72-
checkInputType: {
73-
type: 'boolean',
74-
default: false,
75-
description:
76-
'Check that the input type name follows the convention <mutationName>Input',
77-
},
78-
caseSensitiveInputType: {
79-
type: 'boolean',
80-
default: true,
81-
description: 'Allow for case discrepancies in the input type name',
82-
},
83-
checkQueries: {
84-
type: 'boolean',
85-
default: false,
86-
description: 'Apply the rule to Queries',
87-
},
88-
checkMutations: {
89-
type: 'boolean',
90-
default: true,
91-
description: 'Apply the rule to Mutations',
92-
},
93-
},
94-
},
95-
],
94+
schema,
9695
},
9796
create(context) {
98-
const options: InputNameRuleConfig = {
97+
const options: RuleOptions[0] = {
9998
checkInputType: false,
10099
caseSensitiveInputType: true,
101100
checkQueries: false,

0 commit comments

Comments
 (0)