问题描述
我有几个 GraphQL 查询和变更,现在我正在尝试实现一个 delete 变更而不返回任何数据:
type Mutation{addElement(element: ElementData): IDremoveElement(id: ID): ?}
不过好像是要求删除操作有返回值.有没有办法在 GraphQL 中执行空"响应?如果可能,我想避免诸如返回布尔值或状态标志之类的事情.
我不确定 GraphQL 删除操作的最佳实践是什么.
解决方案(A) Solution with graphql-scalars
原始答案如下.
这是另一种使用 graphql-scalars
图书馆:
npm install graphql-scalars
然后导入他们的 Void
类型:https://www.graphql-scalars.dev/docs/scalars/void(B) 使用自定义 scalar
的解决方案<块引用>注意:使用 void
-result from mutations 的设计违背了 GQL 最佳实践"
这个例子是为 NodeJS Apollo 框架编写的,但是很容易转换你的语言/框架的实现
我很确定:有一个名为 graphql-void
的 NPM 包,但如果您不想添加另一个依赖项,只需复制此代码即可.
1.在你的架构中定义 Void
-scalar
# 文件:./schema.gql标量空
2.实现解析器
//文件 ./scalar-void.js从'graphql'导入{GraphQLScalarType}const Void = 新 GraphQLScalarType({name: '空',描述:'代表空值',序列化(){返回空},解析值(){返回空},解析文字(){返回空}})出口无效
3.将解析器添加到 ApolloServer
将 Void
解析器添加到 Apollo Server 实例的选项中:
# 文件:./server.js从apollo-server-express"导入 { ApolloServer }从 './scalar-void' 导入 { Void }const server = new ApolloServer({typeDefs,//使用你的架构解析器:{虚空:虚空,//... 你的解析器},})
4.将 Void
用于架构中的变更
最后,在您的架构中使用新的scalar
:
# 文件:./schema.gql类型突变{addElement(element: ElementData): IDremoveElement(id: ID): 无效}
I have several GraphQL queries and mutations, now I'm trying to implement a delete mutation without returning any data:
type Mutation{
addElement(element: ElementData): ID
removeElement(id: ID): ¿?
}
However, it seems to be required to have a return value for the delete operation. Is there a way to perform an "empty" response in GraphQL? I would like to avoid things like returning a boolean or status flag if possible.
I'm not sure on what are the best practices for GraphQL delete operations.
解决方案(A) Solution with graphql-scalars
The original answer is below.
Here is another one solution with graphql-scalars
library:
npm install graphql-scalars
and then
import their Void
type: https://www.graphql-scalars.dev/docs/scalars/void
(B) Solution with a custom scalar
Note: design with
void
-result from mutations goes against "GQL best practices"
This example was written for NodeJS Apollo Framework, but it is pretty easy to convert the implementation for your language/framework
I'm pretty sure: there is an NPM-package named graphql-void
but if you don't want to add another one dependency just copy this code.
1. define Void
-scalar in your schema
# file: ./schema.gql
scalar Void
2. implement resolver
// file ./scalar-void.js
import { GraphQLScalarType } from 'graphql'
const Void = new GraphQLScalarType({
name: 'Void',
description: 'Represents NULL values',
serialize() {
return null
},
parseValue() {
return null
},
parseLiteral() {
return null
}
})
export Void
3. add the resolver to ApolloServer
Add the Void
resolver to the options of your instance of Apollo Server:
# file: ./server.js
import { ApolloServer } from 'apollo-server-express'
import { Void } from './scalar-void'
const server = new ApolloServer({
typeDefs, // use your schema
resolvers: {
Void: Void,
// ... your resolvers
},
})
4. use Void
for your mutations in the schema
Finally, use the new scalar
in your schema:
# file: ./schema.gql
type Mutation{
addElement(element: ElementData): ID
removeElement(id: ID): Void
}
这篇关于使用 GraphQL 突变时是否可以不返回任何数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论