Skip to content

Commit 176d50b

Browse files
committed
feat: disable 'did you mean x' during validation
1 parent f597c69 commit 176d50b

6 files changed

+48
-20
lines changed

src/validation/ValidationContext.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ export class SDLValidationContext extends ASTValidationContext {
162162
export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;
163163

164164
export class ValidationContext extends ASTValidationContext {
165+
public readonly didYouMean: boolean;
166+
165167
private _schema: GraphQLSchema;
166168
private _typeInfo: TypeInfo;
167169
private _variableUsages: Map<
@@ -179,12 +181,15 @@ export class ValidationContext extends ASTValidationContext {
179181
ast: DocumentNode,
180182
typeInfo: TypeInfo,
181183
onError: (error: GraphQLError) => void,
184+
/** Whether the "did you mean x" suggestions should be enabled. */
185+
didYouMean?: boolean,
182186
) {
183187
super(ast, onError);
184188
this._schema = schema;
185189
this._typeInfo = typeInfo;
186190
this._variableUsages = new Map();
187191
this._recursiveVariableUsages = new Map();
192+
this.didYouMean = didYouMean == null ? true : didYouMean;
188193
}
189194

190195
get [Symbol.toStringTag]() {

src/validation/rules/FieldsOnCorrectTypeRule.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,19 @@ export function FieldsOnCorrectTypeRule(
4242
const schema = context.getSchema();
4343
const fieldName = node.name.value;
4444

45-
// First determine if there are any suggested types to condition on.
46-
let suggestion = didYouMean(
47-
'to use an inline fragment on',
48-
getSuggestedTypeNames(schema, type, fieldName),
49-
);
50-
51-
// If there are no suggested types, then perhaps this was a typo?
52-
if (suggestion === '') {
53-
suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
45+
let suggestion = '';
46+
47+
if (context.didYouMean) {
48+
// First determine if there are any suggested types to condition on.
49+
suggestion = didYouMean(
50+
'to use an inline fragment on',
51+
getSuggestedTypeNames(schema, type, fieldName),
52+
);
53+
54+
// If there are no suggested types, then perhaps this was a typo?
55+
if (suggestion === '') {
56+
suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
57+
}
5458
}
5559

5660
// Report an error, including helpful suggestions.

src/validation/rules/KnownArgumentNamesRule.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {
3535
const argName = argNode.name.value;
3636
const knownArgsNames = fieldDef.args.map((arg) => arg.name);
3737
const suggestions = suggestionList(argName, knownArgsNames);
38+
let suggestion = '';
39+
40+
if (context.didYouMean) {
41+
suggestion = didYouMean(suggestions);
42+
}
43+
3844
context.reportError(
3945
new GraphQLError(
4046
`Unknown argument "${argName}" on field "${parentType.name}.${fieldDef.name}".` +
41-
didYouMean(suggestions),
47+
suggestion,
4248
argNode,
4349
),
4450
);

src/validation/rules/KnownTypeNamesRule.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,18 @@ export function KnownTypeNamesRule(
5959
typeName,
6060
isSDL ? standardTypeNames.concat(typeNames) : typeNames,
6161
);
62+
let suggestion = '';
63+
64+
if (
65+
(context as ValidationContext).didYouMean == null ||
66+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
67+
(context as ValidationContext).didYouMean === true
68+
) {
69+
suggestion = didYouMean(suggestedTypes);
70+
}
71+
6272
context.reportError(
63-
new GraphQLError(
64-
`Unknown type "${typeName}".` + didYouMean(suggestedTypes),
65-
node,
66-
),
73+
new GraphQLError(`Unknown type "${typeName}".` + suggestion, node),
6774
);
6875
}
6976
},

src/validation/rules/ValuesOfCorrectTypeRule.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,19 @@ export function ValuesOfCorrectTypeRule(
6767
const parentType = getNamedType(context.getParentInputType());
6868
const fieldType = context.getInputType();
6969
if (!fieldType && isInputObjectType(parentType)) {
70-
const suggestions = suggestionList(
71-
node.name.value,
72-
Object.keys(parentType.getFields()),
73-
);
70+
let suggestion = '';
71+
if (context.didYouMean) {
72+
const suggestions = suggestionList(
73+
node.name.value,
74+
Object.keys(parentType.getFields()),
75+
);
76+
suggestion = didYouMean(suggestions);
77+
}
78+
7479
context.reportError(
7580
new GraphQLError(
7681
`Field "${node.name.value}" is not defined by type "${parentType.name}".` +
77-
didYouMean(suggestions),
82+
suggestion,
7883
node,
7984
),
8085
);

src/validation/validate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function validate(
3939
schema: GraphQLSchema,
4040
documentAST: DocumentNode,
4141
rules: ReadonlyArray<ValidationRule> = specifiedRules,
42-
options?: { maxErrors?: number },
42+
options: { maxErrors?: number, didYouMean?: boolean } = { maxErrors: undefined, didYouMean: true },
4343

4444
/** @deprecated will be removed in 17.0.0 */
4545
typeInfo: TypeInfo = new TypeInfo(schema),
@@ -68,6 +68,7 @@ export function validate(
6868
}
6969
errors.push(error);
7070
},
71+
options?.didYouMean
7172
);
7273

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

0 commit comments

Comments
 (0)