8000 Original `execute` should throw if defer/stream directives are present · graphql-python/graphql-core@891586d · GitHub
[go: up one dir, main page]

Skip to content

Commit 891586d

Browse files
committed
Original execute should throw if defer/stream directives are present
Replicates graphql/graphql-js@522f495
1 parent ae91327 commit 891586d

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

src/graphql/execution/execute.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,13 @@ async def yield_subsequent_payloads(
19841984
break
19851985

19861986

1987+
UNEXPECTED_EXPERIMENTAL_DIRECTIVES = (
1988+
"The provided schema unexpectedly contains experimental directives"
1989+
" (@defer or @stream). These directives may only be utilized"
1990+
" if experimental execution features are explicitly enabled."
1991+
)
1992+
1993+
19871994
UNEXPECTED_MULTIPLE_PAYLOADS = (
19881995
"Executing this GraphQL operation would unexpectedly produce multiple payloads"
19891996
" (due to @defer or @stream directive)"
@@ -2016,10 +2023,12 @@ def execute(
20162023
20172024
This function does not support incremental delivery (`@defer` and `@stream`).
20182025
If an operation that defers or streams data is executed with this function,
2019-
it will throw or resolve to an object containing an error instead.
2020-
Use `experimental_execute_incrementally` if you want to support incremental
2021-
delivery.
2026+
it will throw an error instead. Use `experimental_execute_incrementally` if
2027+
you want to support incremental delivery.
20222028
"""
2029+
if schema.get_directive("defer") or schema.get_directive("stream"):
2030+
raise GraphQLError(UNEXPECTED_EXPERIMENTAL_DIRECTIVES)
2031+
20232032
result = experimental_execute_incrementally(
20242033
schema,
20252034
document,
@@ -2043,9 +2052,7 @@ async def await_result() -> Any:
20432052
awaited_result = await result
20442053
if isinstance(awaited_result, ExecutionResult):
20452054
return awaited_result
2046-
return ExecutionResult(
2047-
None, errors=[GraphQLError(UNEXPECTED_MULTIPLE_PAYLOADS)]
2048-
)
2055+
raise GraphQLError(UNEXPECTED_MULTIPLE_PAYLOADS)
20492056

20502057
return await_result()
20512058

tests/execution/test_defer.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -962,15 +962,10 @@ async def original_execute_function_throws_error_if_deferred_and_not_all_is_sync
962962
"""
963963
)
964964

965-
result = await execute(schema, document, {}) # type: ignore
965+
with pytest.raises(GraphQLError) as exc_info:
966+
await execute(schema, document, {}) # type: ignore
966967

967-
assert result == (
968-
None,
969-
[
970-
{
971-
"message": "Executing this GraphQL operation would unexpectedly"
972-
" produce multiple payloads"
973-
" (due to @defer or @stream directive)"
974-
}
975-
],
968+
assert str(exc_info.value) == (
969+
"Executing this GraphQL operation would unexpectedly produce"
970+
" multiple payloads (due to @defer or @stream directive)"
976971
)

tests/execution/test_executor.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from graphql.type import (
1010
GraphQLArgument,
1111
GraphQLBoolean,
12+
GraphQLDeferDirective,
1213
GraphQLField,
1314
GraphQLInt,
1415
GraphQLInterfaceType,
@@ -18,6 +19,7 @@
1819
GraphQLResolveInfo,
1920
GraphQLScalarType,
2021
GraphQLSchema,
22+
GraphQLStreamDirective,
2123
GraphQLString,
2224
GraphQLUnionType,
2325
ResponsePath,
@@ -786,6 +788,38 @@ class Data:
786788
result = execute_sync(schema, document, Data(), operation_name="S")
787789
assert result == ({"a": "b"}, None)
788790

791+
def errors_when_using_original_execute_with_schemas_including_experimental_defer():
792+
schema = GraphQLSchema(
793+
query=GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}),
794+
directives=[GraphQLDeferDirective],
795+
)
796+
document = parse("query Q { a }")
797+
798+
with pytest.raises(GraphQLError) as exc_info:
799+
execute(schema, document)
800+
801+
assert str(exc_info.value) == (
802+
"The provided schema unexpectedly contains experimental directives"
803+
" (@defer or @stream). These directives may only be utilized"
804+
" if experimental execution features are explicitly enabled."
805+
)
806+
807+
def errors_when_using_original_execute_with_schemas_including_experimental_stream():
808+
schema = GraphQLSchema(
809+
query=GraphQLObjectType("Q", {"a": GraphQLField(GraphQLString)}),
810+
directives=[GraphQLStreamDirective],
811+
)
812+
document = parse("query Q { a }")
813+
814+
with pytest.raises(GraphQLError) as exc_info:
815+
execute(schema, document)
816+
817+
assert str(exc_info.value) == (
818+
"The provided schema unexpectedly contains experimental directives"
819+
" (@defer or @stream). These directives may only be utilized"
820+
" if experimental execution features are explicitly enabled."
821+
)
822+
789823
def resolves_to_an_error_if_schema_does_not_support_operation():
790824
schema = GraphQLSchema(assume_valid=True)
791825

0 commit comments

Comments
 (0)
0