5

我正在尝试在 Django 2.1 中组合位于不同应用程序中的多个查询模式。使用 graphene-django 2.2(已经尝试过 2.1 有同样的问题)。蟒蛇 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 以将其与反应中继一起使用。我确实从登录页面(3. 变量)中找到了我的对象“集合”查询模式。继电器返回:

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

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

4

2 回答 2

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)
于 2018-09-06T20:16:17.310 回答
0
  1. setting.py添加一个新文件为schema.py
  2. 在schema.py中组合您的查询和突变,如下所示:

进口石墨烯

导入 about.schema 为 about

将 shop.schema 作为项目导入

将landingpage.schema 作为项目导入

然后加:

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. 在 settings.py 中配置您的组合架构,如下所示:

    石墨烯 = { "SCHEMA": "core.schema.schema", }

于 2021-12-01T09:51:11.243 回答