python - 石墨烯中的元子类是什么?
问题描述
我正在学习如何使用 GraphQL 和 python。我发现了石墨烯项目以及它的 SQLAlchemy 和 Flask 扩展。我一直在阅读教程和文档,但class Meta
在定义模式时无法弄清楚它的用途。我目前正在关注本教程。我用谷歌搜索,似乎找不到任何东西。
这是教程中的一些代码。我已经评论了让我感到困惑的那一行。
从 graphene_sqlalchemy 导入 SQLAlchemyObjectType 从 database.model_people 导入 ModelPeople 进口石墨烯 # 创建一个通用类来共同化查询和突变的人属性描述 类人属性: 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。") 类人(SQLAlchemyObjectType,PeopleAttribute): """人物节点。""" # ---------- 这个类是做什么用的?flask + 石墨烯 + sqlalchemy 生态系统的哪个部分使用它? 元类: 模型 = 模型人 接口 = (graphene.relay.Node,)
解决方案
我和你在同一条船上。对你来说可能有点晚了,但对于遇到同样问题的其他人来说,这是我在学习 GraphQL 和 Graphene 时发现的。
我也对子类感到困惑Meta
。这就是文档所述。
Graphene 在 ObjectType 上使用 Meta 内部类来设置不同的选项。
https://docs.graphene-python.org/en/latest/types/objecttypes/#objecttype-configuration-meta-class
Meta 类本质上允许您更改/修改类的属性。例如,您可以通过设置name = <short name of class>
.
默认情况下,GraphQL 模式中的类型名称将与定义 ObjectType 的类名称相同。这可以通过在 Meta 类上设置 name 属性来改变:
他们使用的例子是这个..
from graphene import ObjectType
class MyGraphQlSong(ObjectType):
class Meta:
name = 'Song'
因此,不必查询“MyGraphQlSong”,您可以通过“Song”查询它。
您还可以添加其他内容,例如描述,以及您的父类应该继承的接口(所有内容都在上面的链接中描述)。
您可以更改/修改的属性的完整列表在 API 参考中(这是特定于 ObjectType。其他类型有其他元类选项。这里有更详细的解释。https://docs.graphene-python.org /zh/最新/api/
Meta class options (optional):
name (str): Name of the GraphQL type (must be unique in schema). Defaults to class
name.
description (str): Description of the GraphQL type in the schema. Defaults to class
docstring.
interfaces (Iterable[graphene.Interface]): GraphQL interfaces to extend with this object.
all fields from interface will be included in this object’s schema.
possible_types (Iterable[class]): Used to test parent value object via isintance to see if
this type can be used to resolve an ambigous type (interface, union).
default_resolver (any Callable resolver): Override the default resolver for this
type. Defaults to graphene default resolver which returns an attribute or dictionary key with the same name as the field.
fields (Dict[str, graphene.Field]): Dictionary of field name to Field. Not recommended to
use (prefer class attributes).
我在查找 SQLAlchemy 示例中“model = ...”的来源时遇到了问题。我找不到任何引用“模型”含义的文档,但我的猜测是,因为SQLAlchemyObjectType
它是 的子类ObjectType
,所以SQLAlchemyObjectType
添加了一些没有真正记录的“选项”(据我所知)。我去阅读了源代码,果然我找到了对“model = ...”的引用
https://github.com/graphql-python/graphene-sqlalchemy/blob/master/graphene_sqlalchemy/types.py#L174
该类SQLAlchemyObjectType
添加了另外三个选项,模型、注册表和连接。如果您查看代码,则model
选项是您的 SQLAlchemy 模型类。该类SQLAlchemyObjectType
使用模型选项来检查您的模型并自动创建各自的字段。
希望这可以节省一些其他人的时间,而不必查找它。
推荐阅读
- javascript - 模运算在阈值签名 (TSS) 实现中不起作用
- ios - 'GADURewardedInterstitialAd.h' 文件未找到 -error xcode-
- loops - Terraform如何循环计数(数字类型)
- android - Android Firebase 不适用于颤振网络
- python - 为什么目标编码器将某些值编码为 NaN?
- mongodb - 如何使用 mongo 驱动程序在 Golang 中实现 MongoDB 自动完成?
- c# - 在实体框架中执行存储过程 - 没有定义键。(代码优先)
- javascript - 引用外部脚本和资源而不是将它们“本地”下载到您的服务器是否安全?
- java - OkHttp 这个浏览器不支持框架
- vb.net - 如何在 vb 中获得唯一的 chrome ID?