0

Is there any way to create Dynamic queries in django graphene without mentioning object type. What I am currently doing is I created an ObjectType class and with a string type field and when I call the query i send the model name as an argument and serialize the queryset and send the json as the string field. But again, in that case I cannot take the advantages of graphQL, thats just me using graphql to get json data. Does anyone know of a proper way to implement this?

1 Answers1

0

You will have to mention ObjectType, that tells graphene what fields to register and gives you the whole functionality. It's very simple though:

from django.db import models

class Book(models.Model):
     name = models.CharField(max_length=128, null=True)

And the in schemas.py:

import graphene
from .models import Book as BookModel
from graphene_django import DjangoObjectType

class Book(DjangoObjectType):
    class Meta:
        model = BookModel

class Query(graphene.ObjectType):
    books = graphene.List(Book, resolver=lambda query, info: Books.objects.all())

schema = graphene.Schema(query=Query)

That's it!

Jura Brazdil
  • 970
  • 7
  • 15