Skip to content

Commit 5e0923b

Browse files
committed
Added type getter in the schema when accessing its attrs
1 parent df2900e commit 5e0923b

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

graphene/types/schema.py

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from graphql.utils.introspection_query import introspection_query
77
from graphql.utils.schema_printer import print_schema
88

9+
from .definitions import GrapheneGraphQLType
910
from .typemap import TypeMap, is_graphene_type
1011

1112

@@ -46,6 +47,20 @@ def get_mutation_type(self):
4647
def get_subscription_type(self):
4748
return self.get_graphql_type(self._subscription)
4849

50+
def __getattr__(self, type_name):
51+
'''
52+
This function let the developer select a type in a given schema
53+
by accessing its attrs.
54+
55+
Example: using schema.Query for accessing the "Query" type in the Schema
56+
'''
57+
_type = super(Schema, self).get_type(type_name)
58+
if _type is None:
59+
raise AttributeError('Type "{}" not found in the Schema'.format(type_name))
60+
if isinstance(_type, GrapheneGraphQLType):
61+
return _type.graphene_type
62+
return _type
63+
4964
def get_graphql_type(self, _type):
5065
if not _type:
5166
return _type

graphene/types/tests/test_schema.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import pytest
2+
3+
from ..schema import Schema
4+
from ..objecttype import ObjectType
5+
from ..scalars import String
6+
from ..field import Field
7+
8+
9+
class MyOtherType(ObjectType):
10+
field = String()
11+
12+
13+
class Query(ObjectType):
14+
inner = Field(MyOtherType)
15+
16+
17+
def test_schema():
18+
schema = Schema(Query)
19+
assert schema.get_query_type() == schema.get_graphql_type(Query)
20+
21+
22+
def test_schema_get_type():
23+
schema = Schema(Query)
24+
assert schema.Query == Query
25+
assert schema.MyOtherType == MyOtherType
26+
27+
28+
def test_schema_get_type_error():
29+
schema = Schema(Query)
30+
with pytest.raises(AttributeError) as exc_info:
31+
schema.X
32+
33+
assert str(exc_info.value) == 'Type "X" not found in the Schema'
34+
35+
36+
def test_schema_str():
37+
schema = Schema(Query)
38+
assert str(schema) == """schema {
39+
query: Query
40+
}
41+
42+
type MyOtherType {
43+
field: String
44+
}
45+
46+
type Query {
47+
inner: MyOtherType
48+
}
49+
"""
50+
51+
52+
def test_schema_introspect():
53+
schema = Schema(Query)
54+
assert '__schema' in schema.introspect()

graphene/types/typemap.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from ..utils.str_converters import to_camel_case
1414
from .definitions import (GrapheneEnumType, GrapheneInputObjectType,
1515
GrapheneInterfaceType, GrapheneObjectType,
16-
GrapheneScalarType, GrapheneUnionType)
16+
GrapheneScalarType, GrapheneUnionType,
17+
GrapheneGraphQLType)
1718
from .dynamic import Dynamic
1819
from .enum import Enum
1920
from .field import Field
@@ -68,7 +69,7 @@ def graphene_reducer(self, map, type):
6869
return self.reducer(map, type.of_type)
6970
if type._meta.name in map:
7071
_type = map[type._meta.name]
71-
if is_graphene_type(_type):
72+
if isinstance(_type, GrapheneGraphQLType):
7273
assert _type.graphene_type == type
7374
return map
7475
if issubclass(type, ObjectType):
@@ -127,7 +128,7 @@ def construct_enum(self, map, type):
127128
def construct_objecttype(self, map, type):
128129
if type._meta.name in map:
129130
_type = map[type._meta.name]
130-
if is_graphene_type(_type):
131+
if isinstance(_type, GrapheneGraphQLType):
131132
assert _type.graphene_type == type
132133
return map
133134
map[type._meta.name] = GrapheneObjectType(

0 commit comments

Comments
 (0)