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

问题描述 投票:1回答:1

我想打开一个发送ID和字符串的GraphQL突变端点。然后,它将基于ID收集项目并更改对象中的值,并将更改保存在DB中。

看起来像这样:

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

我目前具有以下设置:

((Schema.py)

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

((Schema_ExampleObject.py)

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

((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)

我在线查看了解决方案,并且看到可以以下方式工作的库调用:

item = ExampleObject.query.filter(blablanla)

但是对象不具有“查询”之类的功能,所以我感到困惑。

python flask-sqlalchemy graphene-python
1个回答
0
投票

我已经找到执行该操作的正确方法:

© www.soinside.com 2019 - 2024. All rights reserved.