【问题标题】:Flask / SqlAlchemy / Graphene - how to query an object from DB and change value?Flask / SqlAlchemy / Graphene - 如何从数据库中查询对象并更改值?
【发布时间】:2020-04-03 11:48:46
【问题描述】:

我希望打开一个 GraphQL 突变端点,其中发送一个 id 和字符串。然后这将根据 ID 收集项目并更改对象中的值,并将更改保存在数据库中的项目中。

看起来像这样:

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】:

    我找到了正确的操作方法:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 2018-08-22
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多