djangographqlgraphene-django

Cannot query foreign key field in GraphQL


I'm using graphene-django framework for GraphQL. All fields I can retrieve except foreign key

# models.py

from django.db import models
from django.contrib.auth.models import User
from users.models import UserProfile

class Video(models.Model):
    title = models.TextField()
    description = models.TextField()
    author = models.ForeignKey(User, on_delete=models.CASCADE)      
# schema.py

class VideoType(DjangoObjectType):
    class Meta:
        model = Video

My query is like this

query {
  home_videos {
    title
    description
    author
  }
}

Following is error message in GraphQLView.

Cannot query field author on type VideoType


Solution

  • I added UserType and changed model foreignKey type

    class UserType(DjangoObjectType):
        class Meta:
            model = UserProfile
    
    author = models.ForeignKey(UserProfile, on_delete=models.CASCADE)