Skip to content

Commit a819bfc

Browse files
phryneasyaacovCR
andcommitted
fix(validation): incorrect validation errors when variable descriptions are used (graphql#4517)
I initially reported this as an LSP error (graphql/graphiql#4138), but turns out it's an error in `graphql-js`. <img width="1742" height="564" alt="image" src="https://github.com/user-attachments/assets/eca78ebc-e022-4fa0-b423-8169c9c902c4"/> --------- Co-authored-by: Yaacov Rydzinski <yaacovCR@gmail.com>
1 parent 83854f8 commit a819bfc

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/validation/__tests__/validation-test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,25 @@ describe('Validate: Limit maximum number of validation errors', () => {
144144
).to.throw(/^Error from custom rule!$/);
145145
});
146146
});
147+
148+
describe('operation and variable definition descriptions', () => {
149+
it('validates operation with description and variable descriptions', () => {
150+
const schema = buildSchema(
151+
'type Query { field(a: Int, b: String): String }',
152+
);
153+
const query = `
154+
"Operation description"
155+
query myQuery(
156+
"Variable a description"
157+
$a: Int,
158+
"""Variable b\nmultiline description"""
159+
$b: String
160+
) {
161+
field(a: $a, b: $b)
162+
}
163+
`;
164+
const ast = parse(query);
165+
const errors = validate(schema, ast);
166+
expect(errors.length).to.equal(0);
167+
});
168+
});

src/validation/validate.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { mapValue } from '../jsutils/mapValue.js';
12
import type { Maybe } from '../jsutils/Maybe.js';
23

34
import { GraphQLError } from '../error/GraphQLError.js';
45

56
import type { DocumentNode } from '../language/ast.js';
7+
import { QueryDocumentKeys } from '../language/ast.js';
68
import { visit, visitInParallel } from '../language/visitor.js';
79

810
import type { GraphQLSchema } from '../type/schema.js';
@@ -22,6 +24,13 @@ export interface ValidationOptions {
2224
hideSuggestions?: Maybe<boolean>;
2325
}
2426

27+
// Per the specification, descriptions must not affect validation.
28+
// See https://spec.graphql.org/draft/#sec-Descriptions
29+
const QueryDocumentKeysToValidate = mapValue(
30+
QueryDocumentKeys,
31+
(keys: ReadonlyArray<string>) => keys.filter((key) => key !== 'description'),
32+
);
33+
2534
/**
2635
* Implements the "Validation" section of the spec.
2736
*
@@ -78,7 +87,11 @@ export function validate(
7887

7988
// Visit the whole document with each instance of all provided rules.
8089
try {
81-
visit(documentAST, visitWithTypeInfo(typeInfo, visitor));
90+
visit(
91+
documentAST,
92+
visitWithTypeInfo(typeInfo, visitor),
93+
QueryDocumentKeysToValidate,
94+
);
8295
} catch (e: unknown) {
8396
if (e === abortError) {
8497
errors.push(abortError);

0 commit comments

Comments
 (0)