简体   繁体   English

Graphene-Django:在模式中组合查询对象(仅采用第一个参数)

[英]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.我正在尝试组合位于 Django 2.1 中不同应用程序中的多个查询模式。 Using graphene-django 2.2 (have tried 2.1 with same problem).使用 graphene-django 2.2(已经尝试过 2.1 有同样的问题)。 Python 3.7.蟒蛇 3.7。

The Query class only registers the first variable. Query 类只注册第一个变量。 As an example shop.schema.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)

Why is it like this?为什么会这样? Have something changed with classes in python 3.7? 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.我正在将我的架构导出到 schema.json 以便将它与反应中继一起使用。 I do find my object "collection" Query schema from landingpage(the 3. variable).我确实从登陆页面(3. 变量)中找到了我的对象“集合”查询模式。 Relay returns:继电器返回:

ERROR: GraphQLParser: Unknown field collection on type Viewer .错误:GraphQLParser:类型Viewer上的未知字段collection Source: document AppQuery file: containers/App/index.js .来源:文档AppQuery文件: containers/App/index.js AppQuery

Is it a problem with Relay reading my schema.json? Relay 读取我的 schema.json 有问题吗?

I managed to solve it shortly after writing this. 我写完这篇后不久就解决了。 My problem was that I had a Viewer object in every app. 我的问题是每个应用程序中都有一个Viewer对象。 Because I find it useful to have a viewer-graphql-root, like this: 因为我发现拥有一个viewer-graphql-root很有用,例如:

graphql'
  viewer {
    collection {
      somestuff
    }
  }
'

I moved the Viewer object up to the root schema.py like this: 我将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)

in setting.py add all your apps as follows:setting.py 中添加您的所有应用程序,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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