You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用sgqlc构建复杂GraphQL突变查询时遇ValueError求助

问题:使用sgqlc构建GraphQL突变查询触发ValueError

目标GraphQL突变查询

mutation {
  addEntities(
    organization: "НИУ", 
    data: {
    limitedaccess: [
      {
        value: "https://hiddennnadmin.com",
      }
    ]
  }) {
    data {
      id
      source
      value
      state
    }
    rejections {
        key {
          id
        },
        rejections {
          id
          message
        }
      }
  }
}

自动生成的sgqlc Schema代码

class Mutations(sgqlc.types.Type):
    add_entities = sgqlc.types.Field(AdditionResponseServiceEntityResponseBase, graphql_name='addEntities', args=sgqlc.types.ArgDict((
        ('organization', sgqlc.types.Arg(String, graphql_name='organization', default=None)),
        ('data', sgqlc.types.Arg(sgqlc.types.non_null(UpsertDataInput), graphql_name='data', default=None)))))


class AdditionResponseServiceEntityResponseBase(sgqlc.types.Type):
    data = sgqlc.types.Field(sgqlc.types.non_null(sgqlc.types.list_of(sgqlc.types.non_null(ServiceEntity))), graphql_name='data')
    rejections = sgqlc.types.Field(sgqlc.types.non_null(sgqlc.types.list_of(sgqlc.types.non_null('Rejection'))), graphql_name='rejections')


class UpsertDataInput(sgqlc.types.Input):
    __schema__ = schema
    __field_names__ = ('massmedia', 'socialmedia', 'mobileapplication', 'socialaccount', 'fraudlentresource', 'database', 'limitedaccess', 'sharingplatform')
    massmedia = sgqlc.types.Field(sgqlc.types.list_of(sgqlc.types.non_null(MassMediaRequestInput)), graphql_name='massmedia')
 
# ...其他字段定义代码

自行编写的查询构建代码

def add_entities(services: str, values: str):
        """Query to create entities"""
        op = Operation(Mutations)
        op.add_entities(organization='ORGNAME')
        op.add_entities(data={f"{services}": [{'value': values}]})
        op.add_entities.data()
        op.add_entities.rejections()
        print(op)

触发的错误信息

ValueError: add_entities already have a selection addEntities(organization: "ORGNAME") {
  data {
    source
    state
    hidden
    stateUpdated
    service
    id
    updated
    details
    subService
    value
    created
    description
    tags
  }
}. Maybe use __alias__ as param?

问题核心:代码错误地多次调用op.add_entities(),导致重复创建选择集;同时需要明确指定返回字段,而非默认全量返回。


解决方案

错误原因

每次调用op.add_entities()都会生成一个新的GraphQL操作选择,多次调用就会触发重复选择的错误。正确做法是一次性传入所有参数,再链式配置返回字段。

修正后的代码

def add_entities(services: str, values: str):
    """Query to create entities"""
    op = Operation(Mutations)
    # 一次性传入所有参数:organization和data
    add_ent = op.add_entities(
        organization='НИУ',
        data={services: [{'value': values}]}
    )
    # 配置data字段需要返回的具体字段,而非默认全量
    add_ent.data.id()
    add_ent.data.source()
    add_ent.data.value()
    add_ent.data.state()
    # 配置rejections字段的嵌套返回字段
    rej = add_ent.rejections()
    rej.key.id()
    rej.rejections.id()
    rej.rejections.message()
    
    print(op)

代码说明

  1. 一次性传参:调用op.add_entities()时同时传入organization和data参数,避免重复创建操作选择。
  2. 指定返回字段:通过链式调用.data.id()等方法,精准选择需要返回的字段,而非默认返回所有字段。
  3. 嵌套字段配置:对rejections的嵌套字段key和rejections,同样通过链式调用指定需要返回的字段。

运行修正后的代码,生成的查询会与目标GraphQL突变完全一致。

内容的提问来源于stack exchange,提问作者Sofi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 13:34:58