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()方法的参数做了调整,需要修改两处:
- 移除
op_name参数:操作名已经定义在查询字符串的mutation createFoo(...)中,无需额外传递。 - 将
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
相关产品推荐
相关产品推荐

