Skip to content

Commit fda99eb

Browse files
authored
Merge pull request #15 from graphql-python/feat-query-by-id
Feature: Query by relay global id
2 parents 0f93fb9 + 26c92a5 commit fda99eb

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

graphene_mongo/fields.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def args(self, args):
6666
@property
6767
def default_filter_args(self):
6868
def is_filterable(kv):
69-
return hasattr(kv[1], '_type') and hasattr(kv[1]._type, '_of_type')
69+
return hasattr(kv[1], '_type') \
70+
and callable(getattr(kv[1]._type, '_of_type', None))
7071

7172
return reduce(
7273
lambda r, kv: r.update({kv[0]: kv[1]._type._of_type()}) or r if is_filterable(kv) else r,

graphene_mongo/tests/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def setup_fixtures():
1717
article2.save()
1818

1919
Reporter.drop_collection()
20-
reporter = Reporter(first_name='Allen', last_name='Iverson',
20+
reporter = Reporter(id='1', first_name='Allen', last_name='Iverson',
2121
email='[email protected]', awards=['2010-mvp'])
2222
reporter.articles = [article1, article2]
2323
embedded_article1 = EmbeddedArticle(

graphene_mongo/tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class EmbeddedArticle(EmbeddedDocument):
4545
class Reporter(Document):
4646
meta = {'collection': 'test_repoter'}
4747

48+
id = StringField(primary_key=True)
4849
first_name = StringField(required=True)
4950
last_name = StringField(required=True)
5051
email = EmailField()

graphene_mongo/tests/test_relay_query.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class Mutation(graphene.ObjectType):
189189
schema = graphene.Schema(query=Query, mutation=Mutation)
190190
result = schema.execute(query)
191191
assert not result.errors
192-
# assert result.data == expected
192+
assert result.data == expected
193193

194194
def test_should_filter():
195195

@@ -231,6 +231,32 @@ class Query(graphene.ObjectType):
231231
assert result.data == expected
232232

233233

234+
def test_should_get_node_by_id():
235+
# Notes: https://goo.gl/hMNRgs
236+
class Query(graphene.ObjectType):
237+
reporter = Node.Field(ReporterNode)
238+
reporters = MongoengineConnectionField(ReporterNode)
239+
240+
query = '''
241+
query ReportersQuery {
242+
reporter (id: "UmVwb3J0ZXJOb2RlOjE=") {
243+
id,
244+
firstName
245+
}
246+
}
247+
'''
248+
expected = {
249+
'reporter': {
250+
'id': 'UmVwb3J0ZXJOb2RlOjE=',
251+
'firstName': 'Allen'
252+
}
253+
}
254+
schema = graphene.Schema(query=Query)
255+
result = schema.execute(query)
256+
assert not result.errors
257+
assert result.data == expected
258+
259+
234260
def test_should_first_n():
235261

236262
class Query(graphene.ObjectType):

graphene_mongo/types.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ def is_type_of(cls, root, info):
127127
).format(root))
128128
return isinstance(root, cls._meta.model)
129129

130-
# noqa
131130
@classmethod
132-
def get_node(cls, id, context, info):
133-
return cls._meta.model.get(id)
131+
def get_node(cls, info, id):
132+
return cls._meta.model.objects.get(pk=id)
134133

135134
def resolve_id(self, info):
136135
return str(self.id)

0 commit comments

Comments
 (0)