什么時(shí)API
API的本質(zhì)就是幫人讀數(shù)據(jù)寸宵、寫數(shù)據(jù)崖面。流派在變,技術(shù)在變梯影,寫API巫员、用API的人的職稱也會(huì)變,但是API的本質(zhì)不會(huì)變甲棍。無論是哪種API简识,它的終極目的就是能讓人讀數(shù)據(jù)讀的輕松,寫數(shù)據(jù)寫的愉快。懂了這個(gè)七扰,就明白了GraphQL解決的是什么問題奢赂。
為什么不用REST
- Rest一次只能請(qǐng)求一個(gè)資源,會(huì)造成太多的http請(qǐng)求
- Rest的版本控制問題是一個(gè)痛
GraphQL的關(guān)鍵語句
- query:查詢颈走,相當(dāng)于GET
- mutation:變化膳灶,相當(dāng)于POST, PUT, DELETE
環(huán)境搭建
- win10
- Python3.6
- pip install django==2.0.1
- pip install graphene-django==2.0.0
創(chuàng)建項(xiàng)目
- django-admin.py startproject django_graphql
- cd django_graphql
- python startapp book
- 項(xiàng)目的文件組織
創(chuàng)建model
- book/models.py
from django.db import models
# Create your models here.
class Title(models.Model):
title = models.CharField(max_length=255, verbose_name="標(biāo)題")
def __str__(self):
return self.name
class Author(models.Model):
name = models.CharField(max_length=255, verbose_name="作者姓名")
def __str__(self):
return self.name
class Book(models.Model):
title = models.ForeignKey(Title, related_name='book_title', on_delete=models.CASCADE)
author = models.ForeignKey(Author, related_name='book_author', on_delete=models.CASCADE)
創(chuàng)建schema.py
- book/schema.py
import graphene
from graphene_django.types import DjangoObjectType
from .models import Book, Title, Author
class BookType(DjangoObjectType):
class Meta:
model = Book
class TitleType(DjangoObjectType):
class Meta:
model = Title
class AuthorType(DjangoObjectType):
class Meta:
model = Author
# 定義動(dòng)作,類似POST, PUT, DELETE
class BookInput(graphene.InputObjectType):
title = graphene.String(required=True)
author = graphene.String(required=True)
class CreateBook(graphene.Mutation):
# api的輸入?yún)?shù)
class Arguments:
book_data = BookInput(required=True)
# api的響應(yīng)參數(shù)
ok = graphene.Boolean()
book = graphene.Field(BookType)
# api的相應(yīng)操作立由,這里是create
def mutate(self, info, book_data):
title = Title.objects.create(title=book_data['title'])
author = Author.objects.create(name=book_data['author'])
book = Book.objects.create(title=title, author=author)
ok = True
return CreateBook(book=book, ok=ok)
# 定義查詢轧钓,類似GET
class Query(object):
all_books = graphene.List(BookType)
all_titles = graphene.List(TitleType)
all_authors = graphene.List(AuthorType)
def resolve_all_books(self, info, **kwargs):
# 查詢所有book的邏輯
return Book.objects.all()
def resolve_all_titles(self, info, **kwargs):
# 查詢所有title的邏輯
return Book.objects.select_related('book_title').all()
def resolve_all_authors(self, info, **kwargs):
# 查詢所有author的邏輯
return Book.objects.select_related('book_author').all()
創(chuàng)建總的schema入口文件
- 再settings.py的同級(jí)目錄下創(chuàng)建:schema.py
import graphene
import book.schema
class Query(book.schema.Query, graphene.ObjectType):
# 總的Schema的query入口
pass
class Mutations(graphene.ObjectType):
# 總的Schema的mutations入口
create_book = book.schema.CreateBook.Field()
schema = graphene.Schema(query=Query, mutation=Mutations)
配置總的url
from django.contrib import admin
from django.urls import path
from graphene_django.views import GraphQLView
from .schema import schema
urlpatterns = [
path('admin/', admin.site.urls),
path('graphql/', GraphQLView.as_view(graphiql=True, schema=schema)),
]
啟動(dòng)項(xiàng)目
- python manage.py makemigrations
- python manage.py migrate
- python manage.py runserver 0.0.0.0:8080
-
訪問:localhost:8080/graphql/
創(chuàng)建book
mutation createBook {
createBook(bookData: {title:"django-graphgl2", author: "ns2250225"}) {
book {
title {
id,
title
},
author {
id,
name
}
}
ok
}
}