-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Changes from 5 commits
03377cb
c4e619f
e25fe3e
d460b74
10997c5
8982f88
6a80c43
5a9fb1e
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 |
---|---|---|
@@ -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' ]); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* @flow */ | ||
/** | ||
* 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. | ||
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 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]); | ||
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. 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. 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. Because of the flipped word case below, what to you think about setting the filter to keep only suggestions that match |
||
} | ||
|
||
/** | ||
* 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) { | ||
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. 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? 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. 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. |
||
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]; | ||
} |
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.
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.