http://facebook.github.io/graphql/
Query languages are computer languages used to make queries in databases and information systems
- Wikipedia
SELECT * FROM tasks
- Convenciones (que no se respetan)
- Tipado por consecuencia validable
- El servidor decide que datos retornar
- El cliente decide que datos quiere
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"
}
}
]
query{
tasks{
title,
completed,
category{
title
}
}
}
[
{
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"
}
}
]
- Envia información de más
- Obtenemos la información necesaria
- Multiples requests
- Un solo request
- Puede llegar a ser complejo de mantener
- Facil de mantener
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))
]