Graphql-ruby objects

From wikinotes

Documentation

type docs https://graphql-ruby.org/guides#type-definitions-guides
field docs https://graphql-ruby.org/guides#fields-guides

Fields

All objects in graphql are exposed using fields.
Mutations are added as fields to the root Mutation object,
and Queries are added to the root Query object.

Fields

class User < GraphQL::Schema::Object
  field :id, Integer    # name, returntype
  field :age, Integer   # name, returntype

  def id
    return 100
  end

  def age
    return 30
  end
end

Parametrized Fields

USERS = [
  {id: 100, name: "vaderd"},
  {id: 101, name: "skywalkerl"},
]

class QueryRoot < GraphQL::Schema::Object
  field :user, User, do |field|
    field.argument(:id, Integer, required: true)
  end

  def user(id:)
    USERS.find { |user| user[:id] == id }
  end
end