Skip to content

Commit fcccb11

Browse files
committed
fix: Internal error on some invalid schemas
1 parent fd01402 commit fcccb11

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changelog
44
`Unreleased`_ - TBD
55
-------------------
66

7+
**Fixed**
8+
9+
- Internal error on some invalid schemas.
10+
711
`0.8.1`_ - 2022-04-27
812
---------------------
913

src/hypothesis_graphql/_strategies/strategy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ def subset_of_fields(
317317
fields: Dict[str, graphql.GraphQLInputField], *, force_required: bool = False
318318
) -> st.SearchStrategy[List[Tuple[str, graphql.GraphQLInputField]]]:
319319
"""A helper to select a subset of fields."""
320+
if not fields:
321+
# The schema is invalid as there should be at least one field
322+
# But there should not be an internal error because of it
323+
return EMPTY_LISTS_STRATEGY
320324
field_pairs = sorted(fields.items())
321325
# if we need to always generate required fields, then return them and extend with a subset of optional fields
322326
if force_required:

test/test_queries.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,8 @@ def test_arguments(data, schema, arguments, node_names, notnull, validate_operat
129129
@given(data=st.data())
130130
def test_interface(data, schema, query_type, validate_operation):
131131
schema = schema + query_type
132-
parsed_schema = cached_build_schema(schema)
133132
query = data.draw(gql_st.queries(schema))
134-
validate_operation(parsed_schema, query)
133+
validate_operation(schema, query)
135134

136135

137136
@pytest.mark.parametrize(
@@ -453,3 +452,26 @@ def validate_and_find(query):
453452

454453
find(strategy, validate_and_find)
455454
assert all_valid
455+
456+
457+
@given(data=st.data())
458+
def test_empty_interface(data, validate_operation):
459+
# When the schema contains an empty interface (which is invalid)
460+
schema = """
461+
interface Empty
462+
463+
type First implements Empty {
464+
int: Int!
465+
}
466+
type Second implements Empty {
467+
float: Float!
468+
}
469+
470+
type Query {
471+
getByEmpty: Empty
472+
}"""
473+
# Then query generation should not fail
474+
query = data.draw(gql_st.queries(schema))
475+
# And then schema validation should fail instead
476+
with pytest.raises(TypeError, match="Type Empty must define one or more fields"):
477+
validate_operation(schema, query)

0 commit comments

Comments
 (0)