graphql-python / graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
http://docs.graphene-python.org/projects/django/en/latest/
MIT License
4.31k stars 769 forks source link

GraphQLTestCase doesn't work - returns 404 #1253

Open benlei-gfm opened 3 years ago

benlei-gfm commented 3 years ago

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': <SimpleCookie: >, 'closed': True, '_reason_phrase': None, '_container': [b'\n<!doctype html>\n<html lang="en">\n<head>\n  <title>Not Found</title>\n</head>\n<body>\n  <h1>Not Found</h1><p>The requested resource was not found on this server.</p>\n</body>\n</html>\n'], '_has_been_logged': True, 'wsgi_request': <WSGIRequest: POST '/graphql/'>, 'exc_info': None, 'client': <django.test.client.Client object at 0x1053a9e20>, 'request': {'PATH_INFO': '/graphql/', 'REQUEST_METHOD': 'POST', 'SERVER_PORT': '80', 'wsgi.url_scheme': 'http', 'CONTENT_LENGTH': '189', 'CONTENT_TYPE': 'application/json', 'wsgi.input': <django.test.client.FakePayload object at 0x1053a99d0>, 'QUERY_STRING': ''}, 'templates': [<django.template.base.Template object at 0x10559a040>], 'context': [{'True': True, 'False': False, 'None': None}, {'request_path': '/graphql/', 'exception': 'Resolver404'}], 'json': functools.partial(<bound method ClientMixin._parse_json of <django.test.client.Client object at 0x1053a9e20>>, <HttpResponseNotFound status_code=404, "text/html">), 'resolver_match': <SimpleLazyObject: <function Client.request.<locals>.<lambda> at 0x105491b80>>}

I would expect a normal JSON response with 2 ingredients.

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

[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

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.

disposableraft commented 3 years ago

I was able to get around this by setting

GRAPHQL_URL = "http://localhost/graphql"
thelfensdrfer commented 2 years ago

The problem for me was the trailing / in the URL, so putting GRAPHQL_URL = '/graphql' in my test class did work.