graphql-python / graphql-python/graphene-django

GraphQLTestCase doesn't work - returns 404

Open
#1,253 2 comments 1 reaction 0 assignees View on GitHub
🐛bug
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

* **What is the current behavior?**

I was following the graphene-django basic tutorial (https://docs.graphene-python.org/projects/django/en/latest/tutorial-plain/), and was able to query with:

query {
allIngredients {
id
name
}
}

But when I create the following test (note: I also installed mixer):

```
from graphene_django.utils.testing import GraphQLTestCase

from cookbook.ingredients.models import Ingredient

from mixer.backend.django import mixer

class MyFancyTestCase(GraphQLTestCase):

def setUp(self):
super().setUp()
self.ingredient1 = mixer.blend(Ingredient)
self.ingredient2 = mixer.blend(Ingredient)

def test_some_query(self):
response = self.query(
'''
query {
allIngredients {
id
name
}
}
''',
op_name='allIngredients'
)

# content = json.loads(response.content)
print(vars(response))

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

```

I get a 404:

```
{'headers': {'Content-Type': 'text/html', 'X-Frame-Options': 'DENY', 'Content-Length': '179', 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'same-origin'}, '_charset': None, '_resource_closers': [], '_handler_class': None, 'cookies': , 'closed': True, '_reason_phrase': None, '_container': [b'\n\n\n\n Not Found\n\n\n

Not Found

The requested resource was not found on this server.

\n\n\n'], '_has_been_logged': True, 'wsgi_request': , 'exc_info': None, 'client': , 'request': {'PATH_INFO': '/graphql/', 'REQUEST_METHOD': 'POST', 'SERVER_PORT': '80', 'wsgi.url_scheme': 'http', 'CONTENT_LENGTH': '189', 'CONTENT_TYPE': 'application/json', 'wsgi.input': , 'QUERY_STRING': ''}, 'templates': [], 'context': [{'True': True, 'False': False, 'None': None}, {'request_path': '/graphql/', 'exception': 'Resolver404'}], 'json': functools.partial(>, ), 'resolver_match': . at 0x105491b80>>}
```

* **What is the expected behavior?**

I would expect a normal JSON response with 2 ingredients.

* **What is the motivation / use case for changing the behavior?**

To make sure your official docs are accurate and works for any new user.

* **Please tell us about your environment:**
Here's my poetry file:

```
[tool.poetry]
name = "tmp-project-blei"
version = "0.1.0"
description = "test project"
authors = ["blei"]

[tool.poetry.dependencies]
python = "^3.9"
graphene = "^2.1.9"
Django = "^3.2.6"
graphene-django = "^2.15.0"

[tool.poetry.dev-dependencies]
safety = "^1.10.3"
flake8 = "^3.9.2"
flake8-bugbear = "^21.4.3"
autoflake = "^1.4"
mixer = "^7.1.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

```

Platform: MacOS
Python: 3.9

* **Other information** (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)

Update documentation with a working example that continues from the "Basic tutorial". Or have a working example github project that people can reference / clone + test.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.