Skip to content

Commit f3b0f26

Browse files
authored
DSLSchema transform type attribute assert into AttributeError (#409)
1 parent 8e1c6f1 commit f3b0f26

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

gql/dsl.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ def __getattr__(self, name: str) -> "DSLType":
295295
if type_def is None:
296296
raise AttributeError(f"Type '{name}' not found in the schema!")
297297

298-
assert isinstance(type_def, (GraphQLObjectType, GraphQLInterfaceType))
298+
if not isinstance(type_def, (GraphQLObjectType, GraphQLInterfaceType)):
299+
raise AttributeError(
300+
f'Type "{name} ({type_def!r})" is not valid as an attribute of'
301+
" DSLSchema. Only Object types or Interface types are accepted."
302+
)
299303

300304
return DSLType(type_def, self)
301305

tests/starwars/test_dsl.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
NonNullTypeNode,
1515
NullValueNode,
1616
Undefined,
17+
build_ast_schema,
18+
parse,
1719
print_ast,
1820
)
1921
from graphql.utilities import get_introspection_query
@@ -774,8 +776,6 @@ def test_dsl_query_all_fields_should_correspond_to_the_root_type(ds):
774776

775777
def test_dsl_root_type_not_default():
776778

777-
from graphql import parse, build_ast_schema
778-
779779
schema_str = """
780780
schema {
781781
query: QueryNotDefault
@@ -827,6 +827,41 @@ def test_invalid_type(ds):
827827
ds.invalid_type
828828

829829

830+
def test_invalid_type_union():
831+
schema_str = """
832+
type FloatValue {
833+
floatValue: Float!
834+
}
835+
836+
type IntValue {
837+
intValue: Int!
838+
}
839+
840+
union Value = FloatValue | IntValue
841+
842+
type Entry {
843+
name: String!
844+
value: Value
845+
}
846+
847+
type Query {
848+
values: [Entry!]!
849+
}
850+
"""
851+
852+
schema = build_ast_schema(parse(schema_str))
853+
ds = DSLSchema(schema)
854+
855+
with pytest.raises(
856+
AttributeError,
857+
match=(
858+
"Type \"Value \\(<GraphQLUnionType 'Value'>\\)\" is not valid as an "
859+
"attribute of DSLSchema. Only Object types or Interface types are accepted."
860+
),
861+
):
862+
ds.Value
863+
864+
830865
def test_hero_name_query_with_typename(ds):
831866
query = """
832867
hero {

0 commit comments

Comments
 (0)