Python 可扩展项目的graphql代码组织

Python 可扩展项目的graphql代码组织,python,django,graphql,code-organization,graphene-python,Python,Django,Graphql,Code Organization,Graphene Python,想想看,我们有一个大项目,有很多应用程序,导致了很多查询和变化。对于这样的项目,您如何处理graphql代码体系结构。让我们举个例子 我有一个叫accounts的应用程序。它将有与用户和配置文件相关的查询和变异。我使用的文件夹结构是,每个应用程序都会有graphql文件夹,然后会有schema.py和translations.py。现在代码是这样安排的 schema.py class User(DjangoObjectType): class Meta: model =

想想看,我们有一个大项目,有很多应用程序,导致了很多查询和变化。对于这样的项目,您如何处理graphql代码体系结构。让我们举个例子

我有一个叫accounts的应用程序。它将有与用户和配置文件相关的查询和变异。我使用的文件夹结构是,每个应用程序都会有graphql文件夹,然后会有schema.py和translations.py。现在代码是这样安排的

schema.py

class User(DjangoObjectType):

    class Meta:
        model = CustomUser
        filter_fields = {
            'email': ['exact', ],
        }
        exclude_fields = ('password', 'is_superuser', )
        interfaces = (relay.Node, )


class ProfileNode(DjangoObjectType):

    class Meta:
        model = Profile
        interfaces = (relay.Node, )


class UserQuery(object):

    user = relay.Node.Field(User)
    users = DjangoFilterConnectionField(User)  # resolve_users is not needed now


class ProfileQuery(object):

    profile = relay.Node.Field(ProfileNode)
    profiles = DjangoFilterConnectionField(ProfileNode)


class UserProfile(ObjectType):

    profile = Field(ProfileNode)

    def resolve_profile(self, info, **kwargs):
        if id is not None and info.context.user.is_authenticated:
            profile = Profile.objects.get(user=info.context.user)
            return profile
        return None


class Viewer(ObjectType):

    user = Field(User)

    def resolve_user(self, info, **kwargs):
        if info.context.user.is_authenticated:
            return info.context.user
        return None 
Y.py

class Register(graphene.Mutation):
    """
    Mutation to register a user
    """
    class Arguments:

        first_name = graphene.String(required=True)
        last_name = graphene.String(required=True)
        email = graphene.String(required=True)
        password = graphene.String(required=True)
        password_repeat = graphene.String(required=True)

    success = graphene.Boolean()
    errors = graphene.List(graphene.String)

    def mutate(self, info, first_name, last_name, email, password, password_repeat):
        # console.log('info', info, first_name, last_name, email, password)
        if password == password_repeat:
            try:
                user = CustomUser.objects.create(
                    first_name=first_name,
                    last_name=last_name,
                    email=email,
                    is_active=False
                    )
                print ('user', user)
                user.set_password(password)
                user.save()
                if djoser_settings.get('SEND_ACTIVATION_EMAIL'):
                    send_activation_email(user, info.context)
                return Register(success=bool(user.id))
            # TODO: specify exception
            except Exception:
                errors = ["email", "Email already registered."]
                return Register(success=False, errors=errors)
        errors = ["password", "Passwords don't match."]
        return Register(success=False, errors=errors)
根模式

// just to show the number of mutations just for account apps.
from accounts.graphql.mutations import (
    Activate,
    DeleteAccount,
    Login,
    RefreshToken,
    Register,
    ResetPassword,
    ResetPasswordConfirm,
    )
from accounts.graphql.schema import Viewer, UserProfile


class Mutation(company_mutation.Mutation, graphene.ObjectType):

    activate = Activate.Field()
    debug = graphene.Field(DjangoDebug, name='__debug')


class Query(company_schema.Query, graphene.ObjectType):
    viewer = graphene.Field(Viewer)
    user_profile = graphene.Field(UserProfile)
    debug = graphene.Field(DjangoDebug, name='__debug')

    @staticmethod
    def resolve_viewer(self, info, **kwargs):
        if info.context.user.is_authenticated:
            return info.context.user
        return None

    @staticmethod
    def resolve_user_profile(self, info, **kwargs):
        if info.context.user.is_authenticated and id:
            return info.context.user
        return None


schema = graphene.Schema(query=Query, mutation=Mutation)

你可以看到的突变只是帐户应用程序。在考虑所有应用程序时,会有很多变化,而且还会有更多变化。你们这些人是怎么对付这种情况的

我认为仅基于
schema.py
querys.py
translations.py
等文件的项目组织对于大型项目来说是非常糟糕的

这就像用
models.py
fields.py
utils.py
等文件组织模型零件一样

特别是,在
querys.py
文件中某处定义的GraphQL对象类型可以返回,也可以作为变异的输入

所以我更喜欢基于对象及其逻辑关系的结构

schema/
    __init__.py : contains your root schema actual code
    viewer.py : contains the ObjectType Viewer
    user/
        user.py : contains the ObjectType User and UserQuery
        profile.py : contains the ObjectType Profil
        account : contains the account management mutations
    login.py : contains the login / logout mutations
...
注意:您可以使用与查询相同的方式嵌入突变,以便能够获得突变查询,如:

mutation {
    account {
        delete(id: 12) {
            status
        }
    }
}

请看,您是否可以添加一些python代码来演示如何组织这样的突变?