Skip to main content
 首页 » 编程设计

python之Graphite 烯中的元子类是什么

2024年12月31日15oomusou

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

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

from graphene_sqlalchemy import SQLAlchemyObjectType 
from database.model_people import ModelPeople 
import graphene 
 
 
# Create a generic class to mutualize description of people attributes for both queries and mutations 
class PeopleAttribute: 
    name = graphene.String(description="Name of the person.") 
    height = graphene.String(description="Height of the person.") 
    mass = graphene.String(description="Mass of the person.") 
    hair_color = graphene.String(description="Hair color of the person.") 
    skin_color = graphene.String(description="Skin color of the person.") 
    eye_color = graphene.String(description="Eye color of the person.") 
    birth_year = graphene.String(description="Birth year of the person.") 
    gender = graphene.String(description="Gender of the person.") 
    planet_id = graphene.ID(description="Global Id of the planet from which the person comes from.") 
    url = graphene.String(description="URL of the person in the Star Wars API.") 
 
 
class People(SQLAlchemyObjectType, PeopleAttribute): 
    """People node.""" 
 
    # ---------- What's this class used for? Which part of the flask + graphene + sqlalchemy ecosystem uses it? 
    class Meta: 
        model = ModelPeople 
        interfaces = (graphene.relay.Node,) 

请您参考如下方法:

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

我对子类感到困惑Meta类也是如此。这就是文档所述。

Graphene uses a Meta inner class on ObjectType to set different options.

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

Meta 类本质上允许您更改/修改类的属性。例如,您可以通过设置 name = <short name of class> 来更改查询该特定类的方式的名称。 .

By default the type name in the GraphQL schema will be the same as the class name that defines the ObjectType. This can be changed by setting the name property on the Meta class:

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

from graphene import ObjectType 
 
class MyGraphQlSong(ObjectType): 
    class Meta: 
        name = 'Song' 

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

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

您可以更改/修改的完整属性列表在 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 = ...”的来源时遇到了问题。我找不到任何引用“模型”含义的文档,但我的猜测是自 SQLAlchemyObjectType 以来是 ObjectType 的子类, SQLAlchemyObjectType添加了一些自己的“选项”,这些“选项”并没有真正记录下来(据我所知)。我去阅读了源代码,果然我找到了对“model = ...”的引用

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

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

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