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

Include possible field, argument, type names when validation fails #355

Merged
merged 8 commits into from
Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from 5 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
28 changes: 28 additions & 0 deletions src/utilities/__tests__/suggestionList-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { suggestionList } from '../suggestionList';

describe('suggestionList', () => {

it('Returns results when input is empty', () => {
expect(suggestionList('', [ 'a' ])).to.deep.equal([ 'a' ]);
});

it('Returns empty array when there are no options', () => {
expect(suggestionList('input', [])).to.deep.equal([]);
});

it('Returns options sorted based on simularity', () => {
expect(suggestionList('abc', [ 'a', 'ab', 'abc' ]))
.to.deep.equal([ 'abc', 'ab', 'a' ]);
});
});
78 changes: 78 additions & 0 deletions src/utilities/suggestionList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* @flow */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file should go in the jsutils folder - that is where we keep the dependency-free JS utilities that are not specific to graphql, just happen to be used here.

/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

/**
* Given a JavaScript value and a GraphQL type, determine if the value will be
* accepted for that type. This is primarily useful for validating the
* runtime values of query variables.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this description doesn't seem right

*/
export function suggestionList(
input: string,
options: Array<string>
): Array<string> {
let i;
const d = {};
const oLength = options.length;
for (i = 0; i < oLength; i++) {
d[options[i]] = lexicalDistance(input, options[i]);
}
const result = options.slice();
return result.sort((a , b) => d[a] - d[b]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead of or in addition to just sorting them all, we should also filter them down to those with a very low edit distance. Some of the suggestions in the texts seem like they're unlikely to be typos.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of the flipped word case below, what to you think about setting the filter to keep only suggestions that match min(input.length/2)?

}

/**
* Computes the lexical distance between strings A and B.
*
* The "distance" between two strings is given by counting the minimum number
* of edits needed to transform string A into string B. An edit can be an
* insertion, deletion, or substitution of a single character, or a swap of two
* adjacent characters.
*
* This distance can be useful for detecting typos in input or sorting
*
* @param {string} a
* @param {string} b
* @return {int} distance in number of edits
*/
function lexicalDistance(a, b) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need a variation of this for the "flipped words" scenario you mentioned. Can you give an example of a common typo we would want to recommend a fix for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen this couple of times when someone introduces a pretty long field name or mutation response type. They either flip two of the middle words or missed something like the word "Payload" completely. Because at a glance nothing looked misspelled, they thought it was more of a problem with updating the schema and ended up spending a lot of time trying to debug and rerun that.
I actually started with trying to split up the input into words and trying to match up n-1 word, but I didn't think the extra complexity to get that number is going to be significantly better than just lexicalDistance. That's why I decided to keep it simple. We can always add the variation for that case when we have evidence that the lexicalDistance isn't good enough.

let i;
let j;
const d = [];
const aLength = a.length;
const bLength = b.length;

for (i = 0; i <= aLength; i++) {
d[i] = [ i ];
}

for (j = 1; j <= bLength; j++) {
d[0][j] = j;
}

for (i = 1; i <= aLength; i++) {
for (j = 1; j <= bLength; j++) {
const cost = a[i - 1] === b[j - 1] ? 0 : 1;

d[i][j] = Math.min(
d[i - 1][j] + 1,
d[i][j - 1] + 1,
d[i - 1][j - 1] + cost
);

if (i > 1 && j > 1 &&
a[i - 1] === b[j - 2] &&
a[i - 2] === b[j - 1]) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
}
}
}

return d[aLength][bLength];
}
146 changes: 125 additions & 21 deletions src/validation/__tests__/FieldsOnCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,21 @@ import {
} from '../rules/FieldsOnCorrectType';


function undefinedField(field, type, suggestions, line, column) {
function undefinedField(
field,
type,
suggestedTypes,
suggestedFields,
line,
column
) {
return {
message: undefinedFieldMessage(field, type, suggestions),
message: undefinedFieldMessage(
field,
type,
suggestedTypes,
suggestedFields
),
locations: [ { line, column } ],
};
}
Expand Down Expand Up @@ -85,8 +97,16 @@ describe('Validate: Fields on correct type', () => {
}
}
}`,
[ undefinedField('unknown_pet_field', 'Pet', [], 3, 9),
undefinedField('unknown_cat_field', 'Cat', [], 5, 13) ]
[ undefinedField('unknown_pet_field', 'Pet', [], [ 'name' ], 3, 9),
undefinedField(
'unknown_cat_field',
'Cat',
[],
[ 'nickname', 'name', 'meowVolume', 'meows', 'furColor' ],
5,
13
)
]
);
});

Expand All @@ -95,7 +115,22 @@ describe('Validate: Fields on correct type', () => {
fragment fieldNotDefined on Dog {
meowVolume
}`,
[ undefinedField('meowVolume', 'Dog', [], 3, 9) ]
[ undefinedField(
'meowVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'doesKnowCommand',
'isAtLocation',
'isHousetrained',
],
3,
9
)
]
);
});

