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-scalarsThe original answer is below.
Here is another one solution with
graphql-scalarslibrary:npm install graphql-scalarsand thenVoidtype: https://www.graphql-scalars.dev/docs/scalars/void(B) Solution with a custom
scalarThis 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-voidbut if you don't want to add another one dependency just copy this code.1. define
Void-scalar in your schema2. implement resolver
3. add the resolver to ApolloServer
Add the
Voidresolver to the options of your instance of Apollo Server:4. use
Voidfor your mutations in the schemaFinally, use the new
scalarin your schema: