Introduction

GraphQL::ObjectTypes and GraphQL::InterfaceTypes may expose their values with fields. A field definition looks like this:

PostType = GraphQL::ObjectType.define do
  # ...
  #     name  , type        , description (optional)
  field :title, types.String, "The title of the Post"
end

By default, fields are resolved by sending the name to the underlying object (eg post.title in the example above).

You can define a different resolution by providing a resolve function:

PostType = GraphQL::ObjectType.define do
  # ...
  #     name   , type        , description (optional)
  field :teaser, types.String, "The teaser of the Post" do
    # how to get the value?
    resolve ->(obj, args, ctx) {
      # first 40 chars of the body
      obj.body[0, 40]
    }
  end
end

The resolve function receives inputs:

In fact, the field do ... end block is passed to GraphQL::Field’s .define method, so you can define many things there:

field do
  name "teaser"
  type types.String
  description "..."
  resolve ->(obj, args, ctx) { ... }
  deprecation_reason "Too long, use .title instead"
  complexity 2
end