`graphene_django` 的 `op_name` 参数

问题描述

django graphene documentation 显示了这样的测试示例:

class MyFancyTestCase(GraphQLTestCase):
    def test_some_query(self):
        response = self.query(
            '''
            query {
                myModel {
                    id
                    name
                }
            }
            ''',op_name='myModel'
        )

        content = json.loads(response.content)

        # This validates the status code and if you get errors
        self.assertResponseNoErrors(response)

        # Add some more asserts if you like
        ...

他们没有任何关于 op_name 是什么以及我们应该将其设置为什么的 API 文档。我尝试将其设置为我的查询名称,但出现错误

[{'message': 'UnkNown operation named "myQuery".'}]

解决方法

仅当查询字符串中有多个操作时才需要操作名称。您只有一项操作,因此默认值 (None) 就可以了。

https://docs.graphene-python.org/en/latest/execution/execute/#operation-name

,

根据我的评论:

如果查询是突变或命名查询,您必须提供 op_name。对于 annon 查询 ("{ ... }"),应该是 None(默认)

我不确定如何使用 django 石墨烯创建“命名查询”,但显然我的查询不是命名查询。将 op_name 保留为 None 让我的查询通过我的单元测试工作。