`op_name` 参数用于 `graphene_django`

`op_name` parameter for `graphene_django`

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
        ...

他们没有任何 API 文档说明 op_name 是什么,我们应该将其设置为什么。我试图将其设置为我的查询名称,但出现错误:

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

根据我的评论:

If the query is a mutation or named query, you must supply the op_name. For annon queries ("{ ... }"), should be None (default)

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

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

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