簡體   English   中英

Graphene-Django:在模式中組合查詢對象(僅采用第一個參數)

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

我正在嘗試組合位於 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:類型Viewer上的未知字段collection 來源:文檔AppQuery文件: containers/App/index.js AppQuery

Relay 讀取我的 schema.json 有問題嗎?

我寫完這篇后不久就解決了。 我的問題是每個應用程序中都有一個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)

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