Skip to content
Closed
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
7 changes: 6 additions & 1 deletion examples/starwars_relay/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ def get_node(cls, id, context, info):
return get_ship(id)


class ShipConnection(graphene.Connection):
class Meta:
node = Ship


class Faction(graphene.ObjectType):
'''A faction in the Star Wars saga'''

class Meta:
interfaces = (relay.Node, )

name = graphene.String(description='The name of the faction.')
ships = relay.ConnectionField(Ship, description='The ships used by the faction.')
ships = ShipConnection.Field(description='The ships used by the faction.')

@resolve_only_args
def resolve_ships(self, **args):
Expand Down
74 changes: 35 additions & 39 deletions graphene/relay/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import six

from graphql_relay import connection_from_list
from promise import Promise
from promise import Promise, is_thenable, promisify

from ..types import (AbstractType, Boolean, Enum, Int, Interface, List, NonNull, Scalar, String,
Union)
Expand Down Expand Up @@ -90,65 +90,61 @@ class ConnectionBase(AbstractType):


class Connection(six.with_metaclass(ConnectionMeta, ObjectType)):
pass


class IterableConnectionField(Field):

def __init__(self, type, *args, **kwargs):
kwargs.setdefault('before', String())
kwargs.setdefault('after', String())
kwargs.setdefault('first', Int())
kwargs.setdefault('last', Int())
super(IterableConnectionField, self).__init__(
type,
*args,
**kwargs
)

@property
def type(self):
type = super(IterableConnectionField, self).type
if is_node(type):
connection_type = type.Connection
else:
connection_type = type
assert issubclass(connection_type, Connection), (
'{} type have to be a subclass of Connection. Received "{}".'
).format(str(self), connection_type)
return connection_type
@classmethod
def Field(cls, *args, **kwargs): # noqa: N802
return ConnectionField(cls, *args, **kwargs)

@classmethod
def resolve_connection(cls, connection_type, args, resolved):
if isinstance(resolved, connection_type):
return resolved
def is_type_of(cls, root, context, info):
return isinstance(root, cls)

@classmethod
def connection_resolver(cls, resolved, args, context, info):
assert isinstance(resolved, Iterable), (
'Resolved value from the connection field have to be iterable or instance of {}. '
'Received "{}"'
).format(connection_type, resolved)
).format(cls, resolved)
connection = connection_from_list(
resolved,
args,
connection_type=connection_type,
edge_type=connection_type.Edge,
connection_type=cls,
edge_type=cls.Edge,
pageinfo_type=PageInfo
)
connection.iterable = resolved
return connection


class ConnectionField(Field):

def __init__(self, type, *args, **kwargs):
kwargs.setdefault('before', String())
kwargs.setdefault('after', String())
kwargs.setdefault('first', Int())
kwargs.setdefault('last', Int())
assert issubclass(type, Connection), (
'{} type have to be a subclass of Connection. Received "{}".'
).format(str(self), type)
super(ConnectionField, self).__init__(
type,
*args,
**kwargs
)

@classmethod
def connection_resolver(cls, resolver, connection_type, root, args, context, info):
resolved = resolver(root, args, context, info)

on_resolve = partial(cls.resolve_connection, connection_type, args)
if isinstance(resolved, Promise):
return resolved.then(on_resolve)
if connection_type.is_type_of(resolved, context, info):
return resolved

on_resolve = partial(connection_type.connection_resolver, args=args, context=context, info=info)
if is_thenable(resolved):
return promisify(resolved).then(on_resolve)

return on_resolve(resolved)

def get_resolver(self, parent_resolver):
resolver = super(IterableConnectionField, self).get_resolver(parent_resolver)
resolver = super(ConnectionField, self).get_resolver(parent_resolver)
return partial(self.connection_resolver, resolver, self.type)

ConnectionField = IterableConnectionField
20 changes: 0 additions & 20 deletions graphene/relay/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,6 @@ def is_node(objecttype):
return False


def get_default_connection(cls):
from .connection import Connection
assert issubclass(cls, ObjectType), (
'Can only get connection type on implemented Nodes.'
)

class Meta:
node = cls

return type('{}Connection'.format(cls.__name__), (Connection,), {'Meta': Meta})


class GlobalID(Field):

