代码之家  ›  专栏  ›  技术社区  ›  Alexis.Rolland user2915097

使用Graphene和SQLAlchemy通过GraphQL API更新记录

  •  4
  • Alexis.Rolland user2915097  · 技术社区  · 8 年前

    我正在使用python包Flask、SQLAlchemy、Graphene和Graphene SQLAlchemy构建GraphQL API。我遵循了 SQLAlchemy + Flask Tutorial . 我能够执行查询和突变来创建记录。现在我想知道更新现有记录的最佳方法是什么。

    这是我当前的脚本 架构。py公司 :

    from graphene_sqlalchemy import SQLAlchemyObjectType
    from database.batch import BatchOwner as BatchOwnerModel
    import api_utils  # Custom methods to create records in database
    import graphene
    
    
    class BatchOwner(SQLAlchemyObjectType):
        """Batch owners."""
        class Meta:
            model = BatchOwnerModel
            interfaces = (graphene.relay.Node,)
    
    
    class CreateBatchOwner(graphene.Mutation):
        """Create batch owner."""
        class Arguments:
            name = graphene.String()
    
        # Class attributes
        ok = graphene.Boolean()
        batch_owner = graphene.Field(lambda: BatchOwner)
    
        def mutate(self, info, name):
            record = {'name': name}
            api_utils.create('BatchOwner', record) # Custom methods to create records in database
            batch_owner = BatchOwner(name=name)
            ok = True
            return CreateBatchOwner(batch_owner=batch_owner, ok=ok)
    
    
    class Query(graphene.ObjectType):
        """Query endpoint for GraphQL API."""
        node = graphene.relay.Node.Field()
        batch_owner = graphene.relay.Node.Field(BatchOwner)
        batch_owners = SQLAlchemyConnectionField(BatchOwner)
    
    
    class Mutation(graphene.ObjectType):
        """Mutation endpoint for GraphQL API."""
        create_batch_owner = CreateBatchOwner.Field()
    
    
    schema = graphene.Schema(query=Query, mutation=Mutation)
    

    评论:

    • 我的对象 BatchOwner 只有2个属性(Id、名称)
    • 能够更新 批次所有者 名称,我假设需要提供数据库Id(而不是中继全局Id)作为某个更新方法的输入参数
    • 但当我询问 批次所有者 从我的客户端,Graphene只返回base64编码的全局Id(例如: QmF0Y2hPd25lcjox ,对应于 BatchOwner:1 )

    响应示例:

    {
        "data": {
            "batchOwners": {
                "edges": [
                    {
                        "node": {
                            "id": "QmF0Y2hPd25lcjox",
                            "name": "Alexis"
                        }
                    }
                ]
            }
        }
    }
    

    我目前正在考虑的解决方案是:

    • 创建以全局Id为参数的更新变体
    • 解码全局Id(如何?)
    • 使用从解码的全局Id检索的数据库Id查询数据库并更新相应的记录

    有没有更好的方法?

    1 回复  |  直到 8 年前
        1
  •  6
  •   Alexis.Rolland user2915097    8 年前

    我已经找到了使用该方法的解决方案 from_global_id ( documented here )

    from graphql_relay.node.node import from_global_id
    

    我将以下类添加到 架构。py公司 :

    class UpdateBatchOwner(graphene.Mutation):
        """Update batch owner."""
        class Arguments:
            id = graphene.String()
            name = graphene.String()
    
        # Class attributes
        ok = graphene.Boolean()
        batch_owner = graphene.Field(lambda: BatchOwner)
    
        def mutate(self, info, id, name):
            id = from_global_id(id)
            record = {'id': id[1], 'name': name}
            api_utils.update('BatchOwner', record)
            batch_owner = BatchOwner(id=id, name=name)
            ok = True
            return UpdateBatchOwner(batch_owner=batch_owner, ok=ok)
    

    我更新了突变类:

    class Mutation(graphene.ObjectType):
        """Mutation endpoint for GraphQL API."""
        create_batch_owner = CreateBatchOwner.Field()
        update_batch_owner = UpdateBatchOwner.Field()
    

    我想知道是否有更直接的方法可以做到这一点?

    推荐文章