Skip to content

Commit 8a0db3e

Browse files
committed
feat: Support creating query strategy from GraphQLSchema instances
1 parent 7f611ba commit 8a0db3e

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/hypothesis_graphql/_strategies/queries.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Strategies for GraphQL queries."""
22
from functools import partial
3-
from typing import Callable, List, Optional, Tuple
3+
from typing import Callable, List, Optional, Tuple, Union
44

55
import graphql
66
from graphql.pyutils import FrozenList
@@ -10,9 +10,12 @@
1010
from . import primitives
1111

1212

13-
def query(schema: str) -> st.SearchStrategy[str]:
13+
def query(schema: Union[str, graphql.GraphQLSchema]) -> st.SearchStrategy[str]:
1414
"""A strategy for generating valid queries for the given GraphQL schema."""
15-
parsed_schema = graphql.build_schema(schema)
15+
if isinstance(schema, str):
16+
parsed_schema = graphql.build_schema(schema)
17+
else:
18+
parsed_schema = schema
1619
if parsed_schema.query_type is None:
1720
raise ValueError("Query type is not defined in the schema")
1821
return fields(parsed_schema.query_type).map(make_query).map(graphql.print_ast)

test/test_queries.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ def test_query(query):
6767
assert_schema(SCHEMA + query)
6868

6969

70+
def test_query_from_graphql_schema():
71+
query = """type Query {
72+
getBooksByAuthor(name: String): [Book]
73+
}"""
74+
schema = graphql.build_schema(SCHEMA + query)
75+
assert_schema(schema)
76+
77+
7078
@pytest.mark.parametrize("notnull", (True, False))
7179
@pytest.mark.parametrize(
7280
"arguments, node_names",

0 commit comments

Comments
 (0)