diff --git a/gql/dsl.py b/gql/dsl.py index 1876742e..adc48bea 100644 --- a/gql/dsl.py +++ b/gql/dsl.py @@ -295,7 +295,11 @@ def __getattr__(self, name: str) -> "DSLType": if type_def is None: raise AttributeError(f"Type '{name}' not found in the schema!") - assert isinstance(type_def, (GraphQLObjectType, GraphQLInterfaceType)) + if not isinstance(type_def, (GraphQLObjectType, GraphQLInterfaceType)): + raise AttributeError( + f'Type "{name} ({type_def!r})" is not valid as an attribute of' + " DSLSchema. Only Object types or Interface types are accepted." + ) return DSLType(type_def, self) diff --git a/tests/starwars/test_dsl.py b/tests/starwars/test_dsl.py index 8bbdf0c9..098a2b50 100644 --- a/tests/starwars/test_dsl.py +++ b/tests/starwars/test_dsl.py @@ -14,6 +14,8 @@ NonNullTypeNode, NullValueNode, Undefined, + build_ast_schema, + parse, print_ast, ) from graphql.utilities import get_introspection_query @@ -774,8 +776,6 @@ def test_dsl_query_all_fields_should_correspond_to_the_root_type(ds): def test_dsl_root_type_not_default(): - from graphql import parse, build_ast_schema - schema_str = """ schema { query: QueryNotDefault @@ -827,6 +827,41 @@ def test_invalid_type(ds): ds.invalid_type +def test_invalid_type_union(): + schema_str = """ + type FloatValue { + floatValue: Float! + } + + type IntValue { + intValue: Int! + } + + union Value = FloatValue | IntValue + + type Entry { + name: String! + value: Value + } + + type Query { + values: [Entry!]! + } + """ + + schema = build_ast_schema(parse(schema_str)) + ds = DSLSchema(schema) + + with pytest.raises( + AttributeError, + match=( + "Type \"Value \\(\\)\" is not valid as an " + "attribute of DSLSchema. Only Object types or Interface types are accepted." + ), + ): + ds.Value + + def test_hero_name_query_with_typename(ds): query = """ hero {