Strawberry GraphQL Django扩展
项目描述
Strawberry GraphQL Django集成
此软件包提供强大的工具,可从Django模型生成GraphQL类型、查询、突变和解析器。
从python软件包仓库安装strawberry-graphql-django
软件包。
pip install strawberry-graphql-django
支持的功能
- 从模型生成GraphQL类型
- 过滤、分页和排序
- 基本创建、检索、更新和删除(CRUD)类型和突变
- 基本Django认证支持,当前用户查询、登录和注销突变
- Django同步和异步视图
- 使用Django的权限系统扩展权限
- Relay支持,自动生成解析器
- 查询优化,提高性能并避免常见陷阱(例如n+1)
- 与GraphiQL集成的调试工具栏,用于显示SQL查询等指标
- 单元测试集成
基本用法
# models.py
from django.db import models
class Fruit(models.Model):
"""A tasty treat"""
name = models.CharField(
max_length=20,
)
color = models.ForeignKey(
"Color",
on_delete=models.CASCADE,
related_name="fruits",
blank=True,
null=True,
)
class Color(models.Model):
name = models.CharField(
max_length=20,
help_text="field description",
)
# types.py
import strawberry_django
from strawberry import auto
from . import models
@strawberry_django.type(models.Fruit)
class Fruit:
id: auto
name: auto
color: 'Color'
@strawberry_django.type(models.Color)
class Color:
id: auto
name: auto
fruits: list[Fruit]
# schema.py
import strawberry
import strawberry_django
from strawberry_django.optimizer import DjangoOptimizerExtension
from .types import Fruit
@strawberry.type
class Query:
fruits: list[Fruit] = strawberry_django.field()
schema = strawberry.Schema(
query=Query,
extensions=[
DjangoOptimizerExtension, # not required, but highly recommended
],
)
# urls.py
from django.urls import include, path
from strawberry.django.views import AsyncGraphQLView
from .schema import schema
urlpatterns = [
path('graphql', AsyncGraphQLView.as_view(schema=schema)),
]
上面的代码生成以下模式。
"""
A tasty treat
"""
type Fruit {
id: ID!
name: String!
color: Color
}
type Color {
id: ID!
"""
field description
"""
name: String!
fruits: [Fruit!]
}
type Query {
fruits: [Fruit!]!
}
项目详情
下载文件
下载适合您平台文件的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
strawberry_graphql_django-0.48.0.tar.gz (73.5 kB 查看哈希值)
构建分发
关闭
strawberry_graphql_django-0.48.0.tar.gz 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 332065abbaed2e413155345904e468eceab50978c2a44926f209c6f038c79df8 |
|
MD5 | aa440c778f96883e460c209aa72fa308 |
|
BLAKE2b-256 | 87f1705ef073cc702f412dd9f8cef41433a17f71b63959f1e4c7e0a00731676e |
关闭
strawberry_graphql_django-0.48.0-py3-none-any.whl 的散列值
算法 | 散列摘要 | |
---|---|---|
SHA256 | 1b0b51737cddc23d9f524148b4bf4a642944135bd36f8780c66a7f47f2e15ada |
|
MD5 | f2936fece202f675e0009b3cfdae1243 |
|
BLAKE2b-256 | 15f96332883b069c28c99fe0540a76231713d1544443748ab8ea1bb6ed486165 |