Skip to content

Commit 5253786

Browse files
authored
Merge pull request #17 from graphql-python/feat-filter-by-global-id
Feature: Filter by global id
2 parents 9b214b4 + 4323e3e commit 5253786

File tree

6 files changed

+58
-4
lines changed

6 files changed

+58
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ To learn more check out the following [examples](examples/):
7575
- [ ] Filtering & Paging
7676
- [x] Default filtering enabled with all model's attributes by equal comparison (requester: [git-albertomarin](https://github.com/git-albertomarin))
7777
- [x] Take first, or last n items (credit: [alexpantyukhin](https://github.com/alexpantyukhin))
78+
- [x] Filter by global id (requester: [bwalsh](https://github.com/bwalsh))
7879
- [ ] Advanced filtering
7980
- [ ] Support more types
8081
- [x] Self-reference and list-of-self-reference relationship (requester: [mehdiym](https://github.com/mehdiym))

examples/flask_mongoengine/schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Meta:
2929
class Query(graphene.ObjectType):
3030
node = Node.Field()
3131
all_employees = MongoengineConnectionField(Employee)
32-
all_role = MongoengineConnectionField(Role)
32+
all_roles = MongoengineConnectionField(Role)
3333
role = graphene.Field(Role)
3434

3535
schema = graphene.Schema(query=Query, types=[Department, Employee, Role])

graphene_mongo/fields.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from graphene.relay import ConnectionField
88
from graphene.relay.connection import PageInfo
99
from graphql_relay.connection.arrayconnection import connection_from_list_slice
10+
from graphql_relay.node.node import from_global_id
1011
from graphene.types.argument import to_arguments
1112

1213

@@ -86,9 +87,16 @@ def fields(self):
8687
@classmethod
8788
def get_query(cls, model, info, **args):
8889
objs = model.objects()
90+
8991
if args:
9092
first = args.pop('first', None)
9193
last = args.pop('last', None)
94+
id = args.pop('id', None)
95+
96+
if id is not None:
97+
# https://github.com/graphql-python/graphene/issues/124
98+
args['pk'] = from_global_id(id)[-1]
99+
92100
objs = objs.filter(**args)
93101

94102
if first is not None:
@@ -98,6 +106,7 @@ def get_query(cls, model, info, **args):
98106

99107
return objs
100108

109+
# noqa
101110
@classmethod
102111
def merge_querysets(cls, default_queryset, queryset):
103112
return queryset & default_queryset

graphene_mongo/tests/fixtures.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
def setup_fixtures():
55
Editor.drop_collection()
6-
editor1 = Editor(first_name='Penny', last_name='Hardaway')
6+
editor1 = Editor(id='1', first_name='Penny', last_name='Hardaway')
77
editor1.save()
8-
editor2 = Editor(first_name='Grant', last_name='Hill')
8+
editor2 = Editor(id='2', first_name='Grant', last_name='Hill')
99
editor2.save()
10-
editor3 = Editor(first_name='Dennis', last_name='Rodman')
10+
editor3 = Editor(id='3', first_name='Dennis', last_name='Rodman')
1111
editor3.save()
1212

1313
Article.drop_collection()

graphene_mongo/tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
class Editor(Document):
1414

1515
meta = {'collection': 'test_editor'}
16+
id = StringField(primary_key=True)
1617
first_name = StringField(required=True)
1718
last_name = StringField(required=True)
1819

graphene_mongo/tests/test_relay_query.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class Query(graphene.ObjectType):
104104
allEditors {
105105
edges {
106106
node {
107+
id,
107108
firstName,
108109
lastName
109110
}
@@ -116,19 +117,22 @@ class Query(graphene.ObjectType):
116117
'edges': [
117118
{
118119
'node': {
120+
'id': 'RWRpdG9yTm9kZTox',
119121
'firstName': 'Penny',
120122
'lastName': 'Hardaway'
121123
}
122124
},
123125
{
124126
'node': {
127+
'id': 'RWRpdG9yTm9kZToy',
125128
'firstName': 'Grant',
126129
'lastName': 'Hill'
127130
}
128131

129132
},
130133
{
131134
'node': {
135+
'id': 'RWRpdG9yTm9kZToz',
132136
'firstName': 'Dennis',
133137
'lastName': 'Rodman'
134138
}
@@ -142,6 +146,45 @@ class Query(graphene.ObjectType):
142146
assert dict(result.data['allEditors']) == expected['allEditors']
143147

144148

149+
def test_should_filter_editors_by_id():
150+
151+
class Query(graphene.ObjectType):
152+
node = Node.Field()
153+
all_editors = MongoengineConnectionField(EditorNode)
154+
155+
query = '''
156+
query EditorQuery {
157+
allEditors(id: "RWRpdG9yTm9kZToy") {
158+
edges {
159+
node {
160+
id,
161+
firstName,
162+
lastName
163+
}
164+
}
165+
}
166+
}
167+
'''
168+
expected = {
169+
'allEditors': {
170+
'edges': [
171+
{
172+
'node': {
173+
'id': 'RWRpdG9yTm9kZToy',
174+
'firstName': 'Grant',
175+
'lastName': 'Hill'
176+
}
177+
178+
}
179+
]
180+
}
181+
}
182+
schema = graphene.Schema(query=Query)
183+
result = schema.execute(query)
184+
assert not result.errors
185+
assert dict(result.data['allEditors']) == expected['allEditors']
186+
187+
145188
def test_should_mutate():
146189

147190
class CreateArticle(graphene.Mutation):

0 commit comments

Comments
 (0)