From d6968c2e92486dda40725c9ef99f450557003f2b Mon Sep 17 00:00:00 2001 From: Jonathan Kim Date: Wed, 20 Dec 2017 18:43:51 +0000 Subject: [PATCH] Raise better error if type is missing from schema --- graphene/types/tests/test_typemap.py | 22 +++++++++++++++++++++- graphene/types/typemap.py | 5 ++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/graphene/types/tests/test_typemap.py b/graphene/types/tests/test_typemap.py index d4170db6..c0626a1a 100644 --- a/graphene/types/tests/test_typemap.py +++ b/graphene/types/tests/test_typemap.py @@ -1,3 +1,4 @@ +import pytest from graphql.type import (GraphQLArgument, GraphQLEnumType, GraphQLEnumValue, GraphQLField, GraphQLInputObjectField, @@ -13,7 +14,7 @@ from ..inputobjecttype import InputObjectType from ..interface import Interface from ..objecttype import ObjectType from ..scalars import String, Int -from ..typemap import TypeMap +from ..typemap import TypeMap, resolve_type def test_enum(): @@ -232,3 +233,22 @@ def test_objecttype_with_possible_types(): assert graphql_type.is_type_of assert graphql_type.is_type_of({}, None) is True assert graphql_type.is_type_of(MyObjectType(), None) is False + + +def test_resolve_type_with_missing_type(): + class MyObjectType(ObjectType): + foo_bar = String() + + class MyOtherObjectType(ObjectType): + fizz_buzz = String() + + def resolve_type_func(root, info): + return MyOtherObjectType + + typemap = TypeMap([MyObjectType]) + with pytest.raises(AssertionError) as excinfo: + resolve_type( + resolve_type_func, typemap, 'MyOtherObjectType', {}, {} + ) + + assert 'MyOtherObjectTyp' in str(excinfo.value) diff --git a/graphene/types/typemap.py b/graphene/types/typemap.py index d4a4d157..b2bc4a0e 100644 --- a/graphene/types/typemap.py +++ b/graphene/types/typemap.py @@ -46,7 +46,10 @@ def resolve_type(resolve_type_func, map, type_name, root, info): if inspect.isclass(_type) and issubclass(_type, ObjectType): graphql_type = map.get(_type._meta.name) - assert graphql_type and graphql_type.graphene_type == _type, ( + assert graphql_type, "Can't find type {} in schema".format( + _type._meta.name + ) + assert graphql_type.graphene_type == _type, ( 'The type {} does not match with the associated graphene type {}.' ).format(_type, graphql_type.graphene_type) return graphql_type