Skip to content

Commit b0f2b4d

Browse files
committed
Improved relay
1 parent cfba52e commit b0f2b4d

File tree

6 files changed

+28
-19
lines changed

6 files changed

+28
-19
lines changed

graphene/core/schema.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ def T(self, object_type):
4040
if object_type not in self._types:
4141
internal_type = object_type.internal_type(self)
4242
self._types[object_type] = internal_type
43-
name = getattr(internal_type, 'name', None)
44-
if name:
45-
self._types_names[name] = object_type
43+
is_objecttype = inspect.isclass(object_type) and issubclass(object_type, BaseObjectType)
44+
if is_objecttype:
45+
self.register(object_type)
4646
return self._types[object_type]
4747
else:
4848
return object_type
@@ -65,6 +65,10 @@ def schema(self):
6565
return GraphQLSchema(self, query=self.T(self.query), mutation=self.T(self.mutation))
6666

6767
def register(self, object_type):
68+
type_name = object_type._meta.type_name
69+
registered_object_type = self._types_names.get(type_name, None)
70+
if registered_object_type:
71+
assert registered_object_type == object_type, 'Type {} already registered with other object type'.format(type_name)
6872
self._types_names[object_type._meta.type_name] = object_type
6973
return object_type
7074

graphene/core/types/field.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,12 @@ def __init__(self, type, description=None, args=None, name=None, resolver=None,
3131
if isinstance(type, six.string_types):
3232
type = LazyType(type)
3333
self.required = required
34-
if self.required:
35-
type = NonNull(type)
3634
self.type = type
3735
self.description = description
3836
args = OrderedDict(args or {}, **kwargs)
3937
self.arguments = ArgumentsGroup(*args_list, **args)
4038
self.object_type = None
41-
self.resolver = resolver
39+
self.resolver_fn = resolver
4240
self.default = default
4341

4442
def contribute_to_class(self, cls, attname):
@@ -54,11 +52,11 @@ def contribute_to_class(self, cls, attname):
5452

5553
@property
5654
def resolver(self):
57-
return self._resolver or self.get_resolver_fn()
55+
return self.resolver_fn or self.get_resolver_fn()
5856

5957
@resolver.setter
6058
def resolver(self, value):
61-
self._resolver = value
59+
self.resolver_fn = value
6260

6361
def get_resolver_fn(self):
6462
resolve_fn_name = 'resolve_%s' % self.attname
@@ -70,6 +68,8 @@ def default_getter(instance, args, info):
7068
return default_getter
7169

7270
def get_type(self, schema):
71+
if self.required:
72+
return NonNull(self.type)
7373
return self.type
7474

7575
def internal_type(self, schema):

graphene/relay/fields.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections import Iterable
22

33
from graphene.core.fields import Field, IDField
4-
from graphene.core.types.scalars import String, ID
4+
from graphene.core.types.scalars import String, ID, Int
55
from graphql.core.type import GraphQLArgument, GraphQLID, GraphQLNonNull
66
from graphql_relay.connection.arrayconnection import connection_from_list
77
from graphql_relay.node.node import from_global_id
@@ -14,8 +14,8 @@ def __init__(self, field_type, resolver=None, description='',
1414
super(ConnectionField, self).__init__(field_type, resolver=resolver,
1515
before=String(),
1616
after=String(),
17-
first=String(),
18-
last=String(),
17+
first=Int(),
18+
last=Int(),
1919
description=description, **kwargs)
2020
self.connection_type = connection_type
2121
self.edge_type = edge_type
@@ -24,17 +24,18 @@ def wrap_resolved(self, value, instance, args, info):
2424
return value
2525

2626

27-
def resolve(self, instance, args, info):
27+
def resolver(self, instance, args, info):
2828
from graphene.relay.types import PageInfo
2929
schema = info.schema.graphene_schema
3030

31-
resolved = super(ConnectionField, self).resolve(instance, args, info)
31+
resolved = super(ConnectionField, self).resolver(instance, args, info)
3232
if resolved:
3333
resolved = self.wrap_resolved(resolved, instance, args, info)
3434
assert isinstance(
3535
resolved, Iterable), 'Resolved value from the connection field have to be iterable'
3636

37-
node = self.get_object_type(schema)
37+
type = schema.T(self.type)
38+
node = schema.objecttype(type)
3839
connection_type = self.get_connection_type(node)
3940
edge_type = self.get_edge_type(node)
4041

@@ -81,20 +82,22 @@ def id_fetcher(self, global_id, info):
8182

8283
return object_type.get_node(_id)
8384

84-
def resolve(self, instance, args, info):
85+
def resolver(self, instance, args, info):
8586
global_id = args.get('id')
8687
return self.id_fetcher(global_id, info)
8788

8889

89-
class GlobalIDField(IDField):
90+
class GlobalIDField(Field):
9091
'''The ID of an object'''
91-
required = True
92+
def __init__(self, *args, **kwargs):
93+
super(GlobalIDField, self).__init__(ID(), *args, **kwargs)
94+
self.required = True
9295

9396
def contribute_to_class(self, cls, name):
9497
from graphene.relay.utils import is_node, is_node_type
9598
in_node = is_node(cls) or is_node_type(cls)
9699
assert in_node, 'GlobalIDField could only be inside a Node, but got %r' % cls
97100
super(GlobalIDField, self).contribute_to_class(cls, name)
98101

99-
def resolve(self, instance, args, info):
102+
def resolver(self, instance, args, info):
100103
return self.object_type.to_global_id(instance, args, info)

tests/relay/test_relayfields.py renamed to graphene/relay/tests/test_query.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class MyNode(relay.Node):
1515

1616
@classmethod
1717
def get_node(cls, id):
18-
return MyNode(name='mo')
18+
return MyNode(id=id, name='mo')
1919

2020

2121
class Query(graphene.ObjectType):
@@ -35,6 +35,7 @@ def test_nodefield_query():
3535
query = '''
3636
query RebelsShipsQuery {
3737
myNode(id:"TXlOb2RlOjE=") {
38+
id
3839
name
3940
},
4041
allMyNodes (customArg:"1") {
@@ -52,6 +53,7 @@ def test_nodefield_query():
5253
'''
5354
expected = {
5455
'myNode': {
56+
'id': 'TXlOb2RlOjE=',
5557
'name': 'mo'
5658
},
5759
'allMyNodes': {
File renamed without changes.

0 commit comments

Comments
 (0)