0

如何在graphene-django中为react-relay创建一个查看器字段?这不像预期的那样工作。我正在使用graphene-django v.2.2。姜戈 2.1。反应中继 v1.7.0-rc.1。

// landingpage.schema.py

class CollectionNode(DjangoObjectType):
  class Meta:
    model = Collection
    interfaces = (relay.Node, )

class Viewer(graphene.ObjectType):
  class Meta:
    interfaces = [relay.Node, ]

  collection = graphene.Field(CollectionNode)

  def resolve_collection(self, info, **kwargs):
    return Collection.objects.all()


class Query(object):
  viewer = graphene.Field(Viewer)

  def resolve_viewer(self, info, **kwargs):
    return Viewer() # or info.parent_type
4

1 回答 1

1

我设法自己解决了这个问题。所以我像这样将查看器对象移动到根模式。希望这可以帮助其他有同样问题的人。

// project_name.schema.py

import graphene
import graphql_jwt
from django.conf import settings
from graphene import relay, ObjectType

import about.schema
import shop.schema
import landingpage.schema

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() # or info.parent_type

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)

_

// landingpage.schema.py

class CollectionNode(DjangoObjectType):
  class Meta:
    model = Collection
    interfaces = (relay.Node, )

# 
#  QUERY

class Query(object):
  class Meta:
    interfaces = [relay.Node, ]

  collection = graphene.Field(CollectionNode)

  def resolve_collection(self, info, **kwargs):
    return Collection.objects.all()
于 2018-09-06T20:10:19.467 回答