Skip to content

Commit 148575e

Browse files
author
markus
committed
Fix unicode issue for global id conversion.
1 parent c348879 commit 148575e

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

graphql_relay/node/node.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from collections import OrderedDict
22
from graphql_relay.utils import base64, unbase64
33

4+
from six import text_type
5+
46
from graphql.type import (
57
GraphQLArgument,
68
GraphQLNonNull,
@@ -52,7 +54,7 @@ def to_global_id(type, id):
5254
Takes a type name and an ID specific to that type name, and returns a
5355
"global ID" that is unique among all types.
5456
'''
55-
return base64(':'.join([type, str(id)]))
57+
return base64(':'.join([type, text_type(id)]))
5658

5759

5860
def from_global_id(global_id):

graphql_relay/node/tests/__init__.py

Whitespace-only changes.

graphql_relay/node/tests/test_node.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
14
from collections import namedtuple
25
from graphql import graphql
36
from graphql.type import (
@@ -10,7 +13,7 @@
1013
GraphQLID,
1114
)
1215

13-
from graphql_relay.node.node import node_definitions
16+
from ..node import node_definitions, to_global_id, from_global_id
1417

1518
User = namedtuple('User', ['id', 'name'])
1619
Photo = namedtuple('Photo', ['id', 'width'])
@@ -328,3 +331,25 @@ def test_has_correct_node_root_field():
328331
result = graphql(schema, query)
329332
assert not result.errors
330333
assert result.data == expected
334+
335+
336+
def test_to_global_id_converts_unicode_strings_correctly():
337+
my_unicode_id = u'ûñö'
338+
g_id = to_global_id('MyType', my_unicode_id)
339+
assert g_id == 'TXlUeXBlOsO7w7HDtg=='
340+
341+
my_unicode_id = u'\u06ED'
342+
g_id = to_global_id('MyType', my_unicode_id)
343+
assert g_id == 'TXlUeXBlOtut'
344+
345+
346+
def test_from_global_id_converts_unicode_strings_correctly():
347+
my_unicode_id = u'ûñö'
348+
my_type, my_id = from_global_id('TXlUeXBlOsO7w7HDtg==')
349+
assert my_type == 'MyType'
350+
assert my_id == my_unicode_id
351+
352+
my_unicode_id = u'\u06ED'
353+
my_type, my_id = from_global_id('TXlUeXBlOtut')
354+
assert my_type == 'MyType'
355+
assert my_id == my_unicode_id

graphql_relay/tests/test_utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def test_base64_encode_unicode_strings_correctly():
1111
my_base64 = utils.base64(my_unicode)
1212
assert my_base64 == base64.b64encode(my_unicode.encode('utf-8')).decode('utf-8')
1313

14+
my_unicode = u'\u06ED'
15+
my_base64 = utils.base64(my_unicode)
16+
assert my_base64 == base64.b64encode(my_unicode.encode('utf-8')).decode('utf-8')
17+
1418

1519
def test_base64_encode_strings_correctly():
1620
my_string = 'abc'

0 commit comments

Comments
 (0)