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

Commit e166f88

Browse files
authored
Merge pull request #17 from graphql-python/feature/expose_ndb_ids
Expose NDB id in models
2 parents f354e7c + 9a15023 commit e166f88

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

HISTORY.rst

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

6+
0.1.9 (2016-08-17)
7+
---------------------
8+
* Each NdbObject now exposes an `ndbId` String field that maps to the entity's `key.id()`
9+
* Added ndb boolean argument to NdbKeyStringField so now when looking at KeyProperty we can fetch either global GraphQL id or the NDB internal id.
10+
11+
612
0.1.8 (2016-08-16)
713
---------------------
814
* Made connection_from_ndb_query resilient to random ndb timeouts

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.8'
15+
__version__ = '0.1.9'
1616

1717
__all__ = [
1818
NdbObjectType,

graphene_gae/ndb/fields.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from google.appengine.ext import ndb
22
from google.appengine.ext.db import BadArgumentError, Timeout
33

4-
from graphene import relay
4+
from graphene import relay, Argument
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
@@ -105,6 +105,11 @@ def __init__(self, name, kind, *args, **kwargs):
105105
if 'resolver' not in kwargs:
106106
kwargs['resolver'] = self.default_resolver
107107

108+
if 'ndb' not in kwargs:
109+
kwargs['ndb'] = Argument(Boolean(),
110+
description="Return an NDB id (key.id()) instead of a GraphQL global id",
111+
default=False)
112+
108113
super(NdbKeyStringField, self).__init__(*args, **kwargs)
109114

110115
def internal_type(self, schema):
@@ -141,12 +146,14 @@ def default_resolver(self, node, args, info):
141146
if not key:
142147
return None
143148

149+
is_global_id = not args.get('ndb', False)
150+
144151
if isinstance(key, list):
145152
t = self.get_object_type(info.schema.graphene_schema)._meta.type_name
146-
return [to_global_id(t, k.urlsafe()) for k in key]
153+
return [to_global_id(t, k.urlsafe()) for k in key] if is_global_id else [k.id() for k in key]
147154

148155
t = self.get_object_type(info.schema.graphene_schema)._meta.type_name
149-
return to_global_id(t, key.urlsafe()) if key else None
156+
return to_global_id(t, key.urlsafe()) if is_global_id else key.id()
150157

151158

152159
class NdbKeyField(FieldType):

graphene_gae/ndb/types.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import inspect
22
import six
33

4-
from graphene import relay
4+
from graphql.type import GraphQLString
5+
from graphene import relay, Field
56
from graphene.core.classtypes.objecttype import ObjectType, ObjectTypeMeta
67

78
from google.appengine.ext import ndb
@@ -39,6 +40,12 @@ def construct_fields(cls):
3940
for r in conversion_results:
4041
cls.add_to_class(r.name, r.field)
4142

43+
cls.add_to_class("ndb_id",
44+
Field(GraphQLString,
45+
description="ndb internal id. (key.id())",
46+
resolver=lambda self, *args, **kwargs: self.key.id())
47+
)
48+
4249
def construct(cls, *args, **kwargs):
4350
super(NdbObjectTypeMeta, cls).construct(*args, **kwargs)
4451
if not cls._meta.abstract:

tests/_ndb/test_types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,13 +297,15 @@ def testQuery_structuredProperty_repeated(self):
297297

298298
def testQuery_keyProperty(self):
299299
author_key = Author(name="john dow", email="john@dow.com").put()
300-
Article(headline="h1", summary="s1", author_key=author_key).put()
300+
article_key = Article(headline="h1", summary="s1", author_key=author_key).put()
301301

302302
result = schema.execute('''
303303
query ArticleWithAuthorID {
304304
articles {
305+
ndbId
305306
headline
306307
authorId
308+
authorNdbId: authorId(ndb: true)
307309
author {
308310
name, email
309311
}
@@ -314,6 +316,9 @@ def testQuery_keyProperty(self):
314316
self.assertEmpty(result.errors)
315317

316318
article = dict(result.data['articles'][0])
319+
self.assertEqual(article['ndbId'], str(article_key.id()))
320+
self.assertEqual(article['authorNdbId'], str(author_key.id()))
321+
317322
author = dict(article['author'])
318323
self.assertDictEqual(author, {'name': u'john dow', 'email': u'john@dow.com'})
319324
self.assertEqual('h1', article['headline'])

0 commit comments

Comments
 (0)