python manage.py startapp blog
# Taken From http://docs.wagtail.io/en/v1.9/getting_started/tutorial.html from __future__ import unicode_literals from django.db import models # Create your models here. from wagtail.wagtailcore.models import Page from wagtail.wagtailcore.fields import RichTextField from wagtail.wagtailadmin.edit_handlers import FieldPanel from wagtail.wagtailsearch import index class BlogIndexPage(Page): intro = RichTextField(blank=True) content_panels = Page.content_panels + [ FieldPanel('intro', classname="full") ] class BlogPage(Page): date = models.DateField("Post date") intro = models.CharField(max_length=250) body = RichTextField(blank=True) search_fields = Page.search_fields + [ index.SearchField('intro'), index.SearchField('body'), ] content_panels = Page.content_panels + [ FieldPanel('date'), FieldPanel('intro'), FieldPanel('body', classname="full"), ]
pip install "graphene-django==1.2"
GRAPHENE = { 'SCHEMA': 'api.schema.schema', }
from django.apps import AppConfig class ApiConfig(AppConfig): name = 'api'
from __future__ import unicode_literals import graphene from graphene_django import DjangoObjectType from blog.models import BlogPage from django.db import models class ArticleNode(DjangoObjectType): class Meta: model = BlogPage only_fields = ['id', 'title', 'date', 'intro', 'body'] class Query(graphene.ObjectType): articles = graphene.List(ArticleNode) @graphene.resolve_only_args def resolve_articles(self): return BlogPage.objects.live() schema = graphene.Schema(query=Query)
from django.views.decorators.csrf import csrf_exempt from graphene_django.views import GraphQLView
url(r'^api/graphql', csrf_exempt(GraphQLView.as_view())), url(r'^api/graphiql', csrf_exempt(GraphQLView.as_view(graphiql=True, pretty=True))),
INSTALLED_APPS = ( # ... 'api', 'blog', 'graphene_django', )
python manage.py makemigrations python manage.py migrate
python manage.py runserver
query articles { articles { id title date intro body } }
Source: https://habr.com/ru/post/335128/