I'm trying to get my feet wet with graphene and graphene_django. Going through the graphene_django tutorial I ran into a small issue. I've setup my files like so:
ch.schema.py
from django.contrib.auth.models import User
from graphene_django.types import DjangoObjectType
import graphene
class UserType(DjangoObjectType):
class Meta:
model = User
class Query(graphene.ObjectType):
users = graphene.List(UserType)
user = graphene.Field(UserType,
id=graphene.Int(),
username=graphene.String())
def resolve_users(self, args):
return User.objects.all()
def resolve_user(self, args, context, info):
id = args.get('id')
name = args.get('username')
if id is not None:
return User.objects.get(pk=id)
if username is not None:
return User.objects.get(username=username)
return None
schema = graphene.Schema(query=Query)
ch.urls.py
from django.contrib import admin
from django.conf.urls import include
from django.conf.urls import url
from graphene_django.views import GraphQLView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^conferences/', include('conferences.urls')),
url(r'^graphql', GraphQLView.as_view(graphiql=True)),
]
relevant settings.py
GRAPHENE = {
'SCHEMA': 'ch.schema.schema' # Where your Graphene schema lives
}
Now when I run:
{
users {
id
username
password
}
}
Graphql returns a list of all current users and their hashed passwords. But when I run:
{
user(id:1) {
id
username
password
}
}
I get the following error:
{
"errors": [
{
"message": "resolve_user() got an unexpected keyword argument 'id'",
"locations": [
{
"line": 2,
"column": 3
}
]
}
],
"data": {
"user": null
}
}
My installs are:
Django==1.11.4
graphene==2.0.dev20170802065539
graphene-django==2.0.dev2017083101
graphql-core==2.0.dev20170801051721
graphql-relay==0.4.5
iso8601==0.1.12
promise==2.1.dev0
pytz==2017.2
singledispatch==3.4.0.3
six==1.10.0
typing==3.6.2
There are breaking changes in graphql-core==2.0.dev
See UPGRADE-v2.0.md to have help on upgrading to 2.0.
The gist:
Your resolve_user should have this signature def resolve_user(self, info, **args) ( or def resolve_user(self, info, id, username) ) for the rest of the resolve code to work.
Excellent! That was exactly what the problem was! Thank you for your help @richmondwang
Most helpful comment
There are breaking changes in graphql-core==2.0.dev
See UPGRADE-v2.0.md to have help on upgrading to 2.0.
The gist:
Your
resolve_usershould have this signaturedef resolve_user(self, info, **args)( ordef resolve_user(self, info, id, username)) for the rest of the resolve code to work.