5

I am trying to combine multiple Query schemas located in different apps in Django 2.1. Using graphene-django 2.2 (have tried 2.1 with same problem). Python 3.7.

The Query class only registers the first variable. As an example shop.schema.Query.

import graphene
import graphql_jwt
from django.conf import settings

import about.schema
import shop.schema
import landingpage.schema

class Query(about.schema.Query, shop.schema.Query, landingpage.schema.Query, graphene.ObjectType):
  pass

class Mutation(shop.schema.Mutation, graphene.ObjectType):
  token_auth = graphql_jwt.ObtainJSONWebToken.Field()
  verify_token = graphql_jwt.Verify.Field()
  refresh_token = graphql_jwt.Refresh.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)

Why is it like this? Have something changed with classes in python 3.7? The graphene tutorial says this will inherit for multiple...

class Query(cookbook.ingredients.schema.Query, graphene.ObjectType):
    # This class will inherit from multiple Queries
    # as we begin to add more apps to our project
    pass

schema = graphene.Schema(query=Query)

I am exporting my schema to schema.json for using it with react relay. I do find my object "collection" Query schema from landingpage(the 3. variable). Relay returns:

ERROR: GraphQLParser: Unknown field collection on type Viewer. Source: document AppQuery file: containers/App/index.js.

Is it a problem with Relay reading my schema.json?

Espen Finnesand
  • 475
  • 7
  • 22

2 Answers2

0

I managed to solve it shortly after writing this. My problem was that I had a Viewer object in every app. Because I find it useful to have a viewer-graphql-root, like this:

graphql'
  viewer {
    collection {
      somestuff
    }
  }
'

I moved the Viewer object up to the root schema.py like this:

class Viewer(about.schema.Query, landingpage.schema.Query, shop.schema.Query, graphene.ObjectType):
  class Meta:
    interfaces = [relay.Node, ]

class Query(graphene.ObjectType):
  viewer = graphene.Field(Viewer)

  def resolve_viewer(self, info, **kwargs):
    return Viewer()

class Mutation(shop.schema.Mutation, graphene.ObjectType):
  token_auth = graphql_jwt.ObtainJSONWebToken.Field()
  verify_token = graphql_jwt.Verify.Field()
  refresh_token = graphql_jwt.Refresh.Field()

schema = graphene.Schema(query=Query, mutation=Mutation)
Espen Finnesand
  • 475
  • 7
  • 22
0
  1. In setting.py add a new file as schema.py
  2. Combine your Queries and Mutations in schema.py as follows:

import graphene

import about.schema as about

import shop.schema as projects

import landingpage.schema as projects

then add:

class Query(about.schema.Query, shop.schema.Query, landingpage.schema.Query,  graphene.ObjectType):
        pass
class Mutation(about.schema.Mutation, shop.schema.Mutation, landingpage.schema.Mutation, graphene.ObjectType):
        pass
schema = graphene.Schema(query=Query, mutation=Mutation)
  1. Configure your combined schema in settings.py as follows:

    GRAPHENE = { "SCHEMA": "core.schema.schema", }

EILYA
  • 358
  • 4
  • 5