Skip to content

Commit 05b2320

Browse files
committed
fix: Queries missing required fields in their inputs when these fields are custom scalars
1 parent 289d79a commit 05b2320

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
**Fixed**
88

99
- Duplicated inline fragments that may miss aliases. `#69`_
10+
- Queries missing required fields in their inputs when these fields are custom scalars.
1011

1112
`0.7.1`_ - 2022-04-27
1213
---------------------

src/hypothesis_graphql/_strategies/strategy.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ def objects(
8989
self, type_: graphql.GraphQLInputObjectType, nullable: bool = True
9090
) -> st.SearchStrategy[graphql.ObjectValueNode]:
9191
"""Generate a `graphql.ObjectValueNode`."""
92-
fields = {name: field for name, field in type_.fields.items() if self.can_generate_field(field)}
92+
fields = {
93+
name: field
94+
for name, field in type_.fields.items()
95+
# Generate optional fields that are possible to generate and all required fields.
96+
# If a required field is not possible to generate, then it will fail deeper anyway
97+
if self.can_generate_field(field) or graphql.is_required_input_field(field)
98+
}
9399
strategy = subset_of_fields(fields, force_required=True).flatmap(self.lists_of_object_fields)
94100
return primitives.maybe_null(strategy.map(nodes.Object), nullable)
95101

test/test_customization.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import re
22

33
import pytest
4-
from hypothesis import given
4+
from hypothesis import find, given
55
from hypothesis import strategies as st
66
from hypothesis.errors import InvalidArgument
77

88
from hypothesis_graphql import nodes
99
from hypothesis_graphql import strategies as gql_st
10-
from hypothesis_graphql._strategies import factories
1110

1211
CUSTOM_SCALAR_TEMPLATE = """
1312
scalar Date
@@ -21,6 +20,10 @@
2120
id: String!
2221
}}
2322
23+
input RequiredQueryInput {{
24+
created: Date!
25+
}}
26+
2427
type Query {{
2528
{query}
2629
}}
@@ -62,25 +65,24 @@ def test(query):
6265
assert num_of_queries == 2
6366

6467

68+
@pytest.mark.parametrize("input_type", ("Date", "RequiredQueryInput"))
6569
@given(data=st.data())
66-
def test_custom_scalar_argument(data):
70+
def test_custom_scalar_argument(data, input_type):
6771
# When a custom scalar type is defined
6872
# And is used in an argument position
6973
# And is not nullable
7074

71-
schema = CUSTOM_SCALAR_TEMPLATE.format(query="getByDate(created: Date!): Object")
75+
schema = CUSTOM_SCALAR_TEMPLATE.format(query=f"getByDate(created: {input_type}!): Object")
7276

7377
with pytest.raises(TypeError, match="Scalar 'Date' is not supported"):
7478
data.draw(gql_st.queries(schema))
7579

7680

77-
@given(data=st.data())
7881
@pytest.mark.parametrize("other_type", ("String!", "String"))
79-
def test_custom_scalar_nested_argument(data, validate_operation, other_type):
82+
def test_custom_scalar_nested_argument(validate_operation, other_type):
8083
# When a custom scalar type is defined
8184
# And is used as a field inside an input type
8285
# And is nullable
83-
8486
schema = f"""
8587
scalar Date
8688
@@ -94,8 +96,15 @@ def test_custom_scalar_nested_argument(data, validate_operation, other_type):
9496
}}"""
9597

9698
# Then it could be skipped
97-
query = data.draw(gql_st.queries(schema))
98-
validate_operation(schema, query)
99+
strategy = gql_st.queries(schema)
100+
101+
@given(strategy)
102+
def test(query):
103+
validate_operation(schema, query)
104+
105+
test()
106+
# And "id" is still possible to generate
107+
assert find(strategy, lambda x: "id" in x).strip() == '{\n getByDate(created: {id: ""})\n}'
99108

100109

101110
@given(data=st.data())

0 commit comments

Comments
 (0)