GraphQl + (Python/Django)
¿Qué es GraphQl?
http://facebook.github.io/graphql/
QUERY LANGUAGE
Query languages are computer languages used to make queries in databases and information systems
- Wikipedia
QUERY LANGUAGE
SELECT * FROM tasks
alternativa a rest
rest VS graphql
rest
- Convenciones (que no se respetan)
GRAPHQL
- Tipado por consecuencia validable
rest
GRAPHQL
rest
- El servidor decide que datos retornar
GRAPHQL
- El cliente decide que datos quiere
rest
GRAPHQL
https://mydomain.com/api/v1/tasks
//return
[
{
id: "1"
title: "vistar nicobytes.com",
completed: false,
category:{
id: "1",
title: "category 1"
}
},
{
id: "1"
title: "seguir a @nicobytes",
completed: false,
category:{
id: "1",
title: "category 2"
}
},
{
id: "1"
title: "invitar una cerveza a @nicobytes",
completed: false,
category:{
id: "1",
title: "category 2"
}
}
]
rest
GRAPHQL
query{
tasks{
title,
completed,
category{
title
}
}
}
rest
GRAPHQL
[
{
title: "vistar nicobytes.com",
completed: false,
category:{
title: "category 1"
}
},
{
title: "seguir a @nicobytes",
completed: false,
category:{
title: "category 2"
}
},
{
title: "invitar una cerveza a @nicobytes",
completed: false,
category:{
title: "category 2"
}
}
]
rest
- Envia información de más
GRAPHQL
- Obtenemos la información necesaria
rest
- Multiples requests
GRAPHQL
- Un solo request
rest
- Puede llegar a ser complejo de mantener
GRAPHQL
- Facil de mantener
graphql
Queries and Mutations
Schemas and Types
DEMO TIME
GraphQL in Python
made simple
INSTALLED_APPS = [
...
'graphene_django'
]
http://docs.graphene-python.org/projects/django/en/latest/
import graphene
from graphene_django.types import DjangoObjectType
from tasks.models import Task
class TaskType(DjangoObjectType):
class Meta:
model = Task
class TasksQuery(graphene.AbstractType):
task = graphene.Field(TaskType, id=graphene.Int())
all_tasks = graphene.List(TaskType)
def resolve_all_tasks(self, args, context, info):
return Task.objects.all()
def resolve_task(self, args, context, info):
id = args.get('id')
if id is not None:
return Task.objects.get(pk=id)
return None
from graphene import ObjectType, Schema
from django.contrib.auth.models import User
from api.schemas.user_schema import UsersQuery
from tasks.schemas.category_schema import CategoryQuery
from tasks.schemas.task_schema import TasksQuery
class Query(CategoryQuery, TasksQuery, UsersQuery, ObjectType):
# This class will inherit from multiple Queries
# as we begin to add more apps to our project
pass
schema = Schema(query=Query)
from django.conf.urls import url, include
from django.contrib import admin
from graphene_django.views import GraphQLView
from api.query import schema
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
url(r'^api/v1/', include('api.urls')),
url(r'^graphql', GraphQLView.as_view(graphiql=True, schema=schema))
]
- https://github.com/nicobytes/graphql-django
- https://github.com/nicobytes/graphql-ionic
Gracias....
Graphql
By Nicolas Molina
Graphql
Usando graphql + django
- 1,758