Skip to content

Commit

Permalink
FieldsOnCorrectType: implement stable sort order for suggested… (#2395)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Jan 26, 2020
1 parent cdf9c83 commit 762642e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/jsutils/suggestionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default function suggestionList(
}
}
return Object.keys(optionsByDistance).sort((a, b) => {
const diff = optionsByDistance[a] - optionsByDistance[b];
return diff !== 0 ? diff : a.localeCompare(b);
const distanceDiff = optionsByDistance[a] - optionsByDistance[b];
return distanceDiff !== 0 ? distanceDiff : a.localeCompare(b);
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/validation/__tests__/FieldsOnCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe('Validate: Fields on correct type', () => {
`).to.deep.equal([
{
message:
'Cannot query field "nickname" on type "Pet". Did you mean to use an inline fragment on "Dog" or "Cat"?',
'Cannot query field "nickname" on type "Pet". Did you mean to use an inline fragment on "Cat" or "Dog"?',
locations: [{ line: 3, column: 9 }],
},
]);
Expand Down Expand Up @@ -237,7 +237,7 @@ describe('Validate: Fields on correct type', () => {
`).to.deep.equal([
{
message:
'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Being", "Pet", "Canine", "Dog", or "Cat"?',
'Cannot query field "name" on type "CatOrDog". Did you mean to use an inline fragment on "Being", "Pet", "Canine", "Cat", or "Dog"?',
locations: [{ line: 3, column: 9 }],
},
]);
Expand Down
71 changes: 45 additions & 26 deletions src/validation/rules/FieldsOnCorrectType.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @flow strict

import arrayFrom from '../../polyfills/arrayFrom';

import didYouMean from '../../jsutils/didYouMean';
import suggestionList from '../../jsutils/suggestionList';

Expand Down Expand Up @@ -62,44 +64,61 @@ export function FieldsOnCorrectType(context: ValidationContext): ASTVisitor {

/**
* Go through all of the implementations of type, as well as the interfaces that
* they implement. If any of those types include the provided field, suggest
* them, sorted by how often the type is referenced, starting with Interfaces.
* they implement. If any of those types include the provided field, suggest them,
* sorted by how often the type is referenced.
*/
function getSuggestedTypeNames(
schema: GraphQLSchema,
type: GraphQLOutputType,
fieldName: string,
): Array<string> {
if (isAbstractType(type)) {
const suggestedObjectTypes = [];
const interfaceUsageCount = Object.create(null);
for (const possibleType of schema.getPossibleTypes(type)) {
if (!possibleType.getFields()[fieldName]) {
if (!isAbstractType(type)) {
// Must be an Object type, which does not have possible fields.
return [];
}

const suggestedTypes = new Set();
const usageCount = Object.create(null);
for (const possibleType of schema.getPossibleTypes(type)) {
if (!possibleType.getFields()[fieldName]) {
continue;
}

// This object type defines this field.
suggestedTypes.add(possibleType);
usageCount[possibleType.name] = 1;

for (const possibleInterface of possibleType.getInterfaces()) {
if (!possibleInterface.getFields()[fieldName]) {
continue;
}
// This object type defines this field.
suggestedObjectTypes.push(possibleType.name);
for (const possibleInterface of possibleType.getInterfaces()) {
if (!possibleInterface.getFields()[fieldName]) {
continue;
}
// This interface type defines this field.
interfaceUsageCount[possibleInterface.name] =
(interfaceUsageCount[possibleInterface.name] || 0) + 1;
}

// This interface type defines this field.
suggestedTypes.add(possibleInterface);
usageCount[possibleInterface.name] =
(usageCount[possibleInterface.name] || 0) + 1;
}
}

// Suggest interface types based on how common they are.
const suggestedInterfaceTypes = Object.keys(interfaceUsageCount).sort(
(a, b) => interfaceUsageCount[b] - interfaceUsageCount[a],
);
return arrayFrom(suggestedTypes)
.sort((typeA, typeB) => {
// Suggest both interface and object types based on how common they are.
const usageCountDiff = usageCount[typeB.name] - usageCount[typeA.name];
if (usageCountDiff !== 0) {
return usageCountDiff;
}

// Suggest both interface and object types.
return suggestedInterfaceTypes.concat(suggestedObjectTypes);
}
// Suggest super types first followed by subtypes
if (isAbstractType(typeA) && schema.isSubType(typeA, typeB)) {
return -1;
}
if (isAbstractType(typeB) && schema.isSubType(typeB, typeA)) {
return 1;
}

// Otherwise, must be an Object type, which does not have possible fields.
return [];
return typeA.name.localeCompare(typeB.name);
})
.map(x => x.name);
}

/**
Expand Down

0 comments on commit 762642e

Please sign in to comment.