01 Graphene Python Basic Tutorial
01 Graphene Python Basic Tutorial
docs.graphene-python.org
Graphene-Python
11-14 minutes
1 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
class Category(models.Model):
name = models.CharField(max_length=100)
def __str__(self):
return self.name
class Ingredient(models.Model):
name = models.CharField(max_length=100)
2 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
notes = models.TextField()
category = models.ForeignKey(
Category, related_name='ingredients',
on_delete=models.CASCADE)
def __str__(self):
return self.name
3 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
createsuperuser).
admin.site.register(Category)
admin.site.register(Ingredient)
This graph also has a root type through which all access
begins. This is the Query class below.
After we’ve done that, we will list those types as fields in the
Query class.
4 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
following:
# cookbook/ingredients/schema.py
import graphene
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
class Query(object):
all_categories =
graphene.List(CategoryType)
all_ingredients =
graphene.List(IngredientType)
5 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
import graphene
import cookbook.ingredients.schema
class Query(cookbook.ingredients.schema.Query,
graphene.ObjectType):
# This class will inherit from multiple
Queries
# as we begin to add more apps to our
project
pass
schema = graphene.Schema(query=Query)
6 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
Update settings¶
INSTALLED_APPS = [
...
# This will also make the `graphql_schema`
management command available
'graphene_django',
]
GRAPHENE = {
'SCHEMA': 'cookbook.schema.schema'
}
7 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^graphql$',
GraphQLView.as_view(graphiql=True)),
]
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^graphql$',
GraphQLView.as_view(graphiql=True,
schema=schema)),
8 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
We’re now ready to test the API we’ve built. Let’s fire up the
server from the command line.
$ python ./manage.py runserver
If you are using the provided fixtures, you will see the following
response:
{
"data": {
"allIngredients": [
{
"id": "1",
"name": "Eggs"
9 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
},
{
"id": "2",
"name": "Milk"
},
{
"id": "3",
"name": "Beef"
},
{
"id": "4",
"name": "Chicken"
}
]
}
}
Getting relations¶
Right now, with this simple setup in place, we can query for
relations too. This is where graphql becomes really powerful!
10 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
id
name
ingredients {
id
name
}
}
}
This will give you (in case you are using the fixtures) the
following result:
{
"data": {
"allCategories": [
{
"id": "1",
"name": "Dairy",
"ingredients": [
{
"id": "1",
"name": "Eggs"
},
{
"id": "2",
"name": "Milk"
}
]
},
{
"id": "2",
"name": "Meat",
11 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
"ingredients": [
{
"id": "3",
"name": "Beef"
},
{
"id": "4",
"name": "Chicken"
}
]
}
]
}
}
We can also list all ingredients and get information for the
category they are in:
query {
allIngredients {
id
name
category {
id
name
}
}
}
12 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
import graphene
class CategoryType(DjangoObjectType):
class Meta:
model = Category
class IngredientType(DjangoObjectType):
class Meta:
model = Ingredient
class Query(object):
category = graphene.Field(CategoryType,
id=graphene.Int(),
13 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
name=graphene.String())
all_categories =
graphene.List(CategoryType)
ingredient =
graphene.Field(IngredientType,
id=graphene.Int(),
name=graphene.String())
all_ingredients =
graphene.List(IngredientType)
if id is not None:
return
Category.objects.get(pk=id)
14 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
return None
if id is not None:
return
Ingredient.objects.get(pk=id)
return None
Now, with the code in place, we can query for single objects.
query {
category(id: 1) {
name
}
anotherCategory: category(name: "Dairy") {
ingredients {
15 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
id
name
}
}
}
16 of 17 23/04/2020, 2:50 pm
Graphene-Python about:reader?url=https://docs.graphene-python.org/projects/django/en/l...
Summary¶
As you can see, GraphQL is very powerful but there are a lot of
repetitions in our example. We can do a lot of improvements by
adding layers of abstraction on top of graphene-django.
17 of 17 23/04/2020, 2:50 pm