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

Commit

Permalink
Merge pull request #2 from ekampf/feature/ndb_key_urlsafe_support
Browse files Browse the repository at this point in the history
Use ndb.Key.urlsafe() as Graph Node IDs
  • Loading branch information
ekampf committed May 17, 2016
2 parents 82e6b5b + 92b3721 commit b69c262
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
6 changes: 0 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ To learn more check out the following `examples <examples/>`__:

* `Starwars <examples/starwars>`__

Limitations
-----------

- Does not currently support complex NDB keys (keys composed of one or more ancestors) - WIP


Contributing
------------

Expand Down
28 changes: 22 additions & 6 deletions examples/starwars/tests/test_mutation.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from tests.base_test import BaseTest

from google.appengine.ext import ndb

from graphql_relay import to_global_id

from examples.starwars.models import Ship
from examples.starwars.data import initialize
from examples.starwars.schema import schema

Expand Down Expand Up @@ -33,6 +38,7 @@ def testIntroduceShip(self):
}
}
'''

expected = {
'introduceShip': {
'ship': {
Expand All @@ -44,32 +50,32 @@ def testIntroduceShip(self):
'ships': {
'edges': [{
'node': {
'id': 'U2hpcDoz',
'id': 'U2hpcDphaEZuY21Gd2FHVnVaUzFuWVdVdGRHVnpkSElLQ3hJRVUyaHBjQmdEREE=',
'name': 'X-Wing'
}
}, {
'node': {
'id': 'U2hpcDo0',
'id': 'U2hpcDphaEZuY21Gd2FHVnVaUzFuWVdVdGRHVnpkSElLQ3hJRVUyaHBjQmdFREE=',
'name': 'Y-Wing'
}
}, {
'node': {
'id': 'U2hpcDo1',
'id': 'U2hpcDphaEZuY21Gd2FHVnVaUzFuWVdVdGRHVnpkSElLQ3hJRVUyaHBjQmdGREE=',
'name': 'A-Wing'
}
}, {
'node': {
'id': 'U2hpcDo2',
'id': 'U2hpcDphaEZuY21Gd2FHVnVaUzFuWVdVdGRHVnpkSElLQ3hJRVUyaHBjQmdHREE=',
'name': 'Millenium Falcon'
}
}, {
'node': {
'id': 'U2hpcDo3',
'id': 'U2hpcDphaEZuY21Gd2FHVnVaUzFuWVdVdGRHVnpkSElLQ3hJRVUyaHBjQmdIREE=',
'name': 'Home One'
}
}, {
'node': {
'id': 'U2hpcDoxMQ==',
'id': 'U2hpcDphaEZuY21Gd2FHVnVaUzFuWVdVdGRHVnpkSElLQ3hJRVUyaHBjQmdMREE=',
'name': 'XYZWing'
}
}]
Expand All @@ -78,5 +84,15 @@ def testIntroduceShip(self):
}
}
result = schema.execute(query)
ship_in_db = Ship.query().filter(Ship.name == 'XYZWing', Ship.faction_key == ndb.Key('Faction', 'rebels')).fetch(1)

self.assertIsNotNone(ship_in_db)
self.assertLength(ship_in_db, 1)

new_ship = ship_in_db[0]
self.assertEqual(new_ship.name, 'XYZWing')

expected['introduceShip']['ship']['id'] = to_global_id('Ship', new_ship.key.urlsafe())

self.assertFalse(result.errors, msg=str(result.errors))
self.assertDictEqual(result.data, expected)
24 changes: 16 additions & 8 deletions examples/starwars/tests/test_object_identification.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from tests.base_test import BaseTest

from google.appengine.ext import ndb

from graphql_relay import to_global_id

from examples.starwars.data import initialize
from examples.starwars.schema import schema

Expand All @@ -22,7 +26,7 @@ def test_correctly_fetches_id_name_rebels(self):
'''
expected = {
'rebels': {
'id': 'RmFjdGlvbjpyZWJlbHM=',
'id': to_global_id('Faction', ndb.Key('Faction', 'rebels').urlsafe()),
'name': 'Alliance to Restore the Republic'
}
}
Expand All @@ -31,19 +35,21 @@ def test_correctly_fetches_id_name_rebels(self):
self.assertDictEqual(result.data, expected)

def test_correctly_refetches_rebels(self):
rebels_key = to_global_id('Faction', ndb.Key('Faction', 'rebels').urlsafe())
query = '''
query RebelsRefetchQuery {
node(id: "RmFjdGlvbjpyZWJlbHM=") {
node(id: "%s") {
id
... on Faction {
name
}
}
}
'''
''' % rebels_key

expected = {
'node': {
'id': 'RmFjdGlvbjpyZWJlbHM=',
'id': rebels_key,
'name': 'Alliance to Restore the Republic'
}
}
Expand All @@ -52,6 +58,7 @@ def test_correctly_refetches_rebels(self):
self.assertDictEqual(result.data, expected)

def test_correctly_fetches_id_name_empire(self):
empire_key = to_global_id('Faction', ndb.Key('Faction', 'empire').urlsafe())
query = '''
query EmpireQuery {
empire {
Expand All @@ -62,7 +69,7 @@ def test_correctly_fetches_id_name_empire(self):
'''
expected = {
'empire': {
'id': 'RmFjdGlvbjplbXBpcmU=',
'id': empire_key,
'name': 'Galactic Empire'
}
}
Expand All @@ -71,19 +78,20 @@ def test_correctly_fetches_id_name_empire(self):
self.assertDictEqual(result.data, expected)

def test_correctly_refetches_id_name_empire(self):
empire_key = to_global_id('Faction', ndb.Key('Faction', 'empire').urlsafe())
query = '''
query EmpireRefetchQuery {
node(id: "RmFjdGlvbjplbXBpcmU=") {
node(id: "%s") {
id
... on Faction {
name
}
}
}
'''
''' % empire_key
expected = {
'node': {
'id': 'RmFjdGlvbjplbXBpcmU=',
'id': empire_key,
'name': 'Galactic Empire'
}
}
Expand Down
5 changes: 3 additions & 2 deletions graphene_gae/ndb/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ class Meta:
abstract = True

def to_global_id(self):
entity_id = self.key.id() if self.key else None
entity_id = self.key.urlsafe() if self.key else None
return self.global_id(entity_id)

@classmethod
def get_node(cls, id, info=None):
try:
instance = cls._meta.model.get_by_id(id)
key = ndb.Key(urlsafe=id)
instance = key.get()
return cls(instance)
except cls._meta.model.DoesNotExist:
return None

0 comments on commit b69c262

Please sign in to comment.