Graphql mutations without arguments
Asked Answered
K

3

23

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: {})
}
Krypton answered 23/1, 2018 at 22:51 Comment(1)
If you are encountering some error when trying to compile your schema or run a query, you should update your question to include that.Grecism
G
10

There is nothing in the GraphQL spec that specifically requires one or more arguments for a mutation. In terms of arguments, there's nothing special about either query or mutation -- they are both fields themselves, and therefore can accept (or not) arguments like any other field. From the spec:

Inputs (such as field arguments), are always optional by default. However a non‐null input type is required. In addition to not accepting the value null, it also does not accept omission. For the sake of simplicity nullable types are always optional and non‐null types are always required.

In other words, the only time you have to have an argument is when one is defined in the schema and it's non-null.

Grecism answered 23/1, 2018 at 23:34 Comment(1)
Daniel Rearden : Can you pls help me on this ---> #49281314Nonexistence
T
22

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.

Tyr answered 19/2, 2021 at 8:50 Comment(0)
G
10

There is nothing in the GraphQL spec that specifically requires one or more arguments for a mutation. In terms of arguments, there's nothing special about either query or mutation -- they are both fields themselves, and therefore can accept (or not) arguments like any other field. From the spec:

Inputs (such as field arguments), are always optional by default. However a non‐null input type is required. In addition to not accepting the value null, it also does not accept omission. For the sake of simplicity nullable types are always optional and non‐null types are always required.

In other words, the only time you have to have an argument is when one is defined in the schema and it's non-null.

Grecism answered 23/1, 2018 at 23:34 Comment(1)
Daniel Rearden : Can you pls help me on this ---> #49281314Nonexistence
H
1

With the graphql-request package in React you can do:

const query = gql`
  query {
    queryName {
      someProperty
    }
  }
`
Haviland answered 26/3, 2021 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.