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

FieldsOnCorrectType: refactor error message #2393

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions src/validation/rules/FieldsOnCorrectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,23 @@ export function FieldsOnCorrectType(context: ValidationContext): ASTVisitor {
// This field doesn't exist, lets look for suggestions.
const schema = context.getSchema();
const fieldName = node.name.value;

// First determine if there are any suggested types to condition on.
const suggestedTypeNames = getSuggestedTypeNames(
schema,
type,
fieldName,
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?
const suggestedFieldNames =
suggestedTypeNames.length !== 0
? []
: getSuggestedFieldNames(schema, type, fieldName);
if (suggestion === '') {
suggestion = didYouMean(getSuggestedFieldNames(type, fieldName));
}

// Report an error, including helpful suggestions.
context.reportError(
new GraphQLError(
`Cannot query field "${fieldName}" on type "${type.name}".` +
(didYouMean(
'to use an inline fragment on',
suggestedTypeNames,
) || didYouMean(suggestedFieldNames)),
suggestion,
node,
),
);
Expand Down Expand Up @@ -110,7 +107,6 @@ function getSuggestedTypeNames(
* that may be the result of a typo.
*/
function getSuggestedFieldNames(
_schema: GraphQLSchema,
type: GraphQLOutputType,
fieldName: string,
): Array<string> {
Expand Down