-
Notifications
You must be signed in to change notification settings - Fork 2k
[validator] Add suggested types to incorrect field message #282
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,14 +11,31 @@ | |
import type { ValidationContext } from '../index'; | ||
import { GraphQLError } from '../../error'; | ||
import type { Field } from '../../language/ast'; | ||
import type { GraphQLType } from '../../type/definition'; | ||
|
||
import { | ||
isAbstractType, | ||
GraphQLAbstractType, | ||
GraphQLObjectType, | ||
} from '../../type/definition'; | ||
|
||
export function undefinedFieldMessage( | ||
fieldName: string, | ||
type: GraphQLType | ||
type: string, | ||
suggestedTypes: Array<string> | ||
): string { | ||
return `Cannot query field "${fieldName}" on "${type}".`; | ||
let message = `Cannot query field "${fieldName}" on type "${type}".`; | ||
const MAX_LENGTH = 5; | ||
if (suggestedTypes.length !== 0) { | ||
let suggestions = suggestedTypes | ||
.slice(0, MAX_LENGTH) | ||
.map(t => `"${t}"`) | ||
.join(', '); | ||
if (suggestedTypes.length > MAX_LENGTH) { | ||
suggestions += `, and ${suggestedTypes.length - MAX_LENGTH} other types`; | ||
} | ||
message += ` However, this field exists on ${suggestions}.`; | ||
message += ` Perhaps you meant to use an inline fragment?`; | ||
} | ||
return message; | ||
} | ||
|
||
/** | ||
|
@@ -34,12 +51,64 @@ export function FieldsOnCorrectType(context: ValidationContext): any { | |
if (type) { | ||
const fieldDef = context.getFieldDef(); | ||
if (!fieldDef) { | ||
// This isn't valid. Let's find suggestions, if any. | ||
let suggestedTypes = []; | ||
if (isAbstractType(type)) { | ||
suggestedTypes = | ||
getSiblingInterfacesIncludingField(type, node.name.value); | ||
suggestedTypes = suggestedTypes.concat( | ||
getImplementationsIncludingField(type, node.name.value) | ||
); | ||
} | ||
context.reportError(new GraphQLError( | ||
undefinedFieldMessage(node.name.value, type.name), | ||
undefinedFieldMessage(node.name.value, type.name, suggestedTypes), | ||
[ node ] | ||
)); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Return implementations of `type` that include `fieldName` as a valid field. | ||
*/ | ||
function getImplementationsIncludingField( | ||
type: GraphQLAbstractType, | ||
fieldName: string | ||
): Array<string> { | ||
return type.getPossibleTypes() | ||
.filter(t => t.getFields()[fieldName] !== undefined) | ||
.map(t => t.name) | ||
.sort(); | ||
} | ||
|
||
/** | ||
* Go through all of the implementations of type, and find other interaces | ||
* that they implement. If those interfaces include `field` as a valid field, | ||
* return them, sorted by how often the implementations include the other | ||
* interface. | ||
*/ | ||
function getSiblingInterfacesIncludingField( | ||
type: GraphQLAbstractType, | ||
fieldName: string | ||
): Array<string> { | ||
const implementingObjects = type.getPossibleTypes() | ||
.filter(t => t instanceof GraphQLObjectType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit is the same as lines 46/47 above. Perhaps there's a way to only do this once? Maybe you could set this up in three steps:
|
||
|
||
const suggestedInterfaces = implementingObjects.reduce((acc, t) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you wanted this to operate on objects instead of strings, you could do: const implCount = new Map();
implementingObjectTypes.forEach(objType => {
objType.getInterfaces().forEach(interfaceType => {
const count = implCount.get(interfaceType);
if (count) {
implCount.set(interfaceType, count + 1);
} else if (interfaceType.getFields()[fieldName]) {
implCount.set(interfaceType, 1);
}
})
})
return [ ...implCount.values() ].sort(
(a, b) => implCount.get(b) - implCount.get(a)
); |
||
t.getInterfaces().forEach(i => { | ||
if (i.getFields()[fieldName] === undefined) { | ||
return; | ||
} | ||
if (acc[i.name] === undefined) { | ||
acc[i.name] = 0; | ||
} | ||
acc[i.name] += 1; | ||
}); | ||
return acc; | ||
}, {}); | ||
return Object.keys(suggestedInterfaces) | ||
.sort((a,b) => suggestedInterfaces[b] - suggestedInterfaces[a]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor nit, I think it's preferable to put functions which are only used internally below any that are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do. |
||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe these 3 should actually be calls to
undefinedField
to match the other tests?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably; if we do so, I definitely need to add a dedicated test for
undefinedField
(I'll do that anyway).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I'll keep this as a true "integration" test.