graphene icon indicating copy to clipboard operation
graphene copied to clipboard

Please Add Further Support for FastAPI or Pydantic ObjectTypes

Open jcf-dev opened this issue 4 years ago • 1 comments

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)

jcf-dev avatar Apr 05 '21 05:04 jcf-dev

Is this what you are looking for? https://github.com/graphql-python/graphene-pydantic

Kilo59 avatar Apr 11 '21 03:04 Kilo59