Skip to content

Commit f7dbcfb

Browse files
committed
suggestion_list: change threshold to be coefficient of input length
Replicates graphql/graphql-js@4c10844
1 parent 65e9c06 commit f7dbcfb

File tree

9 files changed

+14
-18
lines changed

9 files changed

+14
-18
lines changed

docs/usage/queries.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Let's see what happens when we make a mistake in the query, by querying a non-ex
7070
{
7171
human(id: "1000") {
7272
name
73-
homeTown
73+
homePlace
7474
}
7575
}
7676
""")
@@ -79,7 +79,7 @@ Let's see what happens when we make a mistake in the query, by querying a non-ex
7979
You will get the following result as output::
8080

8181
ExecutionResult(data=None, errors=[GraphQLError(
82-
"Cannot query field 'homeTown' on type 'Human'. Did you mean 'homePlanet'?",
82+
"Cannot query field 'homePlace' on type 'Human'. Did you mean 'homePlanet'?",
8383
locations=[SourceLocation(line=5, column=9)])])
8484

8585
This is very helpful. Not only do we get the exact location of the mistake in the query,

docs/usage/validator.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ GraphQL specification. You can also run the validation step manually by calling
1414
{
1515
human(id: NEWHOPE) {
1616
name
17-
homeTown
17+
homePlace
1818
friends
1919
}
2020
}
@@ -27,7 +27,7 @@ In this case, we will get::
2727
'String cannot represent a non string value: NEWHOPE',
2828
locations=[SourceLocation(line=3, column=17)]),
2929
GraphQLError(
30-
"Cannot query field 'homeTown' on type 'Human'."
30+
"Cannot query field 'homePlace' on type 'Human'."
3131
" Did you mean 'homePlanet'?",
3232
locations=[SourceLocation(line=5, column=9)]),
3333
GraphQLError(

src/graphql/pyutils/suggestion_list.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ def suggestion_list(input_: str, options: Collection[str]) -> List[str]:
1212
options_by_distance = {}
1313
lexical_distance = LexicalDistance(input_)
1414

15-
input_threshold = len(input_) // 2
15+
threshold = int(len(input_) * 0.4) + 1
1616
for option in options:
17-
threshold = max(input_threshold, len(option) // 2, 1)
1817
distance = lexical_distance.measure(option, threshold)
1918
if distance is not None:
2019
options_by_distance[option] = distance

tests/benchmarks/test_validate_invalid_gql.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ def test_validate_invalid_query(benchmark, big_schema_sdl): # noqa: F811
2727
"message": "Cannot query field 'unknownField' on type 'Query'.",
2828
"locations": [(3, 11)],
2929
},
30-
{
31-
"message": "Unknown type 'unknownType'. Did you mean 'UnknownSignature'?",
32-
"locations": [(4, 18)],
33-
},
30+
{"message": "Unknown type 'unknownType'.", "locations": [(4, 18)]},
3431
{"message": "Unknown fragment 'unknownFragment'.", "locations": [(6, 16)]},
3532
{"message": "Unknown type 'anotherUnknownType'.", "locations": [(10, 34)]},
3633
{"message": "Fragment 'TestFragment' is never used.", "locations": [(10, 9)]},

tests/pyutils/test_suggestion_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def returns_options_with_transpositions():
3838
expect_suggestions("214365879", ["123456789"], ["123456789"])
3939

4040
def returns_options_sorted_based_on_lexical_distance():
41-
expect_suggestions("abc", ["a", "ab", "abc"], ["abc", "ab"])
41+
expect_suggestions("abc", ["a", "ab", "abc"], ["abc", "ab", "a"])
4242

4343
expect_suggestions(
4444
"GraphQl",

tests/test_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def executing_queries(capsys):
149149
assert out == expected_result(queries)
150150

151151
bad_query = queries.pop(0)
152-
assert "homeTown" in bad_query
152+
assert "homePlace" in bad_query
153153
exec(bad_query, scope)
154154
out, err = capsys.readouterr()
155155
assert not err

tests/type/test_definition.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,14 +1154,14 @@ def parses_an_enum():
11541154
enum_type.parse_literal(StringValueNode(value="BAR"))
11551155
assert exc_info.value.message == (
11561156
"Enum 'SomeEnum' cannot represent non-enum value: \"BAR\"."
1157-
" Did you mean the enum value 'BAR'?"
1157+
" Did you mean the enum value 'BAR' or 'BAZ'?"
11581158
)
11591159
assert enum_type.parse_literal(EnumValueNode(value="BAZ")) is None
11601160
with raises(GraphQLError) as exc_info:
11611161
enum_type.parse_literal(StringValueNode(value="BAZ"))
11621162
assert exc_info.value.message == (
11631163
"Enum 'SomeEnum' cannot represent non-enum value: \"BAZ\"."
1164-
" Did you mean the enum value 'BAZ'?"
1164+
" Did you mean the enum value 'BAZ' or 'BAR'?"
11651165
)
11661166

11671167
def accepts_an_enum_type_with_ast_node_and_extension_ast_nodes():

tests/type/test_enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ def does_not_accept_values_with_incorrect_casing():
175175
[
176176
{
177177
"message": "Value 'green' does not exist in 'Color' enum."
178-
" Did you mean the enum value 'GREEN'?",
178+
" Did you mean the enum value 'GREEN' or 'RED'?",
179179
"locations": [(1, 23)],
180180
}
181181
],

tests/validation/test_values_of_correct_type.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,16 +997,16 @@ def partial_object_unknown_field_arg():
997997
complicatedArgs {
998998
complexArgField(complexArg: {
999999
requiredField: true,
1000-
unknownField: "value"
1000+
invalidField: "value"
10011001
})
10021002
}
10031003
}
10041004
""",
10051005
[
10061006
{
1007-
"message": "Field 'unknownField'"
1007+
"message": "Field 'invalidField'"
10081008
" is not defined by type 'ComplexInput'."
1009-
" Did you mean 'booleanField', 'intField', or 'nonNullField'?",
1009+
" Did you mean 'intField'?",
10101010
"locations": [(6, 23)],
10111011
},
10121012
],

0 commit comments

Comments
 (0)