Search code examples
djangographene-pythongraphene-django

`op_name` parameter for `graphene_django`


The django graphene documentation shows a test example like this:

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

They don't have any API documentation for what op_name is, and what we should set it as. I tried to set it to my query name, but get the error:

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

Solution

  • As per my comment:

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

    I am not sure how to create a "named query" with django graphene, but apparently my query is NOT a named query. Leaving op_name as None got my query to work via my unit test.