Skip to content

Commit 5ff184b

Browse files
Dimitri POSTOLOVdimitridotansimha
authored
test for lone-schema-definition rule (#407)
* feat: add tests for `lone-schema-definition` rule * Fix lone-schema-definition * update rule docs Co-authored-by: dimitri <[email protected]> Co-authored-by: Dotan Simha <[email protected]>
1 parent ef8d776 commit 5ff184b

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

.changeset/ninety-deers-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
Fix issues with `lone-schema-definition` rule

docs/rules/lone-schema-definition.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
- Category: `Validation`
44
- Rule name: `@graphql-eslint/lone-schema-definition`
5-
- Requires GraphQL Schema: `true` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
5+
- Requires GraphQL Schema: `false` [ℹ️](../../README.md#extended-linting-rules-with-graphql-schema)
66
- Requires GraphQL Operations: `false` [ℹ️](../../README.md#extended-linting-rules-with-siblings-operations)
77

88
A GraphQL document is only valid if it contains only one schema definition.

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,16 @@ export const GRAPHQL_JS_VALIDATIONS = Object.assign(
9999
description: `A GraphQL document is only valid if when it contains an anonymous operation (the query short-hand) that it contains only that one operation definition.`,
100100
},
101101
}),
102-
validationToRule('lone-schema-definition', 'LoneSchemaDefinition', {
103-
docs: {
104-
description: `A GraphQL document is only valid if it contains only one schema definition.`,
102+
validationToRule(
103+
'lone-schema-definition',
104+
'LoneSchemaDefinition',
105+
{
106+
docs: {
107+
description: `A GraphQL document is only valid if it contains only one schema definition.`,
108+
},
105109
},
106-
}),
110+
true
111+
),
107112
validationToRule('no-fragment-cycles', 'NoFragmentCycles', {
108113
docs: {
109114
description: `A GraphQL fragment is only valid when it does not have cycles in fragments usage.`,
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { GraphQLRuleTester } from '../src/testkit';
2+
import { GRAPHQL_JS_VALIDATIONS } from '../src/rules/graphql-js-validation';
3+
import { ParserOptions } from '../src/types';
4+
5+
const TEST_SCHEMA = /* GraphQL */`
6+
type Query {
7+
foo: String
8+
}
9+
10+
type Mutation {
11+
bar: Boolean
12+
}
13+
14+
schema {
15+
query: Query
16+
mutation: Mutation
17+
}
18+
`;
19+
20+
const WITH_SCHEMA = {
21+
parserOptions: <ParserOptions>{
22+
schema: TEST_SCHEMA,
23+
operations: [],
24+
},
25+
};
26+
27+
const ruleTester = new GraphQLRuleTester();
28+
29+
ruleTester.runGraphQLTests('lone-schema-definition', GRAPHQL_JS_VALIDATIONS['lone-schema-definition'], {
30+
valid: [{ ...WITH_SCHEMA, code: TEST_SCHEMA }],
31+
invalid: [
32+
{
33+
...WITH_SCHEMA,
34+
code: /* GraphQL */`
35+
type Query {
36+
foo: String
37+
}
38+
39+
type Mutation {
40+
bar: Boolean
41+
}
42+
43+
schema {
44+
query: Query
45+
mutation: Mutation
46+
}
47+
48+
type RootQuery {
49+
foo: String
50+
}
51+
52+
type RootMutation {
53+
bar: Boolean
54+
}
55+
56+
schema {
57+
query: RootQuery
58+
mutation: RootMutation
59+
}
60+
`,
61+
errors: [
62+
{ message: 'Must provide only one schema definition.' },
63+
],
64+
},
65+
],
66+
});

0 commit comments

Comments
 (0)