Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable 'did you mean x' during validation #3467

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/validation/ValidationContext.ts
Expand Up @@ -162,6 +162,8 @@ export class SDLValidationContext extends ASTValidationContext {
export type SDLValidationRule = (context: SDLValidationContext) => ASTVisitor;

export class ValidationContext extends ASTValidationContext {
public readonly didYouMean: boolean;

private _schema: GraphQLSchema;
private _typeInfo: TypeInfo;
private _variableUsages: Map<
Expand All @@ -179,12 +181,15 @@ export class ValidationContext extends ASTValidationContext {
ast: DocumentNode,
typeInfo: TypeInfo,
onError: (error: GraphQLError) => void,
/** Whether the "did you mean x" suggestions should be enabled. */
didYouMean?: boolean,
) {
super(ast, onError);
this._schema = schema;
this._typeInfo = typeInfo;
this._variableUsages = new Map();
this._recursiveVariableUsages = new Map();
this.didYouMean = didYouMean == null ? true : didYouMean;
}

get [Symbol.toStringTag]() {
Expand Down
22 changes: 13 additions & 9 deletions src/validation/rules/FieldsOnCorrectTypeRule.ts
Expand Up @@ -42,15 +42,19 @@ export function FieldsOnCorrectTypeRule(
const schema = context.getSchema();
const fieldName = node.name.value;

// First determine if there are any suggested types to condition on.
let suggestion = didYouMean(
'to use an inline fragment on',
getSuggestedTypeNames(schema, type, fieldName),
);

// If there are no suggested types, then perhaps this was a typo?
if (suggestion === '') {
suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
let suggestion = '';

if (context.didYouMean) {
// First determine if there are any suggested types to condition on.
suggestion = didYouMean(
'to use an inline fragment on',
getSuggestedTypeNames(schema, type, fieldName),
);

// If there are no suggested types, then perhaps this was a typo?
if (suggestion === '') {
suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
}
}

// Report an error, including helpful suggestions.
Expand Down
8 changes: 7 additions & 1 deletion src/validation/rules/KnownArgumentNamesRule.ts
Expand Up @@ -35,10 +35,16 @@ export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {
const argName = argNode.name.value;
const knownArgsNames = fieldDef.args.map((arg) => arg.name);
const suggestions = suggestionList(argName, knownArgsNames);
let suggestion = '';

if (context.didYouMean) {
suggestion = didYouMean(suggestions);
}

context.reportError(
new GraphQLError(
`Unknown argument "${argName}" on field "${parentType.name}.${fieldDef.name}".` +
didYouMean(suggestions),
suggestion,
argNode,
),
);
Expand Down
15 changes: 11 additions & 4 deletions src/validation/rules/KnownTypeNamesRule.ts
Expand Up @@ -59,11 +59,18 @@ export function KnownTypeNamesRule(
typeName,
isSDL ? standardTypeNames.concat(typeNames) : typeNames,
);
let suggestion = '';

if (
(context as ValidationContext).didYouMean == null ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-boolean-literal-compare
(context as ValidationContext).didYouMean === true
) {
suggestion = didYouMean(suggestedTypes);
}

context.reportError(
new GraphQLError(
`Unknown type "${typeName}".` + didYouMean(suggestedTypes),
node,
),
new GraphQLError(`Unknown type "${typeName}".` + suggestion, node),
);
}
},
Expand Down
15 changes: 10 additions & 5 deletions src/validation/rules/ValuesOfCorrectTypeRule.ts
Expand Up @@ -67,14 +67,19 @@ export function ValuesOfCorrectTypeRule(
const parentType = getNamedType(context.getParentInputType());
const fieldType = context.getInputType();
if (!fieldType && isInputObjectType(parentType)) {
const suggestions = suggestionList(
node.name.value,
Object.keys(parentType.getFields()),
);
let suggestion = '';
if (context.didYouMean) {
const suggestions = suggestionList(
node.name.value,
Object.keys(parentType.getFields()),
);
suggestion = didYouMean(suggestions);
}

context.reportError(
new GraphQLError(
`Field "${node.name.value}" is not defined by type "${parentType.name}".` +
didYouMean(suggestions),
suggestion,
node,
),
);
Expand Down
6 changes: 5 additions & 1 deletion src/validation/validate.ts
Expand Up @@ -39,7 +39,10 @@ export function validate(
schema: GraphQLSchema,
documentAST: DocumentNode,
rules: ReadonlyArray<ValidationRule> = specifiedRules,
options?: { maxErrors?: number },
options: { maxErrors?: number; didYouMean?: boolean } = {
maxErrors: undefined,
didYouMean: true,
},

/** @deprecated will be removed in 17.0.0 */
typeInfo: TypeInfo = new TypeInfo(schema),
Expand Down Expand Up @@ -68,6 +71,7 @@ export function validate(
}
errors.push(error);
},
options?.didYouMean,
);

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