Graphene-Django:在架构中组合查询对象(仅使用第一个参数)

时间:2018-09-06 18:23:31

标签: django reactjs graphql graphene-python react-relay

我正在尝试组合Django 2.1中位于不同应用程序中的多个查询架构。使用graphene-django 2.2(曾尝试使用相同问题的2.1)。 Python 3.7。

Query类仅注册第一个变量。例如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)

为什么会这样? python 3.7中的类有什么变化吗?石墨烯教程说,这将继承多个...

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)

我正在将我的架构导出到schema.json以便与React Relay一起使用。我确实从着陆页(3.变量)找到了我的对象“集合”查询架构。中继返回:

  

错误:GraphQLParser:类型collection上的未知字段Viewer。   来源:文档AppQuery文件:containers/App/index.js

中继读取我的schema.json是否有问题?

1 个答案:

答案 0 :(得分:0)

我写完这篇后不久就设法解决了。我的问题是每个应用程序中都有一个Viewer对象。因为我发现拥有一个viewer-graphql-root很有用,例如:

graphql'
  viewer {
    collection {
      somestuff
    }
  }
'

我将Viewer对象移至根schema.py,如下所示:

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)