Search code examples
pythondjangographqlgraphene-pythongraphene-django

Retrieve GraphQL Introspection Schema as http response


I can generate the Django Graphene introspection schema by using the Django management command as

./manage.py graphql_schema --schema tutorial.quickstart.schema --out schema.json

How can I return the JSON schema as HTTP Response from a view, so that the client is able to view/fetch the same any time?


Solution

  • Use introspect()--(GitHub) method of the Schema(...)--(GitHub) class

    from django.http.response import JsonResponse
    from tutorial.quickstart import schema
    
    
    def introspection_schema(request):
        data = {"data": schema.introspect()}
        return JsonResponse(data)