使用Graphene进行GraphQL多个查询和变更

huangapple go评论71阅读模式
英文:

Graphql multiple queries and mutation using Graphene

问题

我选择处理发送多个查询和突变的方式如下,

首先我在schema.py中定义了一个“Schema”:

# schema.py 代码
from graphene import Schema, Field, ObjectType
from .basic_objects.queries import BasicQuery
from .basic_objects.mutations import BasicMutation

schema = Schema(query=BasicQuery, mutation=BasicMutation)

然后我更新了“schema.py”文件:

from graphene import Schema, Field, ObjectType
from .basic_objects.queries import BasicQuery
from .basic_objects.mutations import BasicMutation


class Query(ObjectType):
    BasicQuery = Field(BasicQuery)
    # otherQuery

class Mutation(ObjectType):
    BasicMutation = Field(BasicMutation)


schema = Schema(query=Query, mutation=Mutation)

但是在尝试发送以下GraphQL请求时:

query MyQuery {
  BasicQueryData {
    oneNode(id: 1) {
      comment
      coordinateY
    }
  }
}

从查询中直接使用oneNode有效,并且graphene找到该方法并且它起作用。

我如何使查询首先进入BasicQueryData,然后调用oneNode方法?

英文:

I chose to handle sending multiple Queries and Mutations as follow,

First I defined a Schema defined in schema.py:

# schema.py code
from graphene import Schema, Field, ObjectType
from .basic_objects.queries import BasicQuery
from .basic_objects.mutations import BasicMutation

schema = Schema(query=BasicQuery, mutation=BasicMutation)

Then I have updated the schema.py file:

from graphene import Schema, Field, ObjectType
from .basic_objects.queries import BasicQuery
from .basic_objects.mutations import BasicMutation


class Query(ObjectType):
    BasicQuery = Field(BasicQuery)
    #otherQuery

class Mutation(ObjectType):
    BasicMutation = Field(BasicMutation)


schema = Schema(query=Query, mutation=Mutation)

But while trying to send the following GraphQL request:

query MyQuery {
  BasicQueryData {
    oneNode(id: 1) {
      comment
      coordinateY
    }
  }
}

from query oneNode directly working and graphene find the method and it is working.

How can I make that this time from query first go to BasicQueryData and then call oneNode method?

答案1

得分: 0

    类 Query 继承自 BasicQuery:
        Field 继承自 BasicQuery
    
    
    类 Mutation 继承自 BasicMutation:
        Field 继承自 BasicMutation
    
    
    schema = Schema(query=Query, mutation=Mutation)

我修复了如下:
英文:
class Query(BasicQuery):
    Field(BasicQuery)


class Mutation(BasicMutation):
    Field(BasicMutation)


schema = Schema(query=Query, mutation=Mutation)

I fixed like this

huangapple
  • 本文由 发表于 2023年6月27日 18:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76563948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定