Skip to content

Commit 8982f88

Browse files
committed
addressed comments in PR: move file, update comment, filter out more options, remove redundant warning
1 parent 10997c5 commit 8982f88

8 files changed

+35
-88
lines changed

src/utilities/__tests__/suggestionList-test.js renamed to src/jsutils/__tests__/suggestionList-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ describe('suggestionList', () => {
2323

2424
it('Returns options sorted based on simularity', () => {
2525
expect(suggestionList('abc', [ 'a', 'ab', 'abc' ]))
26-
.to.deep.equal([ 'abc', 'ab', 'a' ]);
26+
.to.deep.equal([ 'abc', 'ab' ]);
2727
});
2828
});

src/utilities/suggestionList.js renamed to src/jsutils/suggestionList.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
*/
1010

1111
/**
12-
* Given a JavaScript value and a GraphQL type, determine if the value will be
13-
* accepted for that type. This is primarily useful for validating the
14-
* runtime values of query variables.
12+
* Given an invalid input string and a list of valid options, returns a filtered
13+
* list of valid options sorted based on their simularity with the input.
1514
*/
1615
export function suggestionList(
1716
input: string,
@@ -20,10 +19,15 @@ export function suggestionList(
2019
let i;
2120
const d = {};
2221
const oLength = options.length;
22+
const inputThreshold = input.length / 2;
2323
for (i = 0; i < oLength; i++) {
24-
d[options[i]] = lexicalDistance(input, options[i]);
24+
const distance = lexicalDistance(input, options[i]);
25+
const threshold = Math.max(inputThreshold, options[i].length / 2, 1);
26+
if (distance <= threshold) {
27+
d[options[i]] = distance;
28+
}
2529
}
26-
const result = options.slice();
30+
const result = Object.keys(d);
2731
return result.sort((a , b) => d[a] - d[b]);
2832
}
2933

src/validation/__tests__/FieldsOnCorrectType-test.js

+9-44
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ describe('Validate: Fields on correct type', () => {
9797
}
9898
}
9999
}`,
100-
[ undefinedField('unknown_pet_field', 'Pet', [], [ 'name' ], 3, 9),
100+
[ undefinedField('unknown_pet_field', 'Pet', [], [], 3, 9),
101101
undefinedField(
102102
'unknown_cat_field',
103103
'Cat',
104104
[],
105-
[ 'nickname', 'name', 'meowVolume', 'meows', 'furColor' ],
105+
[],
106106
5,
107107
13
108108
)
@@ -119,14 +119,7 @@ describe('Validate: Fields on correct type', () => {
119119
'meowVolume',
120120
'Dog',
121121
[],
122-
[ 'barkVolume',
123-
'name',
124-
'nickname',
125-
'barks',
126-
'doesKnowCommand',
127-
'isAtLocation',
128-
'isHousetrained',
129-
],
122+
[ 'barkVolume' ],
130123
3,
131124
9
132125
)
@@ -145,14 +138,7 @@ describe('Validate: Fields on correct type', () => {
145138
'unknown_field',
146139
'Dog',
147140
[],
148-
[ 'nickname',
149-
'name',
150-
'barkVolume',
151-
'doesKnowCommand',
152-
'isHousetrained',
153-
'isAtLocation',
154-
'barks',
155-
],
141+
[],
156142
3,
157143
9
158144
)
@@ -167,7 +153,7 @@ describe('Validate: Fields on correct type', () => {
167153
unknown_field
168154
}
169155
}`,
170-
[ undefinedField('unknown_field', 'Pet', [], [ 'name' ], 4, 11) ]
156+
[ undefinedField('unknown_field', 'Pet', [], [], 4, 11) ]
171157
);
172158
});
173159

@@ -182,14 +168,7 @@ describe('Validate: Fields on correct type', () => {
182168
'meowVolume',
183169
'Dog',
184170
[],
185-
[ 'barkVolume',
186-
'name',
187-
'nickname',
188-
'barks',
189-
'doesKnowCommand',
190-
'isAtLocation',
191-
'isHousetrained',
192-
],
171+
[ 'barkVolume' ],
193172
4,
194173
11
195174
)
@@ -206,14 +185,7 @@ describe('Validate: Fields on correct type', () => {
206185
'mooVolume',
207186
'Dog',
208187
[],
209-
[ 'barkVolume',
210-
'name',
211-
'nickname',
212-
'barks',
213-
'isAtLocation',
214-
'doesKnowCommand',
215-
'isHousetrained',
216-
],
188+
[ 'barkVolume' ],
217189
3,
218190
9
219191
)
@@ -230,14 +202,7 @@ describe('Validate: Fields on correct type', () => {
230202
'kawVolume',
231203
'Dog',
232204
[],
233-
[ 'barkVolume',
234-
'name',
235-
'nickname',
236-
'barks',
237-
'isAtLocation',
238-
'doesKnowCommand',
239-
'isHousetrained',
240-
],
205+
[ 'barkVolume' ],
241206
3,
242207
9
243208
)
@@ -250,7 +215,7 @@ describe('Validate: Fields on correct type', () => {
250215
fragment notDefinedOnInterface on Pet {
251216
tailLength
252217
}`,
253-
[ undefinedField('tailLength', 'Pet', [], [ 'name' ], 3, 9) ]
218+
[ undefinedField('tailLength', 'Pet', [], [], 3, 9) ]
254219
);
255220
});
256221

src/validation/__tests__/KnownArgumentNames-test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Validate: Known argument names', () => {
109109
dog @skip(unless: true)
110110
}
111111
`, [
112-
unknownDirectiveArg('unless', 'skip', [ 'if' ], 3, 19),
112+
unknownDirectiveArg('unless', 'skip', [], 3, 19),
113113
]);
114114
});
115115

@@ -119,7 +119,7 @@ describe('Validate: Known argument names', () => {
119119
doesKnowCommand(unknown: true)
120120
}
121121
`, [
122-
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 3, 25),
122+
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 25),
123123
]);
124124
});
125125

@@ -129,8 +129,8 @@ describe('Validate: Known argument names', () => {
129129
doesKnowCommand(whoknows: 1, dogCommand: SIT, unknown: true)
130130
}
131131
`, [
132-
unknownArg('whoknows', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 3, 25),
133-
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 3, 55),
132+
unknownArg('whoknows', 'doesKnowCommand', 'Dog', [], 3, 25),
133+
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 3, 55),
134134
]);
135135
});
136136

