石墨烯中的元子类是什么?

Pay*_*ian 6 python sqlalchemy flask flask-graphql graphene-python

我正在学习如何使用 GraphQL 和 python。我找到了graphene项目及其 SQLAlchemy 和 Flask 扩展。我一直在阅读教程和文档,但无法弄清楚class Meta定义模式时的用途。我目前正在关注教程。我用谷歌搜索了一下,似乎找不到任何东西。

这是教程中的一些代码。我已经对令我困惑的行发表了评论。

从 graphene_sqlalchemy 导入 SQLAlchemyObjectType
从数据库.model_people 导入 ModelPeople
进口石墨烯


# 创建一个通用类来共同描述查询和突变的人员属性
People属性类:
    name = graphene.String(description="此人的姓名。")
    height = graphene.String(description="人的身高。")
    mass = graphene.String(description="人的质量。")
    Hair_color = graphene.String(description="人的头发颜色。")
    Skin_color = graphene.String(description="人的肤色。")
    eye_color = graphene.String(description="人的眼睛颜色。")
    birth_year = graphene.String(description="此人的出生年份。")
    性别 = graphene.String(description="该人的性别。")
    Planet_id = graphene.ID(description="该人来自的星球的全局 ID。")
    url = graphene.String(description="星球大战 API 中人物的 URL。")


People 类(SQLAlchemyObjectType,PeopleAttribute):
    “”“人员节点。”“”

    # ---------- 这个类是用来做什么的?Flask + 石墨烯 + sqlalchemy 生态系统的哪一部分使用了它?
    类元:
        模特 = 模特人物
        接口 = (graphene.relay.Node,)

Kev*_*sko 7

我和你在同一条船上。对你来说可能有点晚了,但对于其他遇到同样问题的人来说,这是我在学习 GraphQL 和 Graphene 时发现的。

\n\n

Meta我也对子类感到困惑。这就是文档的说明。

\n\n
\n

Graphene 使用 ObjectType 上的 Meta 内部类来设置不同的选项。

\n
\n\n

https://docs.graphene-python.org/en/latest/types/objecttypes/#objecttype-configuration-meta-class

\n\n

Meta 类本质上允许您更改/修改类的属性。例如,您可以通过设置name = <short name of class>.

\n\n
\n

默认情况下,GraphQL 模式中的类型名称将与定义 ObjectType 的类名称相同。这可以通过设置 Meta 类的 name 属性来更改:

\n
\n\n

他们使用的例子是这样的..

\n\n
from graphene import ObjectType\n\nclass MyGraphQlSong(ObjectType):\n    class Meta:\n        name = \'Song\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,您可以通过“Song”查询“MyGraphQlSong”,而不必查询“MyGraphQlSong”。

\n\n

您还可以添加其他内容,例如描述以及父类应该继承的接口(所有内容都在上面的链接中进行了描述)。

\n\n

您可以更改/修改的属性的完整列表位于 API 参考中(这是特定于 ObjectType 的。其他类型具有其他元类选项。此处有更详细的解释。https: //docs.graphene-python.org /en/最新/api/

\n\n
Meta class options (optional):\n\n    name (str): Name of the GraphQL type (must be unique in schema). Defaults to class\n        name.\n    description (str): Description of the GraphQL type in the schema. Defaults to class\n        docstring.\n    interfaces (Iterable[graphene.Interface]): GraphQL interfaces to extend with this object.\n        all fields from interface will be included in this object\xe2\x80\x99s schema.\n    possible_types (Iterable[class]): Used to test parent value object via isintance to see if\n        this type can be used to resolve an ambigous type (interface, union).\n    default_resolver (any Callable resolver): Override the default resolver for this\n        type. Defaults to graphene default resolver which returns an attribute or dictionary key with the same name as the field.\n    fields (Dict[str, graphene.Field]): Dictionary of field name to Field. Not recommended to\n        use (prefer class attributes).\n
Run Code Online (Sandbox Code Playgroud)\n\n

我在查找 SQLAlchemy 示例中“model = ...”的来源时遇到问题。我找不到任何引用“model”含义的文档,但我的猜测是,sinceSQLAlchemyObjectType是 的子类ObjectTypeSQLAlchemyObjectType添加了一些自己的“选项”,但这些“选项”并未真正记录(据我所知)。我去阅读了源代码,果然我发现了对“model = ...”的引用

\n\n

https://github.com/graphql-python/graphene-sqlalchemy/blob/master/graphene_sqlalchemy/types.py#L174

\n\n

该类SQLAlchemyObjectType添加了三个选项:模型、注册表和连接。如果您查看代码,model选项就是您的 SQLAlchemy 模型类。该类SQLAlchemyObjectType使用 model 选项来检查您的模型并自动创建各自的字段。

\n\n

希望这可以节省其他人查找此内容的时间。

\n