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

graphene-django中GraphQLTestCase.query()遇op_name参数错误

解决graphene-django 3.0.0中GraphQLTestCase.query()参数错误问题

测试代码

def test_create_foo(self):
    response = self.query(
        """
        mutation createFoo($input: MutationInput!) {
            createFoo(input: $input) {
                foo {
                    id
                    title
                }
            }
        }
        """,
        op_name="createFoo",
        input_data={"title": "Title Test"},
    )

触发错误

response = self.query(
TypeError: query() got an unexpected keyword argument 'op_name'

使用版本

  • graphene 3.1.1
  • graphene-django 3.0.0

解决方法

graphene-django 3.x版本对GraphQLTestCase.query()方法的参数做了调整,需要修改两处:

  1. 移除op_name参数:操作名已经定义在查询字符串的mutation createFoo(...)中,无需额外传递。
  2. 将input_data替换为variables参数,并把输入数据包裹在对应变量名的键下(这里查询中的变量是$input,所以变量字典的键为input)。

修改后的代码如下:

def test_create_foo(self):
    response = self.query(
        """
        mutation createFoo($input: MutationInput!) {
            createFoo(input: $input) {
                foo {
                    id
                    title
                }
            }
        }
        """,
        variables={"input": {"title": "Title Test"}},
    )

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 23:50:29