Definition

A GraphQL system is called a schema. The schema contains all the types and fields in the system. The schema executes queries and publishes an introspection system.

Your GraphQL schema is a class that extends GraphQL::Schema, for example:

class MyAppSchema < GraphQL::Schema
  max_complexity 400
  query Types::Query
  use GraphQL::Dataloader

  # Define hooks as class methods:
  def self.resolve_type(type, obj, ctx)
    # ...
  end

  def self.object_from_id(node_id, ctx)
    # ...
  end

  def self.id_from_object(object, type, ctx)
    # ...
  end
end

There are lots of schema configuration methods.

For defining GraphQL types, see the guides for those types: object types, interface types, union types, input object types, enum types, and scalar types.

Types in the Schema

Lazy-loading types

In development, GraphQL-Ruby can defer loading your type definitions until they’re needed. This requires some configuration to opt in:

To enforce these patterns, you can enable two Rubocop rules that ship with GraphQL-Ruby:

Object Identification

Some GraphQL features use unique IDs to load objects:

To use these features, you must provide some methods for generating UUIDs and fetching objects with them:

Schema.object_from_id is called by GraphQL-Ruby to load objects directly from the database. It’s usually used by the node(id: ID!): Node field (see GraphQL::Types::Relay::Node), Argument loads:, or the ObjectCache. It receives a unique ID and must return the object for that ID, or nil if the object isn’t found (or if it should be hidden from the current user).

Schema.id_from_object is used to implement Node.id. It should return a unique ID for the given object. This ID will later be sent to object_from_id to refetch the object.

Additionally, Schema.resolve_type is called by GraphQL-Ruby to get the runtime Object type for fields that return return interface or union types.

Error Handling

Default Limits

Introspection

Authorization

Execution Configuration

Plugins

Production Considerations