Skip to content

Commit 13a2d6d

Browse files
committed
Allow skipping suggestions
1 parent a1d22a2 commit 13a2d6d

7 files changed

+48
-22
lines changed

src/validation/ValidationContext.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ export class SDLValidationContext extends ASTValidationContext {
154154
this._schema = schema;
155155
}
156156

157+
get shouldProvideSuggestions() {
158+
return true;
159+
}
160+
157161
get [Symbol.toStringTag]() {
158162
return 'SDLValidationContext';
159163
}
@@ -177,24 +181,31 @@ export class ValidationContext extends ASTValidationContext {
177181
OperationDefinitionNode,
178182
ReadonlyArray<VariableUsage>
179183
>;
184+
private _shouldProvideSuggestions: boolean;
180185

181186
constructor(
182187
schema: GraphQLSchema,
183188
ast: DocumentNode,
184189
typeInfo: TypeInfo,
185190
onError: (error: GraphQLError) => void,
191+
shouldProvideSuggestions?: boolean,
186192
) {
187193
super(ast, onError);
188194
this._schema = schema;
189195
this._typeInfo = typeInfo;
190196
this._variableUsages = new Map();
191197
this._recursiveVariableUsages = new Map();
198+
this._shouldProvideSuggestions = shouldProvideSuggestions ?? true;
192199
}
193200

194201
get [Symbol.toStringTag]() {
195202
return 'ValidationContext';
196203
}
197204

205+
get shouldProvideSuggestions() {
206+
return this._shouldProvideSuggestions;
207+
}
208+
198209
getSchema(): GraphQLSchema {
199210
return this._schema;
200211
}

src/validation/rules/FieldsOnCorrectTypeRule.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ export function FieldsOnCorrectTypeRule(
4545
// First determine if there are any suggested types to condition on.
4646
let suggestion = didYouMean(
4747
'to use an inline fragment on',
48-
getSuggestedTypeNames(schema, type, fieldName),
48+
context.shouldProvideSuggestions
49+
? getSuggestedTypeNames(schema, type, fieldName)
50+
: [],
4951
);
5052

5153
// If there are no suggested types, then perhaps this was a typo?
5254
if (suggestion === '') {
53-
suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
55+
suggestion = didYouMean(
56+
context.shouldProvideSuggestions
57+
? getSuggestedFieldNames(type, fieldName)
58+
: [],
59+
);
5460
}
5561

5662
// Report an error, including helpful suggestions.

src/validation/rules/KnownArgumentNamesRule.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {
3434
);
3535
if (!varDef) {
3636
const argName = argNode.name.value;
37-
const suggestions = suggestionList(
38-
argName,
39-
Array.from(fragmentSignature.variableDefinitions.values()).map(
40-
(varSignature) => varSignature.variable.name.value,
41-
),
42-
);
37+
const suggestions = context.shouldProvideSuggestions
38+
? suggestionList(
39+
argName,
40+
Array.from(fragmentSignature.variableDefinitions.values()).map(
41+
(varSignature) => varSignature.variable.name.value,
42+
),
43+
)
44+
: [];
4345
context.reportError(
4446
new GraphQLError(
4547
`Unknown argument "${argName}" on fragment "${fragmentSignature.definition.name.value}".` +
@@ -57,10 +59,12 @@ export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {
5759

5860
if (!argDef && fieldDef && parentType) {
5961
const argName = argNode.name.value;
60-
const suggestions = suggestionList(
61-
argName,
62-
fieldDef.args.map((arg) => arg.name),
63-
);
62+
const suggestions = context.shouldProvideSuggestions
63+
? suggestionList(
64+
argName,
65+
fieldDef.args.map((arg) => arg.name),
66+
)
67+
: [];
6468
context.reportError(
6569
new GraphQLError(
6670
`Unknown argument "${argName}" on field "${parentType}.${fieldDef.name}".` +

src/validation/rules/KnownTypeNamesRule.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ export function KnownTypeNamesRule(
4848
return;
4949
}
5050

51-
const suggestedTypes = suggestionList(
52-
typeName,
53-
isSDL ? [...standardTypeNames, ...typeNames] : [...typeNames],
54-
);
51+
const suggestedTypes = context.shouldProvideSuggestions
52+
? suggestionList(
53+
typeName,
54+
isSDL ? [...standardTypeNames, ...typeNames] : [...typeNames],
55+
)
56+
: [];
5557
context.reportError(
5658
new GraphQLError(
5759
`Unknown type "${typeName}".` + didYouMean(suggestedTypes),

src/validation/rules/PossibleTypeExtensionsRule.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ export function PossibleTypeExtensionsRule(
7878
...Object.keys(schema?.getTypeMap() ?? {}),
7979
];
8080

81-
const suggestedTypes = suggestionList(typeName, allTypeNames);
81+
const suggestedTypes = context.shouldProvideSuggestions
82+
? suggestionList(typeName, allTypeNames)
83+
: [];
8284
context.reportError(
8385
new GraphQLError(
8486
`Cannot extend type "${typeName}" because it is not defined.` +

src/validation/rules/ValuesOfCorrectTypeRule.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,9 @@ export function ValuesOfCorrectTypeRule(
9797
const parentType = getNamedType(context.getParentInputType());
9898
const fieldType = context.getInputType();
9999
if (!fieldType && isInputObjectType(parentType)) {
100-
const suggestions = suggestionList(
101-
node.name.value,
102-
Object.keys(parentType.getFields()),
103-
);
100+
const suggestions = context.shouldProvideSuggestions
101+
? suggestionList(node.name.value, Object.keys(parentType.getFields()))
102+
: [];
104103
context.reportError(
105104
new GraphQLError(
106105
`Field "${node.name.value}" is not defined by type "${parentType}".` +

src/validation/validate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ export function validate(
4141
schema: GraphQLSchema,
4242
documentAST: DocumentNode,
4343
rules: ReadonlyArray<ValidationRule> = specifiedRules,
44-
options?: { maxErrors?: number },
44+
options?: { maxErrors?: number; shouldProvideSuggestions?: boolean },
4545
): ReadonlyArray<GraphQLError> {
4646
const maxErrors = options?.maxErrors ?? 100;
47+
const shouldProvideSuggestions = options?.shouldProvideSuggestions ?? true;
4748

4849
// If the schema used for validation is invalid, throw an error.
4950
assertValidSchema(schema);
@@ -63,6 +64,7 @@ export function validate(
6364
}
6465
errors.push(error);
6566
},
67+
shouldProvideSuggestions,
6668
);
6769

6870
// This uses a specialized visitor which runs multiple visitors in parallel,

0 commit comments

Comments
 (0)