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?
Asked
Active
Viewed 249 times
0
-
Welcome to StackOverflow! Please post an example of the code which does not work for you and what you want to achieve. – The Godfather May 17 '20 at 08:13
1 Answers
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