Parsing GraphQL Schema Definition Language into a Ruby Schema

GraphQL-Ruby includes a way to build a runable schema from the GraphQL Schema Definition Language (SDL). GraphQL::Schema.from_definition returns a schema class based on a filename or string containing GraphQL SDL. For example:

# From a file:
MySchema = GraphQL::Schema.from_definition("path/to/schema.graphql")
# Or, from a string:
MySchema = GraphQL::Schema.from_definition(<<~GRAPHQL)
  type Query {
    # ...
  # ...

Definitions from the SDL are converted into Ruby classes, similar to those defined in plain Ruby code.


You can provide execution behaviors to a generated schema as default_resolve:, which accepts two kinds of values:

Implementation Object

By providing an object that implements several runtime methods, you can define the execution behaviors of a schema loaded from SDL:

class SchemaImplementation
  # see below for methods

# Pass the object as `default_resolve:`
MySchema = GraphQL::Schema.from_definition(

The default_resolve: object may implement:

Implementation Hash

Alternatively, you can provide a Hash containing callable behaviors, for example:

schema_implementation = {
  # ... see below for hash structure

# Pass the hash as `default_resolve:`
MySchema = GraphQL::Schema.from_definition(
  default_resolve: schema_implementation

The hash may contain:


GraphQL::Schema.from_definition accepts a using: argument, which may be given as a map of plugin => args pairs. For example:

MySchema = GraphQL::Schema.from_definition("path/to/schema.graphql", using: {
  GraphQL::Pro::PusherSubscriptions => { redis: $redis },
  GraphQL::Pro::OperationStore => nil, # no options here


Although GraphQL-Ruby doesn’t have special handling for directives in the SDL, you can build custom behavior in your own app. If part of the schema had a directive, you can access it using .ast_node.directives. For example:

schema = GraphQL::Schema.from_definition <<-GRAPHQL
type Query @flagged {
  secret: Boolean @privacy(secret: true)

# => ["@flagged"]
pp schema.get_field("Query", "secret")
# => ["@privacy(secret: true)"]

See GraphQL::Language::Nodes::Directive for available methods.