graphql-python / graphql-python/graphene
Please Add Further Support for FastAPI or Pydantic ObjectTypes
- Ngôn ngữ chính
- Python
- Star
- 8.2k
- Fork
- 818
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
I want to avoid repeating code fragments between the mutations and types with graphene in a FastAPI app.
Not a very polished example but please see below sample scenario.
**graphql\mutations.py**
import graphene
from models.customers import CustomerLogin, CustomerLogin_PydanticIn
from graphql.types import CustomerLoginType
class CreateUserMutation(graphene.Mutation):
class Arguments:
id = graphene.Int()
shard_id = graphene.Decimal()
seq_num = graphene.Decimal()
event_timedate = graphene.DateTime()
user_id = graphene.UUID()
device_token = graphene.UUID()
user_ip = graphene.String()
user_agent = graphene.String()
client_id = graphene.String()
process = graphene.String()
auth_result = graphene.String()
auth_fail_cause = graphene.String()
plain_email = graphene.String()
user = graphene.Field(CustomerLoginType)
@staticmethod
async def mutate(parent, info, user: CustomerLogin_PydanticIn):
user = await CustomerLogin.create(**user.dict())
return CreateUserMutation(user=user)
**graphql\types.py**
import graphene
# Fields should coincide with model.CustomerLogin
class CustomerLoginType(graphene.ObjectType):
id = graphene.Int()
shard_id = graphene.Decimal()
seq_num = graphene.Decimal()
event_timedate = graphene.DateTime()
user_id = graphene.UUID()
device_token = graphene.UUID()
user_ip = graphene.String()
user_agent = graphene.String()
client_id = graphene.String()
process = graphene.String()
auth_result = graphene.String()
auth_fail_cause = graphene.String()
plain_email = graphene.String()
**graphql\queries.py**
import graphene
from models.customers import CustomerLogin
from graphql.types import CustomerLoginType
class Query(graphene.ObjectType):
all_customer_logins = graphene.List(CustomerLoginType)
single_customer_login = graphene.Field(CustomerLoginType, id=graphene.Int())
@staticmethod
async def resolve_all_customer_logins(parent, info):
users = await CustomerLogin.all()
return users
@staticmethod
async def resolve_single_customer_login(parent, info, id):
user = await CustomerLogin.get(id=id)
return user
Then the ORM model using TortoiseORM
**models\customers.py**
from tortoise import fields
from tortoise.contrib.pydantic import pydantic_model_creator
from tortoise.models import Model
from models.events import ShardID, SequenceNum
class CustomerLogin(Model):
shard_id: fields.ForeignKeyNullableRelation[ShardID] = \
fields.ForeignKeyField(model_name='models.ShardID',
related_name='customerlogin_shardid',
to_field='shard_id',
on_delete=fields.RESTRICT,
null=True)
seq_num: fields.ForeignKeyNullableRelation[SequenceNum] = \
fields.ForeignKeyField(model_name='models.SequenceNum',
related_name='customerlogin_seqnum',
to_field='seq_num',
on_delete=fields.RESTRICT,
null=True)
event_timedate = fields.DatetimeField(null=True)
user_id = fields.UUIDField(null=True)
device_token = fields.UUIDField(null=True)
user_ip = fields.CharField(256, null=True)
user_agent = fields.CharField(1024, null=True)
client_id = fields.CharField(256, null=True)
process = fields.CharField(256, null=True)
auth_result = fields.CharField(256, null=True)
auth_fail_cause = fields.CharField(256, null=True)
plain_email = fields.CharField(256, null=True)
CustomerLogin_Pydantic = pydantic_model_creator(CustomerLogin, name='CustomerLogin')
CustomerLogin_PydanticIn = pydantic_model_creator(CustomerLogin, name='CustomerLoginIn', exclude_readonly=True)
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Hướng nghiên cứu
Ví dụ bao gồm graphql/mutations.py, graphql/types.py, graphql/queries.py và models/customers.py, sử dụng pydantic_model_creator của TortoiseORM; trước tiên hãy lần theo cách Graphene ObjectTypes và các đối số của mutation hiện được khai báo. Hãy xác định tích hợp FastAPI/Pydantic được hỗ trợ và các tiêu chí hoàn thành của nó trước khi đề xuất triển khai, vì issue không nêu một entry point mục tiêu hoặc các bài kiểm thử.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- fastapi, python
- Lĩnh vực
- api, backend, backend-api-design
- Loại issue
- Tính năng
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Cần làm rõ
- Mức phù hợp với người mới
- 20/100