Expand All @@ -106,7 +141,22 @@ describe('Validate: Fields on correct type', () => {
deeper_unknown_field
}
}`,
[ undefinedField('unknown_field', 'Dog', [], 3, 9) ]
[ undefinedField(
'unknown_field',
'Dog',
[],
[ 'nickname',
'name',
'barkVolume',
'doesKnowCommand',
'isHousetrained',
'isAtLocation',
'barks',
],
3,
9
)
]
);
});

Expand All @@ -117,7 +167,7 @@ describe('Validate: Fields on correct type', () => {
unknown_field
}
}`,
[ undefinedField('unknown_field', 'Pet', [], 4, 11) ]
[ undefinedField('unknown_field', 'Pet', [], [ 'name' ], 4, 11) ]
);
});

Expand All @@ -128,7 +178,22 @@ describe('Validate: Fields on correct type', () => {
meowVolume
}
}`,
[ undefinedField('meowVolume', 'Dog', [], 4, 11) ]
[ undefinedField(
'meowVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'doesKnowCommand',
'isAtLocation',
'isHousetrained',
],
4,
11
)
]
);
});

Expand All @@ -137,7 +202,22 @@ describe('Validate: Fields on correct type', () => {
fragment aliasedFieldTargetNotDefined on Dog {
volume : mooVolume
}`,
[ undefinedField('mooVolume', 'Dog', [], 3, 9) ]
[ undefinedField(
'mooVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'isAtLocation',
'doesKnowCommand',
'isHousetrained',
],
3,
9
)
]
);
});

Expand All @@ -146,7 +226,22 @@ describe('Validate: Fields on correct type', () => {
fragment aliasedLyingFieldTargetNotDefined on Dog {
barkVolume : kawVolume
}`,
[ undefinedField('kawVolume', 'Dog', [], 3, 9) ]
[ undefinedField(
'kawVolume',
'Dog',
[],
[ 'barkVolume',
'name',
'nickname',
'barks',
'isAtLocation',
'doesKnowCommand',
'isHousetrained',
],
3,
9
)
]
);
});

Expand All @@ -155,7 +250,7 @@ describe('Validate: Fields on correct type', () => {
fragment notDefinedOnInterface on Pet {
tailLength
}`,
[ undefinedField('tailLength', 'Pet', [], 3, 9) ]
[ undefinedField('tailLength', 'Pet', [], [ 'name' ], 3, 9) ]
);
});

Expand All @@ -164,7 +259,7 @@ describe('Validate: Fields on correct type', () => {
fragment definedOnImplementorsButNotInterface on Pet {
nickname
}`,
[ undefinedField('nickname', 'Pet', [ 'Cat', 'Dog' ], 3, 9) ]
[ undefinedField('nickname', 'Pet', [ 'Cat', 'Dog' ], [ 'name' ], 3, 9) ]
);
});

Expand All @@ -181,7 +276,7 @@ describe('Validate: Fields on correct type', () => {
fragment directFieldSelectionOnUnion on CatOrDog {
directField
}`,
[ undefinedField('directField', 'CatOrDog', [], 3, 9) ]
[ undefinedField('directField', 'CatOrDog', [], [], 3, 9) ]
);
});

Expand All @@ -195,6 +290,7 @@ describe('Validate: Fields on correct type', () => {
'name',
'CatOrDog',
[ 'Being', 'Pet', 'Canine', 'Cat', 'Dog' ],
[],
3,
9
)
Expand All @@ -218,25 +314,33 @@ describe('Validate: Fields on correct type', () => {
describe('Fields on correct type error message', () => {
it('Works with no suggestions', () => {
expect(
undefinedFieldMessage('T', 'f', [])
).to.equal('Cannot query field "T" on type "f".');
undefinedFieldMessage('f', 'T', [], [])
).to.equal('Cannot query field "f" on type "T".');
});

it('Works with no small numbers of suggestions', () => {
expect(
undefinedFieldMessage('T', 'f', [ 'A', 'B' ])
).to.equal('Cannot query field "T" on type "f". ' +
undefinedFieldMessage('f', 'T', [ 'A', 'B' ], [ 'z', 'y' ])
).to.equal('Cannot query field "f" on type "T". ' +
'However, this field exists on "A", "B". ' +
'Perhaps you meant to use an inline fragment?');
'Perhaps you meant to use an inline fragment? ' +
'Did you mean to query "z", "y"?');
});

it('Works with lots of suggestions', () => {
expect(
undefinedFieldMessage('T', 'f', [ 'A', 'B', 'C', 'D', 'E', 'F' ])
).to.equal('Cannot query field "T" on type "f". ' +
undefinedFieldMessage(
'f',
'T',
[ 'A', 'B', 'C', 'D', 'E', 'F' ],
[ 'z', 'y', 'x', 'w', 'v', 'u' ]
)
).to.equal('Cannot query field "f" on type "T". ' +
'However, this field exists on "A", "B", "C", "D", "E", ' +
'and 1 other types. ' +
'Perhaps you meant to use an inline fragment?');
'Perhaps you meant to use an inline fragment? ' +
'Did you mean to query "z", "y", "x", "w", "v", or 1 other field?'
);
});
});
});
Expand Down
Loading