Skip to content

Commit 412df91

Browse files
committed
node_definitions: Allow to pass number as ID values
Replicates graphql/graphql-relay-js@8c0535e
1 parent e9a4883 commit 412df91

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/graphql_relay/node/node.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Callable, NamedTuple, Optional
1+
from typing import Any, Callable, NamedTuple, Optional, Union
22

33
from graphql_relay.utils.base64 import base64, unbase64
44

@@ -22,7 +22,7 @@ class GraphQLNodeDefinitions(NamedTuple):
2222

2323

2424
def node_definitions(
25-
id_fetcher: Callable[[str, GraphQLResolveInfo], Any],
25+
fetch_by_id: Callable[[str, GraphQLResolveInfo], Any],
2626
type_resolver: Optional[GraphQLTypeResolver] = None,
2727
) -> GraphQLNodeDefinitions:
2828
"""
@@ -55,7 +55,7 @@ def node_definitions(
5555
GraphQLNonNull(GraphQLID), description="The ID of an object"
5656
)
5757
},
58-
resolve=lambda _obj, info, id: id_fetcher(id, info),
58+
resolve=lambda _obj, info, id: fetch_by_id(id, info),
5959
)
6060

6161
nodes_field = GraphQLField(
@@ -67,7 +67,7 @@ def node_definitions(
6767
description="The IDs of objects",
6868
)
6969
},
70-
resolve=lambda _obj, info, ids: [id_fetcher(id_, info) for id_ in ids],
70+
resolve=lambda _obj, info, ids: [fetch_by_id(id_, info) for id_ in ids],
7171
)
7272

7373
return GraphQLNodeDefinitions(node_interface, node_field, nodes_field)
@@ -79,12 +79,12 @@ class ResolvedGlobalId(NamedTuple):
7979
id: str
8080

8181

82-
def to_global_id(type_: str, id_: str) -> str:
82+
def to_global_id(type_: str, id_: Union[str, int]) -> str:
8383
"""
8484
Takes a type name and an ID specific to that type name, and returns a
8585
"global ID" that is unique among all types.
8686
"""
87-
return base64(f"{type_}:{id_}")
87+
return base64(f"{type_}:{GraphQLID.serialize(id_)}")
8888

8989

9090
def from_global_id(global_id: str) -> ResolvedGlobalId:

0 commit comments

Comments
 (0)