GraphQL中的字段必须始终具有类型。 GraphQL具有null的概念,但null本身并不是一种类型-它只是表示缺乏价值。
在GraphQL中没有“ void”类型。但是,类型默认情况下为空,因此,无论字段的类型如何,您的解析器都不会返回任何内容,并且该字段将简单地解析为null。所以您可以可以做
type Mutation {
deleteQueue(input: QueueInput!): Boolean #or any other type
}
或者,如果您想要一个专门表示null的标量,则可以create your own。
const { GraphQLScalarType } = require('graphql')
const Void = new GraphQLScalarType({
description: 'Void custom scalar',name: 'Void',parseLiteral: (ast) => null,parseValue: (value) => null,serialize: (value) => null,})
然后做
type Mutation {
deleteQueue(input: QueueInput!): Void
}
也就是说,返回某物是一种常见的做法。对于删除,通常返回已删除的项目或至少返回其ID。这有助于客户端的缓存管理。返回某种突变有效载荷类型以更好地封装客户端错误也变得越来越普遍。
您可以在“有效负载”类型中包含任意数量的字段,如下所示:
type Mutation {
deleteQueue(input: QueueInput!): DeleteQueuePayload
}
type DeleteQueuePayload {
# the id of the deleted queue
queueId: ID
# the queue itself
queue: Queue
# a status string
status: String
# or a status code
status: Int
# or even an enum
status: Status
# or just include the client error
# with an appropriate code,internationalized message,etc.
error: ClientError
# or an array of errors,if you want to support validation,for example
errors: [ClientError!]!
}
DeleteQueuePayload甚至可以是不同类型的并集,从而使客户端可以使用__typename
来确定突变的结果。
但是,您公开哪些信息取决于您的特定需求,而您采用的特定模式则取决于意见。
有关其他讨论和示例,请参见here和here。
,
我将graphql服务器与pyramida一起使用,并在删除某些对象时返回有关确实删除的内容的信息。很好,因为当您从客户端执行删除操作并获取有关它的响应时,该响应可以帮助您通过更新缓存来更改ui
本文链接:https://www.f2er.com/3090796.html