Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 3dfa734

Browse files
committed
Externalize GraphQL Global IDs instead of internal NDB keys
1 parent 022baa4 commit 3dfa734

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

HISTORY.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
History
44
-------
55

6+
0.1.7 (TBD)
7+
---------------------
8+
9+
610
0.1.6 (2016-06-10)
711
---------------------
812
* Changing development status to Beta

graphene_gae/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
)
1313

1414
__author__ = 'Eran Kampf'
15-
__version__ = '0.1.6'
15+
__version__ = '0.1.7'
1616

1717
__all__ = [
1818
NdbObjectType,

graphene_gae/ndb/fields.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from graphene.core.exceptions import SkipField
66
from graphene.core.types.base import FieldType
77
from graphene.core.types.scalars import Boolean, Int, String
8+
from graphql_relay import to_global_id
89

910
__author__ = 'ekampf'
1011

@@ -101,11 +102,42 @@ def __init__(self, name, *args, **kwargs):
101102
def default_resolver(self, node, args, info):
102103
entity = node.instance
103104
key = getattr(entity, self.name)
105+
if not key:
106+
return None
104107

105108
if isinstance(key, list):
106-
return [k.urlsafe() for k in key]
109+
t = self.__get_key_internal_type(key[0], info.schema.graphene_schema)
110+
return [to_global_id(t.name, k.urlsafe()) for k in key]
107111

108-
return key.urlsafe() if key else None
112+
t = self.__get_key_internal_type(key, info.schema.graphene_schema)
113+
return to_global_id(t.name, key.urlsafe()) if key else None
114+
115+
def __get_key_internal_type(self, key, schema):
116+
_type = self.__find_key_object_type(key, schema)
117+
if not _type and self.parent._meta.only_fields:
118+
raise Exception(
119+
"Model %r is not accessible by the schema. "
120+
"You can either register the type manually "
121+
"using @schema.register. "
122+
"Or disable the field in %s" % (
123+
key.kind(),
124+
self.parent,
125+
)
126+
)
127+
128+
if not _type:
129+
raise SkipField()
130+
131+
return schema.T(_type)
132+
133+
def __find_key_object_type(self, key, schema):
134+
for _type in schema.types.values():
135+
type_model = hasattr(_type, '_meta') and getattr(_type._meta, 'model', None)
136+
if not type_model:
137+
continue
138+
139+
if key.kind() == type_model or key.kind() == type_model.__name__:
140+
return _type
109141

110142

111143
class NdbKeyField(FieldType):

tests/_ndb/test_types.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from graphql_relay import to_global_id
12
from tests.base_test import BaseTest
23

34
import graphene
@@ -280,7 +281,7 @@ def testQuery_keyProperty(self):
280281
query ArticleWithAuthorID {
281282
articles {
282283
headline
283-
authorKey
284+
authorId
284285
author {
285286
name, email
286287
}
@@ -293,7 +294,8 @@ def testQuery_keyProperty(self):
293294
article = dict(result.data['articles'][0])
294295
author = dict(article['author'])
295296
self.assertDictEqual(author, {'name': u'john dow', 'email': u'[email protected]'})
296-
self.assertDictContainsSubset(dict(headline='h1', authorKey=author_key.urlsafe()), article)
297+
self.assertEqual('h1', article['headline'])
298+
self.assertEqual(to_global_id('AuthorType', author_key.urlsafe()), article['authorId'])
297299

298300
def testQuery_repeatedKeyProperty(self):
299301
tk1 = Tag(name="t1").put()
@@ -302,14 +304,12 @@ def testQuery_repeatedKeyProperty(self):
302304
tk4 = Tag(name="t4").put()
303305
Article(headline="h1", summary="s1", tags=[tk1, tk2, tk3, tk4]).put()
304306

305-
print str(schema)
306-
307307
result = schema.execute('''
308308
query ArticleWithAuthorID {
309309
articles {
310310
headline
311-
authorKey
312-
tagKeys
311+
authorId
312+
tagIds
313313
tags {
314314
name
315315
}
@@ -320,7 +320,7 @@ def testQuery_repeatedKeyProperty(self):
320320
self.assertEmpty(result.errors)
321321

322322
article = dict(result.data['articles'][0])
323-
self.assertListEqual(map(lambda k: k.urlsafe(), [tk1, tk2, tk3, tk4]), article['tagKeys'])
323+
self.assertListEqual(map(lambda k: to_global_id('TagType', k.urlsafe()), [tk1, tk2, tk3, tk4]), article['tagIds'])
324324

325325
self.assertLength(article['tags'], 4)
326326
for i in range(0, 3):

tests/base_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def get_filtered_tasks(self, url=None, name=None, queue_names=None):
6161

6262
# region Extra Assertions
6363
def assertEmpty(self, l, msg=None):
64-
self.assertEqual(0, len(list(l)), msg=msg)
64+
self.assertEqual(0, len(list(l)), msg=msg or str(l))
6565

6666
def assertLength(self, l, expectation, msg=None):
6767
self.assertEqual(len(l), expectation, msg)

0 commit comments

Comments
 (0)