Skip to content

DSLSchema transform type attribute assert into AttributeError #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion gql/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
39 changes: 37 additions & 2 deletions tests/starwars/test_dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
NonNullTypeNode,
NullValueNode,
Undefined,
build_ast_schema,
parse,
print_ast,
)
from graphql.utilities import get_introspection_query
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \\(<GraphQLUnionType '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 {
Expand Down