8000 Add support for django filters · graphql-python/swapi-graphene@91a1422 · GitHub
[go: up one dir, main page]

Skip to content

Commit 91a1422

Browse files
committed
Add support for django filters
1 parent 898539d commit 91a1422

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ __pycache__/
1010
# C extensions
1111
*.so
1212

13+
# Graphene schema
14+
schema.json
15+
1316
# Distribution / packaging
1417
.Python
1518
env/

requirements_base.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Django==1.8.5
2-
graphene[django]==0.4.3
2+
graphene[django]==0.5.1
33
django-graphiql==0.4.2

starwars/schema.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import graphene
66
from graphene import resolve_only_args, relay
77
from graphene.contrib.django import DjangoNode, DjangoConnection
8+
from graphene.contrib.django.filter import DjangoFilterConnectionField
9+
from graphene.contrib.django.debug import DjangoDebugPlugin
810

911
import models
1012

1113

12-
schema = graphene.Schema(name='Starwars Relay Schema')
14+
schema = graphene.Schema(name='Starwars Relay Schema', plugins=[DjangoDebugPlugin()])
1315

1416

1517
class Connection(DjangoConnection):
@@ -24,6 +26,7 @@ class Person(DjangoNode):
2426
class Meta:
2527
model = models.People
2628
exclude_fields = ('created', 'edited')
29+
filter_fields = ('name', )
2730

2831
connection_type = Connection
2932

@@ -47,8 +50,10 @@ def resolve_terrains(self):
4750
class Meta:
4851
model = models.Planet
4952
exclude_fields = ('created', 'edited', 'climate', 'terrain')
53+
filter_fields = ('name', )
5054

5155

56+
@schema.register
5257
class Film(DjangoNode):
5358
producers = graphene.String().List
5459

@@ -62,6 +67,7 @@ def resolve_producers(self):
6267
class Meta:
6368
model = models.Film
6469
exclude_fields = ('created', 'edited', 'producer')
70+
filter_fields = {'episode_id': ('gt', )}
6571

6672

6773
class Specie(DjangoNode):
@@ -127,12 +133,12 @@ class Meta:
127133

128134

129135
class Query(graphene.ObjectType):
130-
all_films = relay.ConnectionField(Film)
131-
all_species = relay.ConnectionField(Specie)
132-
all_characters = relay.ConnectionField(Person)
133-
all_vehicles = relay.ConnectionField(Vehicle)
134-
all_planets = relay.ConnectionField(Planet)
135-
all_starships = relay.ConnectionField(Starship)
136+
all_films = DjangoFilterConnectionField(Film)
137+
all_species = DjangoFilterConnectionField(Specie)
138+
all_characters = DjangoFilterConnectionField(Person)
139+
all_vehicles = DjangoFilterConnectionField(Vehicle)
140+
all_planets = DjangoFilterConnectionField(Planet)
141+
all_starships = DjangoFilterConnectionField(Starship)
136142
film = relay.NodeField(Film)
137143
specie = relay.NodeField(Specie)
138144
character = relay.NodeField(Person)

swapi_graphene/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
4040
'django_graphiql',
41+
'graphene.contrib.django',
4142
'starwars',
4243
)
4344

@@ -161,3 +162,5 @@
161162
'TEST_COLLATION': 'utf8_general_ci',
162163
}
163164
}
165+
166+
GRAPHENE_SCHEMA = 'starwars.schema'

0 commit comments

Comments
 (0)
0