Skip to content

Commit 68d550c

Browse files
committed
schema: fix error message when provided type has no name
Replicates graphql/graphql-js@63c2009
1 parent 09cc7a2 commit 68d550c

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

src/graphql/type/schema.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,11 @@ def __init__(
230230
continue
231231

232232
type_name = getattr(named_type, "name", None)
233+
if not type_name:
234+
raise TypeError(
235+
"One of the provided types for building the Schema"
236+
" is missing a name.",
237+
)
233238
if type_name in type_map:
234239
raise TypeError(
235240
"Schema must contain uniquely named types"

tests/type/test_schema.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
GraphQLScalarType,
2424
GraphQLSchema,
2525
GraphQLString,
26+
GraphQLType,
2627
specified_directives,
2728
)
2829
from graphql.utilities import print_schema
@@ -243,7 +244,7 @@ def includes_input_types_only_used_in_directives():
243244
assert "Foo" in schema.type_map
244245
assert "Bar" in schema.type_map
245246

246-
def preserves_order_of_user_provided_types():
247+
def preserves_the_order_of_user_provided_types():
247248
a_type = GraphQLObjectType(
248249
"A", {"sub": GraphQLField(GraphQLScalarType("ASub"))}
249250
)
@@ -341,6 +342,17 @@ def rejects_a_schema_which_redefines_a_built_in_type():
341342
f" but contains multiple types named 'String'."
342343
)
343344

345+
def rejects_a_schema_when_a_provided_type_has_no_name():
346+
query = GraphQLObjectType("Query", {"foo": GraphQLField(GraphQLString)})
347+
types = [GraphQLType(), query, GraphQLType()]
348+
349+
with raises(TypeError) as exc_info:
350+
GraphQLSchema(query, types=types) # type: ignore
351+
msg = str(exc_info.value)
352+
assert msg == (
353+
"One of the provided types for building the Schema is missing a name."
354+
)
355+
344356
def rejects_a_schema_which_defines_an_object_twice():
345357
types = [
346358
GraphQLObjectType("SameName", {}),

tests/type/test_validation.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ def with_modifiers(
102102
*with_modifiers(SomeInterfaceType),
103103
]
104104

105+
not_graphql_types = [
106+
type("IntType", (int,), {"name": "IntType"}),
107+
type("FloatType", (float,), {"name": "FloatType"}),
108+
type("StringType", (str,), {"name": "StringType"}),
109+
]
110+
105111
parametrize_type = partial(
106112
mark.parametrize("type_", ids=lambda type_: type_.__class__.__name__)
107113
)
@@ -1044,7 +1050,7 @@ def rejects_a_non_output_type_as_an_object_field_type(type_):
10441050
}
10451051
]
10461052

1047-
@parametrize_type([int, float, str])
1053+
@parametrize_type(not_graphql_types)
10481054
def rejects_a_non_type_value_as_an_object_field_type(type_):
10491055
schema = _schema_with_object_field_of_type(type_)
10501056
assert validate_schema(schema) == [
@@ -1372,7 +1378,7 @@ def rejects_a_non_output_type_as_an_interface_field_type(type_):
13721378
},
13731379
]
13741380

1375-
@parametrize_type([int, float, str])
1381+
@parametrize_type(not_graphql_types)
13761382
def rejects_a_non_type_value_as_an_interface_field_type(type_):
13771383
schema = _schema_with_interface_field_of_type(type_)
13781384
assert validate_schema(schema) == [
@@ -1525,7 +1531,7 @@ def rejects_a_non_input_type_as_a_field_arg_type(type_):
15251531
},
15261532
]
15271533

1528-
@parametrize_type([int, float, str])
1534+
@parametrize_type(not_graphql_types)
15291535
def rejects_a_non_type_value_as_a_field_arg_type(type_):
15301536
schema = _schema_with_arg_of_type(type_)
15311537
assert validate_schema(schema) == [
@@ -1635,7 +1641,7 @@ def rejects_a_non_input_type_as_an_input_field_type(type_):
16351641
}
16361642
]
16371643

1638-
@parametrize_type([int, float, str])
1644+
@parametrize_type(not_graphql_types)
16391645
def rejects_a_non_type_value_as_an_input_field_type(type_):
16401646
schema = _schema_with_input_field_of_type(type_)
16411647
assert validate_schema(schema) == [

0 commit comments

Comments
 (0)