Skip to content

Commit a005688

Browse files
committed
Improved integration with latest version of graphql-core
1 parent 417b211 commit a005688

File tree

11 files changed

+54
-46
lines changed

11 files changed

+54
-46
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ Helper functions are provided for both building the GraphQL types
4949
for connections and for implementing the `resolver` method for fields
5050
returning those types.
5151

52-
- `connectionArgs` returns the arguments that fields should provide when
52+
- `connection_args` returns the arguments that fields should provide when
5353
they return a connection type.
5454
- `connection_definitions` returns a `connection_type` and its associated
5555
`edgeType`, given a name and a node type.
5656
- `connection_from_list` is a helper method that takes an array and the
57-
arguments from `connectionArgs`, does pagination and filtering, and returns
57+
arguments from `connection_args`, does pagination and filtering, and returns
5858
an object in the shape expected by a `connection_type`'s `resolver` function.
5959
- `connection_from_promised_list` is similar to `connection_from_list`, but
6060
it takes a promise that resolves to an array, and returns a promise that
@@ -79,7 +79,7 @@ factionType = GraphQLObjectType(
7979
'ships': GraphQLField(
8080
shipConnection,
8181
description= 'The ships used by the faction.',
82-
args= connectionArgs,
82+
args= connection_args,
8383
resolver= lambda faction, args, *_: connection_from_list(
8484
map(getShip, faction.ships),
8585
args
@@ -92,7 +92,7 @@ factionType = GraphQLObjectType(
9292

9393
This shows adding a `ships` field to the `Faction` object that is a connection.
9494
It uses `connection_definitions({name: 'Ship', nodeType: shipType})` to create
95-
the connection type, adds `connectionArgs` as arguments on this function, and
95+
the connection type, adds `connection_args` as arguments on this function, and
9696
then implements the resolver function by passing the array of ships and the
9797
arguments to `connection_from_list`.
9898

README.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ Helper functions are provided for both building the GraphQL types for
5656
connections and for implementing the ``resolver`` method for fields
5757
returning those types.
5858

59-
- ``connectionArgs`` returns the arguments that fields should provide
59+
- ``connection_args`` returns the arguments that fields should provide
6060
when they return a connection type.
6161
- ``connection_definitions`` returns a ``connection_type`` and its
6262
associated ``edgeType``, given a name and a node type.
6363
- ``connection_from_list`` is a helper method that takes an array and
64-
the arguments from ``connectionArgs``, does pagination and filtering,
64+
the arguments from ``connection_args``, does pagination and filtering,
6565
and returns an object in the shape expected by a ``connection_type``'s
6666
``resolver`` function.
6767
- ``connection_from_promised_list`` is similar to
@@ -91,7 +91,7 @@ schema <tests/starwars/schema.py>`__:
9191
'ships': GraphQLField(
9292
shipConnection,
9393
description= 'The ships used by the faction.',
94-
args= connectionArgs,
94+
args= connection_args,
9595
resolver= lambda faction, args, *_: connection_from_list(
9696
map(getShip, faction.ships),
9797
args
@@ -104,7 +104,7 @@ schema <tests/starwars/schema.py>`__:
104104
This shows adding a ``ships`` field to the ``Faction`` object that is a
105105
connection. It uses
106106
``connection_definitions({name: 'Ship', nodeType: shipType})`` to create
107-
the connection type, adds ``connectionArgs`` as arguments on this
107+
the connection type, adds ``connection_args`` as arguments on this
108108
function, and then implements the resolver function by passing the array
109109
of ships and the arguments to ``connection_from_list``.
110110

graphql_relay/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from .connection.connection import (
2-
connectionArgs,
2+
connection_args,
33
connection_definitions
44
)
55
from .connection.arrayconnection import (
@@ -19,7 +19,7 @@
1919

2020
__all__ = [
2121
# Helpers for creating connection types in the schema
22-
'connectionArgs', 'connection_definitions',
22+
'connection_args', 'connection_definitions',
2323
# Helpers for creating connections from arrays
2424
'connection_from_list', 'connection_from_promised_list', 'cursor_for_object_in_connection',
2525
# Helper for creating node definitions

graphql_relay/connection/arrayconnection.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
from .connectiontypes import Connection, PageInfo, Edge
44

55

6-
def connection_from_list(data, args={}, **kwargs):
6+
def connection_from_list(data, args={}, connection_type=None,
7+
edge_type=None, pageinfo_type=None, **kwargs):
78
'''
89
A simple function that accepts an array and connection arguments, and returns
910
a connection object for use in GraphQL. It uses array offsets as pagination,
1011
so pagination will only work if the array is static.
1112
'''
13+
connection_type = connection_type or Connection
14+
edge_type = edge_type or Edge
15+
pageinfo_type = pageinfo_type or PageInfo
16+
1217
full_args = dict(args, **kwargs)
1318

1419
before = full_args.get('before')
@@ -21,7 +26,7 @@ def connection_from_list(data, args={}, **kwargs):
2126
begin = max(get_offset(after, -1), -1) + 1
2227
end = min(get_offset(before, count + 1), count)
2328
if begin >= count or begin >= end:
24-
return empty_connection()
29+
return empty_connection(connection_type, pageinfo_type)
2530

2631
# Save the pre-slice cursors
2732
first_preslice_cursor = offset_to_cursor(begin)
@@ -34,24 +39,24 @@ def connection_from_list(data, args={}, **kwargs):
3439
begin = max(end - last, begin)
3540

3641
if begin >= count or begin >= end:
37-
return empty_connection()
42+
return empty_connection(connection_type, pageinfo_type)
3843

3944
sliced_data = data[begin:end]
4045
edges = [
41-
Edge(node, cursor=offset_to_cursor(i + begin))
46+
edge_type(node=node, cursor=offset_to_cursor(i + begin))
4247
for i, node in enumerate(sliced_data)
4348
]
4449

4550
# Construct the connection
4651
first_edge = edges[0]
4752
last_edge = edges[len(edges) - 1]
48-
return Connection(
49-
edges,
50-
PageInfo(
51-
startCursor=first_edge.cursor,
52-
endCursor=last_edge.cursor,
53-
hasPreviousPage=(first_edge.cursor != first_preslice_cursor),
54-
hasNextPage=(last_edge.cursor != last_preslice_cursor)
53+
return connection_type(
54+
edges=edges,
55+
page_info=pageinfo_type(
56+
start_cursor=first_edge.cursor,
57+
end_cursor=last_edge.cursor,
58+
has_previous_page=(first_edge.cursor != first_preslice_cursor),
59+
has_next_page=(last_edge.cursor != last_preslice_cursor)
5560
)
5661
)
5762

@@ -66,17 +71,20 @@ def connection_from_promised_list(data_promise, args={}, **kwargs):
6671
# return dataPromise.then(lambda data:connection_from_list(data, args))
6772

6873

69-
def empty_connection():
74+
def empty_connection(connection_type=None, pageinfo_type=None):
7075
'''
7176
Helper to get an empty connection.
7277
'''
73-
return Connection(
74-
[],
75-
PageInfo(
76-
startCursor=None,
77-
endCursor=None,
78-
hasPreviousPage=False,
79-
hasNextPage=False,
78+
connection_type = connection_type or Connection
79+
pageinfo_type = pageinfo_type or PageInfo
80+
81+
return connection_type(
82+
edges=[],
83+
page_info=pageinfo_type(
84+
start_cursor=None,
85+
end_cursor=None,
86+
has_previous_page=False,
87+
has_next_page=False,
8088
)
8189
)
8290

graphql_relay/connection/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def __init__(self, edge_type, connection_type):
3131
self.connection_type = connection_type
3232

3333

34-
connectionArgs = {
34+
connection_args = {
3535
'before': GraphQLArgument(GraphQLString),
3636
'after': GraphQLArgument(GraphQLString),
3737
'first': GraphQLArgument(GraphQLInt),

graphql_relay/connection/connectiontypes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ def to_dict(self):
1313

1414
class PageInfo(object):
1515

16-
def __init__(self, startCursor="", endCursor="",
17-
hasPreviousPage=False, hasNextPage=False):
18-
self.startCursor = startCursor
19-
self.endCursor = endCursor
20-
self.hasPreviousPage = hasPreviousPage
21-
self.hasNextPage = hasNextPage
16+
def __init__(self, start_cursor="", end_cursor="",
17+
has_previous_page=False, has_next_page=False):
18+
self.startCursor = start_cursor
19+
self.endCursor = end_cursor
20+
self.hasPreviousPage = has_previous_page
21+
self.hasNextPage = has_next_page
2222

2323
def to_dict(self):
2424
return {

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def run_tests(self):
5454
packages=find_packages(exclude=['tests']),
5555

5656
install_requires=[
57-
'graphql-core'
57+
'graphql-core>=0.4.7a0'
5858
],
5959
tests_require=['pytest>=2.7.2'],
6060
extras_require={

tests/connection/test_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from graphql_relay.connection.arrayconnection import connection_from_list
1818
from graphql_relay.connection.connection import (
19-
connectionArgs,
19+
connection_args,
2020
connection_definitions
2121
)
2222

@@ -36,7 +36,7 @@
3636
'name': GraphQLField(GraphQLString),
3737
'friends': GraphQLField(
3838
friendConnection,
39-
args=connectionArgs,
39+
args=connection_args,
4040
resolver=lambda user, args, *
4141
_: connection_from_list(allUsers, args),
4242
),

tests/node/test_global.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@
3535

3636

3737
def get_node(global_id, *args):
38-
resolvedGlobalId = from_global_id(global_id)
39-
_type, _id = resolvedGlobalId.type, resolvedGlobalId.id
38+
resolved_global_id = from_global_id(global_id)
39+
_type, _id = resolved_global_id.type, resolved_global_id.id
4040
if _type == 'User':
4141
return userData[_id]
4242
else:
4343
return photoData[_id]
4444

4545

46-
def get_node_type(obj):
46+
def get_node_type(obj, info):
4747
if isinstance(obj, User):
4848
return userType
4949
else:

tests/node/test_node.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def get_node(id, info):
3838
return photoData[id]
3939

4040

41-
def get_node_type(obj):
41+
def get_node_type(obj, info):
4242
if obj.id in userData:
4343
return userType
4444
else:

0 commit comments

Comments
 (0)