@@ -149,8 +149,8 @@ describe('Validate: Known argument names', () => {
149149
}
150150
}
151151
`, [
152-
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 4, 27),
153-
unknownArg('unknown', 'doesKnowCommand', 'Dog', [ 'dogCommand' ], 9, 31),
152+
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 4, 27),
153+
unknownArg('unknown', 'doesKnowCommand', 'Dog', [], 9, 31),
154154
]);
155155
});
156156

src/validation/__tests__/KnownTypeNames-test.js

+4-24
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,19 @@ describe('Validate: Known type names', () => {
5151
`, [
5252
unknownType(
5353
'JumbledUpLetters',
54-
[ 'ComplexInput',
55-
'ComplicatedArgs',
56-
'QueryRoot',
57-
'Intelligent',
58-
'HumanOrAlien'
59-
],
54+
[],
6055
2,
6156
23
6257
),
6358
unknownType(
6459
'Badger',
65-
[ 'Dog',
66-
'Boolean',
67-
'Pet',
68-
'Alien',
69-
'Being'
70-
],
60+
[],
7161
5,
7262
25
7363
),
7464
unknownType(
7565
'Peettt',
76-
[ 'Pet',
77-
'Float',
78-
'Being',
79-
'Cat',
80-
'Int'
81-
],
66+
[ 'Pet' ],
8267
8,
8368
29
8469
)
@@ -105,12 +90,7 @@ describe('Validate: Known type names', () => {
10590
`, [
10691
unknownType(
10792
'NotInTheSchema',
108-
[ '__Schema',
109-
'Intelligent',
110-
'HumanOrAlien',
111-
'Int',
112-
'Canine'
113-
],
93+
[],
11494
12,
11595
23
11696
),

src/validation/rules/FieldsOnCorrectType.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import type { ValidationContext } from '../index';
1212
import { GraphQLError } from '../../error';
13+
import { suggestionList } from '../../jsutils/suggestionList';
1314
import type { Field } from '../../language/ast';
1415
import type { GraphQLSchema } from '../../type/schema';
1516
import type { GraphQLAbstractType } from '../../type/definition';
@@ -19,7 +20,6 @@ import {
1920
GraphQLInputObjectType,
2021
GraphQLInterfaceType
2122
} from '../../type/definition';
22-
import { suggestionList } from '../../utilities/suggestionList';
2323

2424

2525
export function undefinedFieldMessage(
@@ -88,7 +88,7 @@ export function FieldsOnCorrectType(context: ValidationContext): any {
8888
type instanceof GraphQLInputObjectType) {
8989
suggestedFields = suggestionList(
9090
node.name.value,
91-
Object.keys(schema.getType(type.name).getFields())
91+
Object.keys(type.getFields())
9292
);
9393
}
9494
context.reportError(new GraphQLError(

src/validation/rules/KnownArgumentNames.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import type { ValidationContext } from '../index';
1212
import { GraphQLError } from '../../error';
1313
import find from '../../jsutils/find';
1414
import invariant from '../../jsutils/invariant';
15+
import { suggestionList } from '../../jsutils/suggestionList';
1516
import {
1617
FIELD,
1718
DIRECTIVE
1819
} from '../../language/kinds';
1920
import type { GraphQLType } from '../../type/definition';
20-
import { suggestionList } from '../../utilities/suggestionList';
21+
2122

2223
export function unknownArgMessage(
2324
argName: string,
@@ -32,8 +33,6 @@ export function unknownArgMessage(
3233
.map(t => `"${t}"`)
3334
.join(', ');
3435
message += ` Perhaps you meant ${suggestions}?`;
35-
} else {
36-
message += ' There is no known argument for this field.';
3736
}
3837
return message;
3938
}
@@ -50,8 +49,6 @@ export function unknownDirectiveArgMessage(
5049
.map(t => `"${t}"`)
5150
.join(', ');
5251
message += ` Perhaps you meant ${suggestions}?`;
53-
} else {
54-
message += ' There is no known argument for this directive.';
5552
}
5653
return message;
5754
}

src/validation/rules/KnownTypeNames.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
import type { ValidationContext } from '../index';
1212
import { GraphQLError } from '../../error';
13+
import { suggestionList } from '../../jsutils/suggestionList';
1314
import type { GraphQLType } from '../../type/definition';
14-
import { suggestionList } from '../../utilities/suggestionList';
15+
1516

1617
export function unknownTypeMessage(
1718
type: GraphQLType,

0 commit comments

Comments
 (0)