Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to not return any data when using a GraphQL mutation?

Tags:

graphql

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.

like image 340
angrykoala Avatar asked Jun 24 '17 13:06

angrykoala


People also ask

What does GraphQL mutation return?

In this chapter, we will learn mutation queries in GraphQL. Mutation queries modify data in the data store and returns a value. It can be used to insert, update, or delete data.

How do mutations work in GraphQL?

Mutations allow you to modify server-side data, and it also returns an object based on the operation performed. It can be used to insert, update, or delete data. Dgraph automatically generates GraphQL mutations for each type that you define in your schema.

Can GraphQL return an object?

In other words, if you return an empty object ( {} ), an empty array ( [] ) or some other value, GraphQL will treat this as you returning an object and not a null value!

Can GraphQL return null?

Learn how to define nullable and non-nullable fields your GraphQL schema with the schema-first approach. The GraphQL type system has built-in support for null, and non-null fields. GraphQL is null by default. If you've worked with GraphQL before this probably looks very familiar.


1 Answers

According to this Github issue you cannot return nothing.

You can define a return type which is nullable e.g.

type Mutation {    addElement(element: ElementData): ID    removeElement(id: ID): Boolean  }

But I suggest you return the id of the deleted element, because if you want to work with a cached store you have to update the store when the delete mutation has ran successfully.

like image 85
Locco0_0 Avatar answered Sep 22 '22 06:09

Locco0_0