Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphql mutations without arguments

Generally a query is when you fetch data and mutation is when you manipulate data. But how would I implement a mutation without any arguments?

In my particular case I have delete and create 2fa token endpoints. Both the delete and create token have no arguments as they rely on the logged in user id. They both either destroy or create a record in the database. So I would prefer that they be mutations. But that is not possible?

I'm using Graphql-Ruby. But this is more of a general Graphql question.

EDIT:

So turns out I was wrong. I couldn't find any info about it so I just assumed it wasn't possible. I hope this helps someone else. In Graphql-Ruby you can do:

mutation {
  createFoo(input: {})
}
like image 862
dan-klasson Avatar asked Jan 23 '18 22:01

dan-klasson


People also ask

How do mutations work in GraphQL?

A Mutation is a GraphQL Operation that allows you to insert new data or modify the existing data on the server-side. You can think of GraphQL Mutations as the equivalent of POST , PUT , PATCH and DELETE requests in REST. The above mutation inserts a new todo note into the database.

What is difference between query and mutation in GraphQL?

In a GraphQL API, queries are used to fetch data from a server, while mutations are used to modify or write server-side data. Therefore, it is essential to understand its structure when defining a mutation, including the type definition, the mutation name, and variable definitions.

How do you use update mutations in GraphQL?

Update mutations take filter as an input to select specific objects. You can specify set and remove operations on fields belonging to the filtered objects. It returns the state of the objects after updating. Note Executing an empty remove {} or an empty set{} doesn't have any effect on the update mutation.


1 Answers

If a mutation doesn't declare any parameters than it must looks this way:

mutation {
  createFoo : Payload
}

So you just must not to write parentheses at all.

like image 153
Andrei Avatar answered Sep 17 '22 18:09

Andrei