How to correctly declare a GraphQL query without parameters.

The queries you specify in your schema behave just like any other field on a particular type (the main difference is that their type is linked to a particular operation). If you don’t want to declare any arguments for a particular field, you just omit the parentheses entirely. The same goes for queries and mutations:

type Query {
  users: Int
}

From the spec:

Fields are conceptually functions which return values, and occasionally accept arguments which alter their behavior. These arguments often map directly to function arguments within a GraphQL server’s implementation.

So it’s worthwhile pointing out that any Type’s field could have arguments. For example, a query could look like this:

query UsersQuery {
  users {
    name
    posts (onlyNew: true) {
      title
    }
  }
}

Leave a Comment