Skip to content

Commit b217aef

Browse files
committed
from_global_id should work same as in relay-js (#39)
1 parent 640c908 commit b217aef

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

Diff for: src/graphql_relay/node/node.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ def from_global_id(global_id: str) -> ResolvedGlobalId:
9292
Takes the "global ID" created by to_global_id, and returns the type name and ID
9393
used to create it.
9494
"""
95-
return ResolvedGlobalId(*unbase64(global_id).split(":", 1))
95+
global_id = unbase64(global_id)
96+
if ":" not in global_id:
97+
return ResolvedGlobalId("", global_id)
98+
return ResolvedGlobalId(*global_id.split(":", 1))
9699

97100

98101
def global_id_field(

Diff for: tests/node/test_global.py

+12
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,15 @@ def allows_to_refetch_the_ids(schema):
202202
},
203203
None,
204204
)
205+
206+
def handles_valid_global_ids():
207+
assert from_global_id("Zm9v") == ("", "foo")
208+
assert from_global_id(b"Zm9v") == ("", "foo") # type: ignore
209+
assert from_global_id("Zm9vOmJhcg==") == ("foo", "bar")
210+
assert from_global_id(b"Zm9vOmJhcg==") == ("foo", "bar") # type: ignore
211+
212+
def handles_invalid_global_ids():
213+
assert from_global_id("") == ("", "")
214+
assert from_global_id("Og==") == ("", "")
215+
assert from_global_id("bad!") == ("", "")
216+
assert from_global_id("invalid") == ("", "")

0 commit comments

Comments
 (0)