Skip to content

Commit 83c94e6

Browse files
committed
Add isOneOf option in introspection query
Needs graphql-core>=3.3.0a7
1 parent a3a4597 commit 83c94e6

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

gql/utilities/get_introspection_query_ast.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ def get_introspection_query_ast(
1111
directive_is_repeatable: bool = False,
1212
schema_description: bool = False,
1313
input_value_deprecation: bool = True,
14+
input_object_one_of: bool = False,
15+
*,
1416
type_recursion_level: int = 7,
1517
) -> DocumentNode:
1618
"""Get a query for introspection as a document using the DSL module.
@@ -68,6 +70,13 @@ def get_introspection_query_ast(
6870
)
6971
if descriptions:
7072
fragment_FullType.select(ds.__Type.description)
73+
if input_object_one_of:
74+
try:
75+
fragment_FullType.select(ds.__Type.isOneOf)
76+
except AttributeError:
77+
raise NotImplementedError(
78+
"IsOneOf is only supported from graphql-core version 3.3.0a7"
79+
)
7180
if specified_by_url:
7281
fragment_FullType.select(ds.__Type.specifiedByURL)
7382

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
tests_requires = [
1818
"parse==1.20.2",
19+
"packaging>=21.0",
1920
"pytest==8.3.4",
2021
"pytest-asyncio==0.25.3",
2122
"pytest-console-scripts==1.4.1",

tests/starwars/test_dsl.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,15 @@
1515
NonNullTypeNode,
1616
NullValueNode,
1717
Undefined,
18+
)
19+
from graphql import __version__ as graphql_version
20+
from graphql import (
1821
build_ast_schema,
1922
parse,
2023
print_ast,
2124
)
2225
from graphql.utilities import get_introspection_query
26+
from packaging import version
2327

2428
from gql import Client, gql
2529
from gql.dsl import (
@@ -1084,6 +1088,50 @@ def test_get_introspection_query_ast(option):
10841088
)
10851089

10861090

1091+
@pytest.mark.skipif(
1092+
version.parse(graphql_version) < version.parse("3.3.0a7"),
1093+
reason="Requires graphql-core >= 3.3.0a7",
1094+
)
1095+
@pytest.mark.parametrize("option", [True, False])
1096+
def test_get_introspection_query_ast_is_one_of(option):
1097+
1098+
introspection_query = print_ast(
1099+
gql(
1100+
get_introspection_query(
1101+
input_value_deprecation=option,
1102+
)
1103+
).document
1104+
)
1105+
1106+
# Because the option does not exist yet in graphql-core,
1107+
# we add it manually here for now
1108+
if option:
1109+
introspection_query = introspection_query.replace(
1110+
"fields",
1111+
"isOneOf\n fields",
1112+
)
1113+
1114+
dsl_introspection_query = get_introspection_query_ast(
1115+
input_value_deprecation=option,
1116+
input_object_one_of=option,
1117+
type_recursion_level=9,
1118+
)
1119+
1120+
assert introspection_query == print_ast(dsl_introspection_query)
1121+
1122+
1123+
@pytest.mark.skipif(
1124+
version.parse(graphql_version) >= version.parse("3.3.0a7"),
1125+
reason="Test only for older graphql-core versions < 3.3.0a7",
1126+
)
1127+
def test_get_introspection_query_ast_is_one_of_not_implemented_yet():
1128+
1129+
with pytest.raises(NotImplementedError):
1130+
get_introspection_query_ast(
1131+
input_object_one_of=True,
1132+
)
1133+
1134+
10871135
def test_typename_aliased(ds):
10881136
query = """
10891137
hero {

0 commit comments

Comments
 (0)