fluttergraphqlconnectiongraphene-pythongraphene-django

The connection problem in the graphql-flutter mobile app and the server created by Graphene-Django


I have a simple graphql-server created using the Graphene-Django package. Now I can test the query and mutation successfully in the desktop browser at http://127.0.0.1:8000/graphql. For testing the query and mutation in the mobile app, I made a simple flutter mobile app using the graphql_flutter package. My flutter app works properly with Hasura and Heroku graphql endpoint. But it doesn't work with my Graphene-Django graphql endpoint. when I try to run my mobile app, it gives an error message:

ClientExceptation: Failed to connect to http://127.0.0.1:8000/graphql.

Please help me for solving the problem. Thank you so much.


Solution

  • I solve above mentioned problem.I exempt my Graphql endpoint from CSRF protection by wrapping the GraphQLView with the csrf_exempt decorator in urls.py file in django project, just same as this (see the source ):

    from django.conf.urls import url, include
    from django.contrib import admin
    from django.views.decorators.csrf import csrf_exempt
    from graphene_django.views import GraphQLView
    from cookbook.schema import schema
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
        url(r'^graphql$', csrf_exempt(GraphQLView.as_view(graphiql=True,schema=schema))),
    ]
    

    it should be mention that i used 'http://my-IPv4-address:8000/graphql' in flutter app for successfully connection after above mentioned modification in CSRF Protection settings. for achieving IPv4-address follow this guide. after that, i added my-IPv4-address to ALLOWED_HOSTS in settings.py file like this:

    ALLOWED_HOSTS = ['192.168.x.xxx', 'localhost', '127.0.0.1']
    

    and finally for running graphene-django server i use this command in cmd console:

    (env) python ./manage.py runserver 0.0.0.0:8000