Skip to content

Commit 778d94b

Browse files
committed
Add old functions back with deprecation warning
1 parent 124d4f7 commit 778d94b

File tree

3 files changed

+108
-13
lines changed

3 files changed

+108
-13
lines changed

src/graphql_relay/__init__.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
1+
"""The graphql_relay package"""
2+
3+
# The graphql-relay and graphql-relay-js version info
14
from .version import version, version_info, version_js, version_info_js
5+
6+
# Types for creating connection types in the schema
27
from .connection.connectiontypes import (
3-
Connection, ConnectionArguments, ConnectionCursor, Edge, PageInfo
4-
)
8+
Connection, ConnectionArguments, ConnectionCursor, Edge, PageInfo)
9+
10+
# Helpers for creating connection types in the schema
511
from .connection.connection import (
612
backward_connection_args, connection_args, connection_definitions,
713
forward_connection_args, GraphQLConnectionDefinitions)
14+
15+
# Helpers for creating connections from arrays
816
from .connection.arrayconnection import (
917
connection_from_array, connection_from_array_slice,
1018
cursor_for_object_in_connection,
1119
cursor_to_offset, get_offset_with_default, offset_to_cursor)
20+
21+
# Helper for creating mutations with client mutation IDs
1222
from .mutation.mutation import mutation_with_client_mutation_id
23+
24+
# Helper for creating node definitions
25+
from .node.node import node_definitions
26+
27+
# Helper for creating plural identifying root fields
28+
from .node.plural import plural_identifying_root_field
29+
30+
# Utilities for creating global IDs in systems that don't have them
1331
from .node.node import (
14-
node_definitions,
1532
from_global_id, global_id_field, to_global_id)
16-
from .node.plural import plural_identifying_root_field
33+
34+
# Deprecated functions from older graphql-relay-py versions
35+
# noinspection PyProtectedMember,PyUnresolvedReferences,PyDeprecation
36+
from .connection.arrayconnection import ( # noqa: F401
37+
connection_from_list, connection_from_list_slice)
1738

1839
__version__ = version
1940
__version_info__ = version_info
2041
__version_js__ = version_js
2142
__version_info_js__ = version_info_js
2243

2344
__all__ = [
24-
# The graphql-relay and graphql-relay-js version info
2545
'version', 'version_info', 'version_js', 'version_info_js',
26-
# Types for creating connection types in the schema
2746
'Connection', 'ConnectionArguments', 'ConnectionCursor', 'Edge', 'PageInfo',
28-
# Helpers for creating connection types in the schema
2947
'backward_connection_args', 'connection_args', 'connection_definitions',
3048
'forward_connection_args', 'GraphQLConnectionDefinitions',
31-
# Helpers for creating connections from arrays
3249
'connection_from_array', 'connection_from_array_slice',
3350
'cursor_for_object_in_connection', 'cursor_to_offset',
3451
'get_offset_with_default', 'offset_to_cursor',
35-
# Helper for creating mutations with client mutation IDs
3652
'mutation_with_client_mutation_id',
37-
# Helper for creating node definitions
3853
'node_definitions',
39-
# Helper for creating plural identifying root fields
4054
'plural_identifying_root_field',
41-
# Utilities for creating global IDs in systems that don't have them
4255
'from_global_id', 'global_id_field', 'to_global_id',
4356
]

src/graphql_relay/connection/arrayconnection.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import binascii
2+
import warnings
23
from typing import Any, Optional, Sequence
34

45
from ..utils.base64 import base64, unbase64
@@ -37,6 +38,25 @@ def connection_from_array(
3738
)
3839

3940

