Skip to content

Commit 0d6d9b7

Browse files
committed
didYouMean: quote suggested values (#2165)
1 parent 10b8e95 commit 0d6d9b7

File tree

8 files changed

+21
-21
lines changed

8 files changed

+21
-21
lines changed

src/jsutils/__tests__/didYouMean-test.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,28 @@ describe('didYouMean', () => {
1111
});
1212

1313
it('Handles single suggestion', () => {
14-
expect(didYouMean(['A'])).to.equal(' Did you mean A?');
14+
expect(didYouMean(['A'])).to.equal(' Did you mean "A"?');
1515
});
1616

1717
it('Handles two suggestions', () => {
18-
expect(didYouMean(['A', 'B'])).to.equal(' Did you mean A or B?');
18+
expect(didYouMean(['A', 'B'])).to.equal(' Did you mean "A" or "B"?');
1919
});
2020

2121
it('Handles multiple suggestions', () => {
22-
expect(didYouMean(['A', 'B', 'C'])).to.equal(' Did you mean A, B, or C?');
22+
expect(didYouMean(['A', 'B', 'C'])).to.equal(
23+
' Did you mean "A", "B", or "C"?',
24+
);
2325
});
2426

2527
it('Limits to five suggestions', () => {
2628
expect(didYouMean(['A', 'B', 'C', 'D', 'E', 'F'])).to.equal(
27-
' Did you mean A, B, C, D, or E?',
29+
' Did you mean "A", "B", "C", "D", or "E"?',
2830
);
2931
});
3032

3133
it('Adds sub-message', () => {
3234
expect(didYouMean('the letter', ['A'])).to.equal(
33-
' Did you mean the letter A?',
35+
' Did you mean the letter "A"?',
3436
);
3537
});
3638
});

src/jsutils/didYouMean.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare function didYouMean(
1414

1515
// eslint-disable-next-line no-redeclare
1616
export default function didYouMean(firstArg, secondArg) {
17-
const [subMessage, suggestions] =
17+
const [subMessage, suggestionsArg] =
1818
typeof firstArg === 'string'
1919
? [firstArg, secondArg]
2020
: [undefined, firstArg];
@@ -24,6 +24,7 @@ export default function didYouMean(firstArg, secondArg) {
2424
message += subMessage + ' ';
2525
}
2626

27+
const suggestions = suggestionsArg.map(x => `"${x}"`);
2728
switch (suggestions.length) {
2829
case 0:
2930
return '';

src/utilities/coerceInputValue.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function coerceInputValueImpl(
149149
inputValue,
150150
new GraphQLError(
151151
`Field "${fieldName}" is not defined by type "${type.name}".` +
152-
didYouMean(suggestions.map(x => `"${x}"`)),
152+
didYouMean(suggestions),
153153
),
154154
);
155155
}
@@ -206,7 +206,7 @@ function coerceInputValueImpl(
206206
inputValue,
207207
new GraphQLError(
208208
`Expected type "${type.name}".` +
209-
didYouMean('the enum value', suggestions.map(x => `"${x}"`)),
209+
didYouMean('the enum value', suggestions),
210210
),
211211
);
212212
return;

src/validation/rules/FieldsOnCorrectType.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ export function FieldsOnCorrectType(context: ValidationContext): ASTVisitor {
4747
: getSuggestedFieldNames(schema, type, fieldName);
4848

4949
// Report an error, including helpful suggestions.
50-
const quotedTypeNames = suggestedTypeNames.map(x => `"${x}"`);
51-
const quotedFieldNames = suggestedFieldNames.map(x => `"${x}"`);
5250
context.reportError(
5351
new GraphQLError(
5452
`Cannot query field "${fieldName}" on type "${type.name}".` +
55-
(didYouMean('to use an inline fragment on', quotedTypeNames) ||
56-
didYouMean(quotedFieldNames)),
53+
(didYouMean(
54+
'to use an inline fragment on',
55+
suggestedTypeNames,
56+
) || didYouMean(suggestedFieldNames)),
5757
node,
5858
),
5959
);

src/validation/rules/KnownArgumentNames.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function KnownArgumentNames(context: ValidationContext): ASTVisitor {
3636
context.reportError(
3737
new GraphQLError(
3838
`Unknown argument "${argName}" on field "${parentType.name}.${fieldDef.name}".` +
39-
didYouMean(suggestions.map(x => `"${x}"`)),
39+
didYouMean(suggestions),
4040
argNode,
4141
),
4242
);
@@ -81,7 +81,7 @@ export function KnownArgumentNamesOnDirectives(
8181
context.reportError(
8282
new GraphQLError(
8383
`Unknown argument "${argName}" on directive "@${directiveName}".` +
84-
didYouMean(suggestions.map(x => `"${x}"`)),
84+
didYouMean(suggestions),
8585
argNode,
8686
),
8787
);

src/validation/rules/KnownTypeNames.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ export function KnownTypeNames(
5959
);
6060
context.reportError(
6161
new GraphQLError(
62-
`Unknown type "${typeName}".` +
63-
didYouMean(suggestedTypes.map(x => `"${x}"`)),
62+
`Unknown type "${typeName}".` + didYouMean(suggestedTypes),
6463
node,
6564
),
6665
);

src/validation/rules/PossibleTypeExtensions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export function PossibleTypeExtensions(
7878
context.reportError(
7979
new GraphQLError(
8080
`Cannot extend type "${typeName}" because it is not defined.` +
81-
didYouMean(suggestedTypes.map(x => `"${x}"`)),
81+
didYouMean(suggestedTypes),
8282
node.name,
8383
),
8484
);

src/validation/rules/ValuesOfCorrectType.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export function ValuesOfCorrectType(context: ValidationContext): ASTVisitor {
7777
context.reportError(
7878
new GraphQLError(
7979
`Field "${node.name.value}" is not defined by type "${parentType.name}".` +
80-
didYouMean(suggestions.map(name => '"' + name + '"')),
80+
didYouMean(suggestions),
8181
node,
8282
),
8383
);
@@ -118,9 +118,7 @@ function isValidValueNode(context: ValidationContext, node: ValueNode): void {
118118
if (isEnumType(type)) {
119119
if (node.kind !== Kind.ENUM || !type.getValue(node.value)) {
120120
const allNames = type.getValues().map(value => value.name);
121-
const suggestedValues = suggestionList(print(node), allNames).map(
122-
x => '"' + x + '"',
123-
);
121+
const suggestedValues = suggestionList(print(node), allNames);
124122

125123
context.reportError(
126124
new GraphQLError(

0 commit comments

Comments
 (0)