【问题标题】:What is the meta subclass in graphene?石墨烯中的元子类是什么?
【发布时间】:2018-10-22 08:57:31
【问题描述】:

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

这是教程中的一些代码。我已经评论了让我感到困惑的那一行。

从 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,)

【问题讨论】:

    标签: python flask sqlalchemy graphene-python flask-graphql


    【解决方案1】:

    我和你在同一条船上。对你来说可能有点晚了,但对于遇到同样问题的其他人来说,这是我在学习 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/en/latest/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 = ...”的来源时遇到了问题。我找不到任何引用“模型”含义的文档,但我的猜测是,由于SQLAlchemyObjectTypeObjectType 的子类,SQLAlchemyObjectType 添加了一些没有真正记录的自己的“选项”(到目前为止据我所知)。我仔细阅读了源代码,果然找到了对“model = ...”的引用

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 2017-05-13
      • 2017-05-11
      • 2022-01-24
      • 2019-12-20
      • 2018-01-11
      • 2020-09-01
      • 2021-08-24
      • 2020-01-08
      相关资源
      最近更新 更多