12
votes

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: {})
}
3
If you are encountering some error when trying to compile your schema or run a query, you should update your question to include that.Daniel Rearden

3 Answers

8
votes

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.

6
votes

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.

1
votes

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

const query = gql`
  query {
    queryName {
      someProperty
    }
  }
`