Fields can take arguments as input. These can be used to determine the return value (eg, filtering search results) or to modify the application state (eg, updating the database in MutationType).

Arguments are defined with the argument helper:

field :search_posts, types[PostType] do
  argument :category, types.String
  resolve ->(obj, args, ctx) {
    # => maybe a string, eg "Programming"
    if args[:category]
      Post.where(category: category).limit(10)

Use ! to mark an argument as required:

# This argument is a required string:
argument :category, !types.String

Use default_value: value to provide a default value for the argument if not supplied in the query.

argument :category, types.String, default_value: "Programming"

Use as: :alternateName to use a different key from within your resolvers while exposing another key to clients.

field :post, PostType do
  argument :postId, types.Id, as: :id
  resolve ->(obj, args, ctx) {

Provide a prepare function to modify or validate the value of an argument before the field’s resolve function is executed:

field :posts, types[PostType] do
  argument :startDate, types.String, prepare: ->(startDate, ctx) {
    # return the prepared argument or"msg")
    # to halt the execution of the field and add "msg" to the `errors` key.
  resolve ->(obj, args, ctx) {
    # use prepared args['startDate']

Only certain types are valid for arguments:

The args parameter of a resolve function will always be a GraphQL::Query::Arguments. You can access specific arguments with ["arg_name"] or [:arg_name]. You recursively turn it into a Ruby Hash with to_h. Inside args, scalars will be parsed into Ruby values and enums will be converted to their value: (if one was provided).

resolve ->(obj, args, ctx) {
  args["category"] == args[:category]
  # => true
  # => { "category" => "Programming" }
  # ...