简体   繁体   English

Flask / SqlAlchemy / Graphene - 如何从数据库查询对象并更改值?

[英]Flask / SqlAlchemy / Graphene - how to query an object from DB and change value?

I wish to open a GraphQL mutation endpoint where an id and string is sent in. This would then gather the item based on the ID and change a value in the object, and save the change in the item in the DB.我希望打开一个 GraphQL 突变端点,其中发送了一个 id 和字符串。然后这将根据 ID 收集项目并更改对象中的值,并将更改保存在数据库中的项目中。

It would look something like this:它看起来像这样:

Query SomeMutation{
ExampleMutation(input: {id: 1, Status: "Something"}){
    ExampleObject{
        id
        Status
     }
 }
}

I currently have the following setup:我目前有以下设置:

(Schema.py) (架构.py)

class Mutation(graphene.ObjectType):
    ExampleMutation = ExampleMutation.Field()

(Schema_ExampleObject.py) (Schema_ExampleObject.py)

class Captcha(SQLAlchemyObjectType):
    class Meta:
        model = ExampleObjectModel
        interfaces = (relay.Node,)

(ExampleMutation.py) (ExampleMutation.py)

class Attributes:
    id = graphene.Int(description="Id")
    status = graphene.String(description="Status")


class ExampleMutationInput(graphene.InputObjectType, Attributes):
    pass


class ExampleSolution(graphene.Mutation):
    ExampleObject= graphene.Field(lambda: ExampleObject, description="Example Object")

    class Arguments:
        input = ExampleMutationInput(required=True)

    def mutate(self, info, input):
        data = input

        # **** Here I want to query an item from the DB based on the ID and change a value in it, then save it in the DB and return the new object to the GraphQL. ****

        return ExampleMutation(exampleobject=exampleobject)

I looked up at solutions online and I saw library calls that would work in the following manner:我在网上查看了解决方案,我看到了以以下方式工作的库调用:

item = ExampleObject.query.filter(blablanla)

But the Object doesn't have such functions as "Query" so I'm left confused.但是对象没有“查询”这样的功能,所以我很困惑。

I have found the correct way of performing the operation:我找到了执行操作的正确方法:

 query = ExameObject.get_query(info=info)
 query = query.filter(ExampleObject.id == data.id)
 example_object = query.first()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM