Skip to content

Commit

Permalink
Allow skipping suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 4, 2024
1 parent a1d22a2 commit 43b2f4e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 12 deletions.
11 changes: 11 additions & 0 deletions src/validation/ValidationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export class SDLValidationContext extends ASTValidationContext {
this._schema = schema;
}

get shouldProvideSuggestions() {
return true
}

get [Symbol.toStringTag]() {
return 'SDLValidationContext';
}
Expand All @@ -177,24 +181,31 @@ export class ValidationContext extends ASTValidationContext {
OperationDefinitionNode,
ReadonlyArray<VariableUsage>
>;
private _shouldProvideSuggestions: boolean

constructor(
schema: GraphQLSchema,
ast: DocumentNode,
typeInfo: TypeInfo,
onError: (error: GraphQLError) => void,
shouldProvideSuggestions: boolean
) {
super(ast, onError);
this._schema = schema;
this._typeInfo = typeInfo;
this._variableUsages = new Map();
this._recursiveVariableUsages = new Map();
this._shouldProvideSuggestions = shouldProvideSuggestions
}

get [Symbol.toStringTag]() {
return 'ValidationContext';
}

get shouldProvideSuggestions() {
return this._shouldProvideSuggestions
}

getSchema(): GraphQLSchema {
return this._schema;
}
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/FieldsOnCorrectTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ export function FieldsOnCorrectTypeRule(
// First determine if there are any suggested types to condition on.
let suggestion = didYouMean(
'to use an inline fragment on',
getSuggestedTypeNames(schema, type, fieldName),
context.shouldProvideSuggestions ? getSuggestedTypeNames(schema, type, fieldName) : [],
);

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

// Report an error, including helpful suggestions.
Expand Down
8 changes: 4 additions & 4 deletions src/validation/rules/KnownArgumentNamesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {
);
if (!varDef) {
const argName = argNode.name.value;
const suggestions = suggestionList(
const suggestions = context.shouldProvideSuggestions ? suggestionList(
argName,
Array.from(fragmentSignature.variableDefinitions.values()).map(
(varSignature) => varSignature.variable.name.value,
),
);
) : [];
context.reportError(
new GraphQLError(
`Unknown argument "${argName}" on fragment "${fragmentSignature.definition.name.value}".` +
Expand All @@ -57,10 +57,10 @@ export function KnownArgumentNamesRule(context: ValidationContext): ASTVisitor {

if (!argDef && fieldDef && parentType) {
const argName = argNode.name.value;
const suggestions = suggestionList(
const suggestions = context.shouldProvideSuggestions ? suggestionList(
argName,
fieldDef.args.map((arg) => arg.name),
);
) : [];
context.reportError(
new GraphQLError(
`Unknown argument "${argName}" on field "${parentType}.${fieldDef.name}".` +
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/KnownTypeNamesRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ export function KnownTypeNamesRule(
return;
}

const suggestedTypes = suggestionList(
const suggestedTypes = context.shouldProvideSuggestions ? suggestionList(
typeName,
isSDL ? [...standardTypeNames, ...typeNames] : [...typeNames],
);
) : [];
context.reportError(
new GraphQLError(
`Unknown type "${typeName}".` + didYouMean(suggestedTypes),
Expand Down
2 changes: 1 addition & 1 deletion src/validation/rules/PossibleTypeExtensionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function PossibleTypeExtensionsRule(
...Object.keys(schema?.getTypeMap() ?? {}),
];

const suggestedTypes = suggestionList(typeName, allTypeNames);
const suggestedTypes = context.shouldProvideSuggestions ? suggestionList(typeName, allTypeNames) : [];
context.reportError(
new GraphQLError(
`Cannot extend type "${typeName}" because it is not defined.` +
Expand Down
4 changes: 2 additions & 2 deletions src/validation/rules/ValuesOfCorrectTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ export function ValuesOfCorrectTypeRule(
const parentType = getNamedType(context.getParentInputType());
const fieldType = context.getInputType();
if (!fieldType && isInputObjectType(parentType)) {
const suggestions = suggestionList(
const suggestions = context.shouldProvideSuggestions ? suggestionList(
node.name.value,
Object.keys(parentType.getFields()),
);
) : [];
context.reportError(
new GraphQLError(
`Field "${node.name.value}" is not defined by type "${parentType}".` +
Expand Down
4 changes: 3 additions & 1 deletion src/validation/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ export function validate(
schema: GraphQLSchema,
documentAST: DocumentNode,
rules: ReadonlyArray<ValidationRule> = specifiedRules,
options?: { maxErrors?: number },
options?: { maxErrors?: number; shouldProvideSuggestions?: boolean },
): ReadonlyArray<GraphQLError> {
const maxErrors = options?.maxErrors ?? 100;
const shouldProvideSuggestions = options?.shouldProvideSuggestions ?? true;

// If the schema used for validation is invalid, throw an error.
assertValidSchema(schema);
Expand All @@ -63,6 +64,7 @@ export function validate(
}
errors.push(error);
},
shouldProvideSuggestions
);

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

0 comments on commit 43b2f4e

Please sign in to comment.