Skip to content

Commit c602d00

Browse files
committed
resolve_type: add workaround for transformed schemas
Replicates graphql/graphql-js@d6e760c
1 parent 374c777 commit c602d00

File tree

5 files changed

+122
-255
lines changed

5 files changed

+122
-255
lines changed

src/graphql/execution/execute.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
is_abstract_type,
5959
is_leaf_type,
6060
is_list_type,
61+
is_named_type,
6162
is_non_null_type,
6263
is_object_type,
6364
)
@@ -912,29 +913,55 @@ async def await_complete_object_value() -> Any:
912913

913914
def ensure_valid_runtime_type(
914915
self,
915-
runtime_type_or_name: Optional[Union[GraphQLObjectType, str]],
916+
runtime_type_or_name: Any,
916917
return_type: GraphQLAbstractType,
917918
field_nodes: List[FieldNode],
918919
info: GraphQLResolveInfo,
919920
result: Any,
920921
) -> GraphQLObjectType:
921-
runtime_type = (
922-
self.schema.get_type(runtime_type_or_name)
923-
if isinstance(runtime_type_or_name, str)
922+
if runtime_type_or_name is None:
923+
raise GraphQLError(
924+
f"Abstract type '{return_type.name}' must resolve"
925+
" to an Object type at runtime"
926+
f" for field '{info.parent_type.name}.{info.field_name}'."
927+
f" Either the '{return_type.name}' type should provide"
928+
" a 'resolve_type' function or each possible type should provide"
929+
" an 'is_type_of' function.",
930+
field_nodes,
931+
)
932+
933+
# temporary workaround until support for passing object types will be removed
934+
runtime_type_name = (
935+
runtime_type_or_name.name
936+
if is_named_type(runtime_type_or_name)
924937
else runtime_type_or_name
925938
)
926939

927-
if not is_object_type(runtime_type):
940+
if not isinstance(runtime_type_name, str):
928941
raise GraphQLError(
929942
f"Abstract type '{return_type.name}' must resolve"
930943
" to an Object type at runtime"
931-
f" for field '{info.parent_type.name}.{info.field_name}'"
932-
f" with value {inspect(result)}, received '{inspect(runtime_type)}'."
933-
f" Either the '{return_type.name}' type should provide"
934-
" a 'resolve_type' function or each possible type should"
935-
" provide an 'is_type_of' function.",
944+
f" for field '{info.parent_type.name}.{info.field_name}' with value"
945+
f" {inspect(result)}, received '{inspect(runtime_type_name)}'.",
946+
field_nodes,
947+
)
948+
949+
runtime_type = self.schema.get_type(runtime_type_name)
950+
951+
if runtime_type is None:
952+
raise GraphQLError(
953+
f"Abstract type '{return_type.name}' was resolved to a type"
954+
f" '{runtime_type_name}' that does not exist inside the schema.",
955+
field_nodes,
956+
)
957+
958+
if not is_object_type(runtime_type):
959+
raise GraphQLError(
960+
f"Abstract type '{return_type.name}' was resolved"
961+
f" to a non-object type '{runtime_type_name}'.",
936962
field_nodes,
937963
)
964+
938965
runtime_type = cast(GraphQLObjectType, runtime_type)
939966

940967
if not self.schema.is_sub_type(return_type, runtime_type):
@@ -1260,15 +1287,15 @@ def default_type_resolver(
12601287
append_awaitable_results(cast(Awaitable, is_type_of_result))
12611288
append_awaitable_types(type_)
12621289
elif is_type_of_result:
1263-
return type_
1290+
return type_.name
12641291

12651292
if awaitable_is_type_of_results:
12661293
# noinspection PyShadowingNames
12671294
async def get_type() -> Optional[Union[GraphQLObjectType, str]]:
12681295
is_type_of_results = await gather(*awaitable_is_type_of_results)
12691296
for is_type_of_result, type_ in zip(is_type_of_results, awaitable_types):
12701297
if is_type_of_result:
1271-
return type_
1298+
return type_.name
12721299
return None
12731300

12741301
return get_type()

src/graphql/type/definition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ class GraphQLResolveInfo(NamedTuple):
588588

589589
# Note: Contrary to the Javascript implementation of GraphQLTypeResolver,
590590
# the context is passed as part of the GraphQLResolveInfo:
591+
# Note: returning GraphQLObjectType is deprecated and will be removed.
591592
GraphQLTypeResolver = Callable[
592593
[Any, GraphQLResolveInfo, "GraphQLAbstractType"],
593594
AwaitableOrValue[Optional[Union["GraphQLObjectType", str]]],

0 commit comments

Comments
 (0)