41+
def connection_from_list(
42+
data: Sequence, args: ConnectionArguments = None,
43+
connection_type: Any = Connection,
44+
edge_type: Any = Edge, pageinfo_type: Any = PageInfo) -> Connection:
45+
"""Deprecated alias for connection_from_array.
46+
47+
We're now using the JavaScript terminology in Python as well, since list
48+
is too narrow a type and there is no other really appropriate type name.
49+
"""
50+
warnings.warn(
51+
"connection_from_list() has been deprecated."
52+
" Please use connection_from_array() instead.",
53+
DeprecationWarning, stacklevel=2)
54+
return connection_from_array_slice(
55+
data, args,
56+
connection_type=connection_type,
57+
edge_type=edge_type, page_info_type=pageinfo_type)
58+
59+
4060
def connection_from_array_slice(
4161
array_slice: Sequence, args: ConnectionArguments = None,
4262
slice_start: int = 0, array_length: int = None, array_slice_length: int = None,
@@ -116,6 +136,28 @@ def connection_from_array_slice(
116136
)
117137

118138

139+
def connection_from_list_slice(
140+
list_slice: Sequence, args: ConnectionArguments = None,
141+
connection_type: Any = Connection,
142+
edge_type: Any = Edge, pageinfo_type: Any = PageInfo,
143+
slice_start=0, list_length=0, list_slice_length=None) -> Connection:
144+
"""Deprecated alias for connection_from_array_slice.
145+
146+
We're now using the JavaScript terminology in Python as well, since list
147+
is too narrow a type and there is no other really appropriate type name.
148+
"""
149+
warnings.warn(
150+
"connection_from_list_slice() has been deprecated."
151+
" Please use connection_from_array_slice() instead.",
152+
DeprecationWarning, stacklevel=2)
153+
return connection_from_array_slice(
154+
list_slice, args,
155+
slice_start=slice_start, array_length=list_length,
156+
array_slice_length=list_slice_length,
157+
connection_type=connection_type,
158+
edge_type=edge_type, page_info_type=pageinfo_type)
159+
160+
119161
PREFIX = 'arrayconnection:'
120162

121163

tests/connection/test_arrayconnection.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
from typing import cast, Sequence
22

3-
from pytest import raises # type: ignore
3+
from pytest import deprecated_call, raises # type: ignore
44

55
from graphql_relay import (
66
connection_from_array,
77
connection_from_array_slice,
88
cursor_for_object_in_connection,
99
Connection, Edge, PageInfo)
1010

11+
# noinspection PyProtectedMember
12+
from graphql_relay import connection_from_list, connection_from_list_slice
13+
1114

1215
def describe_connection_from_array():
1316
letters = ['A', 'B', 'C', 'D', 'E']
@@ -484,6 +487,24 @@ def __init__(
484487
assert page_info.hasPreviousPage is False
485488
assert page_info.hasNextPage is False
486489

490+
def provides_deprecated_connection_from_list():
491+
with deprecated_call():
492+
# noinspection PyDeprecation
493+
c = connection_from_list(
494+
letters[:1], args={}, connection_type=Connection,
495+
edge_type=Edge, pageinfo_type=PageInfo)
496+
assert c == Connection(
497+
edges=[
498+
Edge(node='A', cursor='YXJyYXljb25uZWN0aW9uOjA='),
499+
],
500+
pageInfo=PageInfo(
501+
startCursor='YXJyYXljb25uZWN0aW9uOjA=',
502+
endCursor='YXJyYXljb25uZWN0aW9uOjA=',
503+
hasPreviousPage=False,
504+
hasNextPage=False,
505+
)
506+
)
507+
487508

488509
def describe_connection_from_array_slice():
489510
letters = ['A', 'B', 'C', 'D', 'E']
@@ -828,3 +849,22 @@ def __init__(
828849
assert page_info.endCursor == 'YXJyYXljb25uZWN0aW9uOjA='
829850
assert page_info.hasPreviousPage is False
830851
assert page_info.hasNextPage is False
852+
853+
def provides_deprecated_connection_from_list_slice():
854+
with deprecated_call():
855+
# noinspection PyDeprecation
856+
c = connection_from_list_slice(
857+
letters[:1], args={}, connection_type=Connection,
858+
edge_type=Edge, pageinfo_type=PageInfo,
859+
slice_start=0, list_length=1, list_slice_length=1)
860+
assert c == Connection(
861+
edges=[
862+
Edge(node='A', cursor='YXJyYXljb25uZWN0aW9uOjA='),
863+
],
864+
pageInfo=PageInfo(
865+
startCursor='YXJyYXljb25uZWN0aW9uOjA=',
866+
endCursor='YXJyYXljb25uZWN0aW9uOjA=',
867+
hasPreviousPage=False,
868+
hasNextPage=False,
869+
)
870+
)

0 commit comments

Comments
 (0)