简体   繁体   中英

Graphene-Django: In schema combine Query-objects (only takes first argument)

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?

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)

in setting.py add all your apps as follows:

  GRAPHENE = {
       "SCHEMA": "about.schema.schema",
       "SCHEMA": "shop.schema.schema",
       "SCHEMA": "landingpage.schema.schema", 
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM