4

I am trying to use graphQL queries in django. Basically I have two apps, my 'api' app which contains everything I need to make the queries and another one called 'frontend' from which I call the api to use these queries.

I can use the GraphQL view to type queries in it and it works perfectly, but whenever I try to make the query, I get this: "OrderedDict([('users', None)])"

Result of my query in the GraphQl view

The code:

In 'api' my schema.py:

import graphene import graphql_jwt from graphene import relay, ObjectType, AbstractType, List, String, Field,InputObjectType from graphene_django import DjangoObjectType from graphene_django.filter import DjangoFilterConnectionField from datetime import date, datetime from django.contrib.auth.models import User from django.contrib.auth import get_user_model .... class Query(graphene.ObjectType): me = graphene.Field(UserType) users = graphene.List(UserType) profile = relay.Node.Field(ProfileNode) all_profiles = DjangoFilterConnectionField(ProfileNode) def resolve_users(self, info): ### Returns all users ### user = info.context.user if user.is_anonymous: raise Exception('Not logged!') if not user.is_superuser: raise Exception('premission denied') return User.objects.all() def resolve_me(self, info): ### Returns logged user ### user = info.context.user if user.is_anonymous: raise Exception('Not logged!') return user def resolve_all_profiles(self, info, **kwargs): ### Returns all profiles ### return Profile.objects.all() ..... def execute(my_query): schema = graphene.Schema(query=Query) return schema.execute(my_query) 

And the views.py that calls the app 'api' in my app frontend:

from django.shortcuts import render import graphene from api import schema from django.contrib.auth import authenticate def accueil(request): if request.user.is_authenticated: check = "I am logged" else: check = "I am not logged" result = schema.execute("""query { users { id username } }""") return render(request, 'frontend/accueil.html', {'result' : result.data, 'check' : check}) 

The template :

<h1>OTC</h1> <p> the users are : {{result}}</p> <br/> <p>{{check}}</p> <a href="{%url 'login' %}">login</a> <a href="{%url 'logout' %}">logout</a> 

and finally:

The web page result

and the error in the console:

An error occurred while resolving field Query.users Traceback (most recent call last): File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executor.py", line 311, in resolve_or_error return executor.execute(resolve_fn, source, info, **args) File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executors/sync.py", line 7, in execute return fn(*args, **kwargs) File "/home/victor/poc2/poc2/api/schema.py", line 67, in resolve_users user = info.context.user AttributeError: 'NoneType' object has no attribute 'user' Traceback (most recent call last): File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executor.py", line 330, in complete_value_catching_error exe_context, return_type, field_asts, info, result) File "/home/victor/myenv/lib/python3.5/site-packages/graphql/execution/executor.py", line 383, in complete_value raise GraphQLLocatedError(field_asts, original_error=result) graphql.error.located_error.GraphQLLocatedError: 'NoneType' object has no attribute 'user' 
4
  • What are you passing as info in resolve_users? Commented Apr 19, 2018 at 13:32
  • Why are you making a graphQL query from a inside Django view? Commented Apr 19, 2018 at 14:24
  • @kartikmaji the methods resolve_* are automatically called by the calls Query , so it's the Query class that passes info i don't handle it Commented Apr 19, 2018 at 14:54
  • @MarkChackerian the graphQL query is made in the api.schema.execute() i'm just passing the string of the query in the views Commented Apr 19, 2018 at 14:56

1 Answer 1

8

Unless you're writing a test client, you probably should not be calling schema.execute from inside a Django view. But assuming that you have your reasons for doing this, your specific problem is that you're not passing the user when you invoke schema.execute in accueil view.

Have a look at the execute documentation and you'll see that you'll need to supply an optional argument for the context. Your code is not supplying a context, hence the info.context is None, as per your exception. Unfortunately, the example

result = schema.execute('{ name }', context_value={'name': 'Syrus'}) 

is not Django-specific. But I think what works in a Django functional view is:

result = schema.execute(query, context_value=request) 
Sign up to request clarification or add additional context in comments.

1 Comment

oh boi, that's it, thanks a lot dude ! (and yes i'm writting a test client ;) )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.