def __init__(self, node, *args, **kwargs):
Expand Down Expand Up @@ -101,11 +89,3 @@ def from_global_id(cls, global_id):
@classmethod
def to_global_id(cls, type, id):
return to_global_id(type, id)

@classmethod
def implements(cls, objecttype):
get_connection = getattr(objecttype, 'get_connection', None)
if not get_connection:
get_connection = partial(get_default_connection, objecttype)

objecttype.Connection = get_connection()
6 changes: 5 additions & 1 deletion graphene/relay/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ class Edge(BaseEdge):


def test_edge_on_node():
Edge = MyObject.Connection.Edge
class MyObjectConnection(Connection):
class Meta:
node = MyObject

Edge = MyObjectConnection.Edge
assert Edge._meta.name == 'MyObjectEdge'
edge_fields = Edge._meta.fields
assert list(edge_fields.keys()) == ['node', 'cursor']
Expand Down
17 changes: 11 additions & 6 deletions graphene/relay/tests/test_connection_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from promise import Promise

from ...types import ObjectType, Schema, String
from ..connection import ConnectionField, PageInfo
from ..connection import ConnectionField, Connection, PageInfo
from ..node import Node

letter_chars = ['A', 'B', 'C', 'D', 'E']
Expand All @@ -18,10 +18,15 @@ class Meta:
letter = String()


class LetterConnection(Connection):
class Meta:
node = Letter


class Query(ObjectType):
letters = ConnectionField(Letter)
connection_letters = ConnectionField(Letter)
promise_letters = ConnectionField(Letter)
letters = LetterConnection.Field()
connection_letters = LetterConnection.Field()
promise_letters = LetterConnection.Field()

node = Node.Field()

Expand All @@ -32,13 +37,13 @@ def resolve_promise_letters(self, args, context, info):
return Promise.resolve(list(letters.values()))

def resolve_connection_letters(self, args, context, info):
return Letter.Connection(
return LetterConnection(
page_info=PageInfo(
has_next_page=True,
has_previous_page=False
),
edges=[
Letter.Connection.Edge(
LetterConnection.Edge(
node=Letter(id=0, letter='A'),
cursor='a-cursor'
),
Expand Down
10 changes: 8 additions & 2 deletions graphene/relay/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from ...types import (AbstractType, Argument, Field, InputField,
InputObjectType, NonNull, ObjectType, Schema)
from ...types.scalars import String
from ..connection import Connection
from ..mutation import ClientIDMutation
from ..node import Node

Expand All @@ -19,6 +20,11 @@ class Meta:
name = String()


class MyNodeConnection(Connection):
class Meta:
node = MyNode


class SaySomething(ClientIDMutation):

class Input:
Expand All @@ -37,13 +43,13 @@ class Input(SharedFields):
additional_field = String()

name = String()
my_node_edge = Field(MyNode.Connection.Edge)
my_node_edge = Field(MyNodeConnection.Edge)

@classmethod
def mutate_and_get_payload(cls, args, context, info):
shared = args.get('shared', '')
additionalField = args.get('additionalField', '')
edge_type = MyNode.Connection.Edge
edge_type = MyNodeConnection.Edge
return OtherMutation(name=shared + additionalField,
my_node_edge=edge_type(
cursor='1', node=MyNode(name='name')))
Expand Down
9 changes: 0 additions & 9 deletions graphene/relay/tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ def test_node_good():
assert 'id' in MyNode._meta.fields


def test_node_get_connection():
connection = MyNode.Connection
assert issubclass(connection, Connection)


def test_node_get_connection_dont_duplicate():
assert MyNode.Connection == MyNode.Connection


def test_node_query():
executed = schema.execute(
'{ node(id:"%s") { ... on MyNode { name } } }' % to_global_id("MyNode", 1)
Expand Down
4 changes: 0 additions & 4 deletions graphene/types/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,3 @@ def resolve_type(cls, instance, context, info):

def __init__(self, *args, **kwargs):
raise Exception("An Interface cannot be intitialized")

@classmethod
def implements(cls, objecttype):
pass
4 changes: 0 additions & 4 deletions graphene/types/objecttype.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ def __new__(cls, name, bases, attrs):
)

cls = type.__new__(cls, name, bases, dict(attrs, _meta=options))

for interface in options.interfaces:
interface.implements(cls)

return cls

def __str__(cls): # noqa: N802
Expand Down