跳转到主要内容

Ariadne工具包,用于模块化定义GraphQL模式。

项目描述

Ariadne

Build Status


Ariadne GraphQL Modules

Ariadne包,用于使用模块化方法实现Ariadne GraphQL模式。

关于这项工作的原因,请参阅此GitHub讨论

请参阅API参考文件以获取文档。

安装

可以使用pip安装Ariadne GraphQL Modules

pip install ariadne-graphql-modules

需要Ariadne 0.22或更高版本才能使库正常工作。

示例

基本示例

from datetime import date

from ariadne.asgi import GraphQL
from ariadne_graphql_modules import ObjectType, gql, make_executable_schema


class Query(ObjectType):
    __schema__ = gql(
        """
        type Query {
            message: String!
            year: Int!
        }
        """
    )

    @staticmethod
    def resolve_message(*_):
        return "Hello world!"

    @staticmethod
    def resolve_year(*_):
        return date.today().year


schema = make_executable_schema(Query)
app = GraphQL(schema=schema, debug=True)

依赖注入

如果`__schema__`字符串包含其他类型,其定义应通过`__requires__`属性提供

from typing import List, Optional

from ariadne.asgi import GraphQL
from ariadne_graphql_modules import ObjectType, gql, make_executable_schema

from my_app.users import User, get_user, get_last_users


class UserType(ObjectType):
    __schema__ = gql(
        """
        type User {
            id: ID!
            name: String!
            email: String
        }
        """
    )

    @staticmethod
    def resolve_email(user: User, info):
        if info.context["is_admin"]:
            return user.email

        return None


class UsersQueries(ObjectType):
    __schema__ = gql(
        """
        type Query {
            user(id: ID!): User
            users: [User!]!
        }
        """
    )
    __requires__ = [UserType]

    @staticmethod
    def resolve_user(*_, id: string) -> Optional[User]:
        return get_user(id=id)

    @staticmethod
    def resolve_users(*_, id: string) -> List[User]:
        return get_last_users()


# UsersQueries already knows about `UserType` so it can be omitted
# in make_executable_schema arguments
schema = make_executable_schema(UsersQueries)
app = GraphQL(schema=schema, debug=True)

延迟依赖

可选地,依赖项可以声明为延迟,以便它们可以直接提供给`make_executable_schema`

from typing import List, Optional

from ariadne.asgi import GraphQL
from ariadne_graphql_modules import DeferredType, ObjectType, gql, make_executable_schema

from my_app.users import User, get_user, get_last_users


class UserType(ObjectType):
    __schema__ = gql(
        """
        type User {
            id: ID!
            name: String!
            email: String
        }
        """
    )

    @staticmethod
    def resolve_email(user: User, info):
        if info.context["is_admin"]:
            return user.email

        return None


class UsersQueries(ObjectType):
    __schema__ = gql(
        """
        type Query {
            user(id: ID!): User
            users: [User!]!
        }
        """
    )
    __requires__ = [DeferredType("User")]

    @staticmethod
    def resolve_user(*_, id: string) -> Optional[User]:
        return get_user(id=id)

    @staticmethod
    def resolve_users(*_, id: string) -> List[User]:
        return get_last_users()


schema = make_executable_schema(UserType, UsersQueries)
app = GraphQL(schema=schema, debug=True)

在`python_world`和`clientWorld`之间自动进行大小写转换

解析字段值

使用`__aliases__ = convert_case`自动设置转换大小写的字段的别名

from ariadne_graphql_modules import ObjectType, convert_case, gql


class UserType(ObjectType):
    __schema__ = gql(
        """
        type User {
            id: ID!
            fullName: String!
        }
        """
    )
    __aliases__ = convert_case

转换字段参数

在类型上使用 __fields_args__ = convert_case,以自动将字段参数转换为 resolver kwargs 中的 Python 大小写

from ariadne_graphql_modules import MutationType, convert_case, gql

from my_app import create_user


class UserRegisterMutation(MutationType):
    __schema__ = gql(
        """
        type Mutation {
            registerUser(fullName: String!, email: String!): Boolean!
        }
        """
    )
    __fields_args__ = convert_case

    @staticmethod
    async def resolve_mutation(*_, full_name: str, email: str):
        user = await create_user(
            full_name=full_name,
            email=email,
        )
        return bool(user)

转换输入字段

在类型上使用 __args__ = convert_case,以自动将输入字段转换为 resolver kwargs 中的 Python 大小写

from ariadne_graphql_modules import InputType, MutationType, convert_case, gql

from my_app import create_user


class UserRegisterInput(InputType):
    __schema__ = gql(
        """
        input UserRegisterInput {
            fullName: String!
            email: String!
        }
        """
    )
    __args__ = convert_case


class UserRegisterMutation(MutationType):
    __schema__ = gql(
        """
        type Mutation {
            registerUser(input: UserRegisterInput!): Boolean!
        }
        """
    )
    __requires__ = [UserRegisterInput]

    @staticmethod
    async def resolve_mutation(*_, input: dict):
        user = await create_user(
            full_name=input["full_name"],
            email=input["email"],
        )
        return bool(user)

根合并

通过 make_executable_schema 自动将 QueryMutationSubscription 类型合并为单个类型

from datetime import date

from ariadne.asgi import GraphQL
from ariadne_graphql_modules import ObjectType, gql, make_executable_schema


class YearQuery(ObjectType):
    __schema__ = gql(
        """
        type Query {
            year: Int!
        }
        """
    )

    @staticmethod
    def resolve_year(*_):
        return date.today().year


class MessageQuery(ObjectType):
    __schema__ = gql(
        """
        type Query {
            message: String!
        }
        """
    )

    @staticmethod
    def resolve_message(*_):
        return "Hello world!"


schema = make_executable_schema(YearQuery, MessageQuery)
app = GraphQL(schema=schema, debug=True)

最终模式将包含单个合并后的 Query 类型

type Query {
    message: String!
    year: Int!
}

最终类型上的字段将按字母顺序排序。

从 Ariadne 移动声明

Ariadne GraphQL 模块支持将旧方法和新方法结合用于模式定义。

请参阅迁移指南以获取示例和详细信息。

贡献

我们欢迎向 Ariadne GraphQL 模块贡献!如果您发现了错误或问题,请随时使用 GitHub 问题。如果您有任何问题或反馈,请通过 GitHub 讨论区告诉我们。

同时,请确保关注@AriadneGraphQL 在 Twitter 上的最新更新、新闻和随机思考!

Mirumee Software 以 ❤️ 制作 hello@mirumee.com

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源分发

ariadne_graphql_modules-0.8.0.tar.gz (12.8 kB 查看哈希值)

上传日期

构建分发

ariadne_graphql_modules-0.8.0-py2.py3-none-any.whl (23.5 kB 查看哈希值)

上传日期 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面