GraphQL在Django中的使用教程
作者:Mr.Lee 发布时间:2023-02-20 18:11:31
标签:GraphQL,Django,使用
简介
特点
请求你所要的数据,不多不少
获取多个资源,只用一个请求
描述所有的可能,类型系统
几乎所有语言支持
文档
Graphene-Python
GraphQL | A query language for your API
背景
传统restful的接口定义类型多,试图简化接口定义
django中使用restframework定义restful资源接口时,可能会出现深度查询,造成有时候查询过度
例如前端用户需要查询接口用于展示在下拉框时,用户仅需要id与value值时,造成无用字段冗余,影响接口返回性能
当一张表字段较多时,例如接口1一共有40个字段,A页面需要5个字段做展示,B页面需要另外10个字段展示,这时我们需要根据用户需求定义返回接口提升性能,且数据不会被暴露
实际问题
问题
请求数据量40kB可以根据用户缩减,也就是返回数据量可以做到<40KB
后端数据实际耗时783ms,但是数据传输一共耗时5s
Django中如何使用呢
安装
安装
pip install graphene-django
django配置
INSTALLED_APPS = [
"django.contrib.staticfiles",
"graphene_django"
]
GRAPHENE = {
"SCHEMA": "test_api.schema.schema" # 下文中需要定义schema.py文件
}
Demo
定义数据库模型
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=100, help_text="名称")
id = models.BigAutoField(primary_key=True)
class Ingredient(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=100, help_text="名称")
notes = models.TextField(help_text="笔记")
category = models.ForeignKey(
Category, related_name="category", on_delete=models.CASCADE
)
def __str__(self):
return self.name
定义serializer
from graphene_django.rest_framework.mutation import SerializerMutation
from rest_framework.serializers import ModelSerializer
from ..models import Category, Ingredient
class CategorySerializer(ModelSerializer):
class Meta:
model = Category
fields = "__all__"
class IngredientSerializer(ModelSerializer):
class Meta:
model = Ingredient
fields = "__all__"
定义接口
import graphene
from graphene import relay
from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField
from graphene_django.rest_framework.mutation import SerializerMutation
from ..models import Category, Ingredient
from ..serializer import CategorySerializer, IngredientSerializer
# 为查询添加查询总数
class CountableConnectionBase(relay.Connection):
class Meta:
abstract = True
total_count = graphene.Int()
def resolve_total_count(self, info, **kwargs):
return self.iterable.count()
# Ingredient 查看过滤
class IngredientFilter(DjangoObjectType):
class Meta:
model = Ingredient
fields = "__all__"
filter_fields = {
"name": ['exact', "contains", "istartswith"],
"category": ["exact"],
'category__name': ['exact'],
}
interfaces = (relay.Node,)
connection_class = CountableConnectionBase
extra_field = graphene.String()
def resolve_extra_field(self: Ingredient, info):
return "hello!" + str(self.id)
# CategoryFilter 查询过滤
class CategoryFilter(DjangoObjectType):
class Meta:
model = Category
fields = "__all__"
filter_fields = {
"name": ['exact', "contains", "istartswith"],
}
interfaces = (relay.Node,)
connection_class = CountableConnectionBase
# CategoryMutation 修改或新增
class CategoryMutation(SerializerMutation):
class Meta:
serializer_class = CategorySerializer
# IngredientMutation 修改或新增
class IngredientMutation(SerializerMutation):
class Meta:
serializer_class = IngredientSerializer
# 汇总query接口
class ApiQuery(graphene.ObjectType):
search_category = DjangoFilterConnectionField(CategoryFilter)
search_ingredient = DjangoFilterConnectionField(IngredientFilter)
# 汇总操作类接口
class ApiMutation(graphene.ObjectType):
update_category = CategoryMutation.Field()
update_ingredient = IngredientMutation.Field()
汇总所有接口
import graphene
from .api import ApiQuery, ApiMutation
class Query(ApiQuery):
# 新增时提供多继承即可
pass
class Mutation(ApiMutation):
# 新增时提供多继承即可
pass
schema = graphene.Schema(query=Query, mutation=Mutation)
启动
python manage.py runserver 0.0.0.0:8080
接口文档
总结
查询时,可以使用django_filter , 快速查询
用法基本和drf框架基本类似
接口面涉及的深度查询,通过connection实现,如果返回字段中没有改要求,将不会深度查询
来源:https://blog.csdn.net/xzpdxz/article/details/128436581


猜你喜欢
- 脚本语言的第一行的目的就是指出,你想要你的这个文件中的代码用什么可执行程序去运行它。写法:#!/usr/bin/python是告诉操作系统执
- 本文实例讲述了php从文件夹随机读取文件的方法。分享给大家供大家参考。具体实现方法如下:function RandomFile($folde
- 1.--区分大小写select * from a where a=’AbCdE’ collate C
- 今天研究了个开源项目,数据库是mysql的,其中的脚本数据需要备份,由于本人的机器时mac pro,而且mac下的数据库连接工具都不怎么好用
- PHP bin2hex() 函数实例把 "Hello World!" 转换为十六进制值:<?php $str =
- 为什么需要线程锁当我们访问一些特殊的数据时,需要保证该数据的原子性,比如: 文章的阅读量、文章的点赞量等。我们必须要确保这些共享数据必须是原
- 今天为大家介绍一下python中与class 相关的知识……获取对象的类名python是一门面向对象的语言,对于一切接对象的pyt
- 这篇文章主要介绍了Python tkinter三种布局实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- <?php function BigEndian2Int($byte_word, $signed = false) { $int_va
- 问题描述:为了把之前的CPU版本的tensorflow卸载,换成GPU版本的tensorflow,经历了一番折腾。BUG1 Could no
- int 数字python 有3种数字类型int: 整数类型float: 浮点类型complex: 复数类型int类型#Int或整数是完整的数
- 一、采用?a=1&b=2访问修改views.py:views.pyfrom django.shortcuts import rend
- 如果你已经理解了block formatting contexts那么请继续,否则请先看看这篇文章。Overflow能够做一些很牛掰的事情,
- 加在< head>中< SCRIPT LANGUAGE="JavaScript">//more
- 求字符串中最大的递增子序列数据库环境:SQL SERVER 2005如题,求字符串“abcbklmnodfghijkmer”中最大的递增子序
- 当然这应该属于正常过滤手法,而还有一种过滤HTML标签的最终极手法,则是将一对尖括号及尖括号中的所有字符均替换不显示,该方法对于内容中必须描
- uwsgi介绍uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作
- Django的核心(1.4+)可以运行在从2.5到2.7之间的任何Python版本。我的电脑是操作系统是window10 ,内存是4G。1。
- infer 这个关键字,整理记录一下,避免后面忘记了。有点难以理解呢。inferinfer 是在 typescript 2.8中新增的关键字
- 本文实例讲述了JS数组合并push与concat区别。分享给大家供大家参考,具体如下:共同点两个方法都可以在一个数组内增添新的元素,参数都是