|
58 | 58 | is_abstract_type,
|
59 | 59 | is_leaf_type,
|
60 | 60 | is_list_type,
|
| 61 | + is_named_type, |
61 | 62 | is_non_null_type,
|
62 | 63 | is_object_type,
|
63 | 64 | )
|
@@ -912,29 +913,55 @@ async def await_complete_object_value() -> Any:
|
912 | 913 |
|
913 | 914 | def ensure_valid_runtime_type(
|
914 | 915 | self,
|
915 |
| - runtime_type_or_name: Optional[Union[GraphQLObjectType, str]], |
| 916 | + runtime_type_or_name: Any, |
916 | 917 | return_type: GraphQLAbstractType,
|
917 | 918 | field_nodes: List[FieldNode],
|
918 | 919 | info: GraphQLResolveInfo,
|
919 | 920 | result: Any,
|
920 | 921 | ) -> 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) |
924 | 937 | else runtime_type_or_name
|
925 | 938 | )
|
926 | 939 |
|
927 |
| - if not is_object_type(runtime_type): |
| 940 | + if not isinstance(runtime_type_name, str): |
928 | 941 | raise GraphQLError(
|
929 | 942 | f"Abstract type '{return_type.name}' must resolve"
|
930 | 943 | " 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}'.", |
936 | 962 | field_nodes,
|
937 | 963 | )
|
| 964 | + |
938 | 965 | runtime_type = cast(GraphQLObjectType, runtime_type)
|
939 | 966 |
|
940 | 967 | if not self.schema.is_sub_type(return_type, runtime_type):
|
@@ -1260,15 +1287,15 @@ def default_type_resolver(
|
1260 | 1287 | append_awaitable_results(cast(Awaitable, is_type_of_result))
|
1261 | 1288 | append_awaitable_types(type_)
|
1262 | 1289 | elif is_type_of_result:
|
1263 |
| - return type_ |
| 1290 | + return type_.name |
1264 | 1291 |
|
1265 | 1292 | if awaitable_is_type_of_results:
|
1266 | 1293 | # noinspection PyShadowingNames
|
1267 | 1294 | async def get_type() -> Optional[Union[GraphQLObjectType, str]]:
|
1268 | 1295 | is_type_of_results = await gather(*awaitable_is_type_of_results)
|
1269 | 1296 | for is_type_of_result, type_ in zip(is_type_of_results, awaitable_types):
|
1270 | 1297 | if is_type_of_result:
|
1271 |
| - return type_ |
| 1298 | + return type_.name |
1272 | 1299 | return None
|
1273 | 1300 |
|
1274 | 1301 | return get_type()
|
|
0 commit comments