class GraphQL::RakeTask

A rake task for dumping a schema as IDL or JSON.

By default, schemas are looked up by name as constants using `schema_name:`. You can provide a `load_schema` function to return your schema another way.

`load_context:`, `only:` and `except:` are supported so that you can keep an eye on how filters affect your schema.

@example Dump a Schema to .graphql + .json files

require "graphql/rake_task"
GraphQL::RakeTask.new(schema_name: "MySchema")

# $ rake graphql:schema:dump
# Schema IDL dumped to ./schema.graphql
# Schema JSON dumped to ./schema.json

@example Invoking the task from Ruby

require "rake"
Rake::Task["graphql:schema:dump"].invoke

Constants

DEFAULT_OPTIONS

Attributes

dependencies[RW]

@return [Array<String>]

directory[RW]

@return [String] directory for IDL & JSON files

except[RW]

@return [<#call(member, ctx)>, nil] A filter for this task

idl_outfile[RW]

@return [String] target for IDL task

json_outfile[RW]

@return [String] target for JSON task

load_context[RW]

@return [<#call(task)>] A callable for loading the query context

load_schema[RW]

@return [<#call(task)>] A proc for loading the target GraphQL schema

namespace[W]

@return [String] Namespace for generated tasks

only[RW]

@return [<#call(member, ctx)>, nil] A filter for this task

schema_name[RW]

@return [String] By default, used to find the schema as a constant. @see {#load_schema} for loading a schema another way

Public Class Methods

new(options = {}) { |self| ... } click to toggle source

Set the parameters of this task by passing keyword arguments or assigning attributes inside the block

# File lib/graphql/rake_task.rb, line 78
def initialize(options = {})
  all_options = DEFAULT_OPTIONS.merge(options)
  all_options.each do |k, v|
    self.public_send("#{k}=", v)
  end

  if block_given?
    yield(self)
  end

  define_task
end

Public Instance Methods

rake_namespace() click to toggle source
# File lib/graphql/rake_task.rb, line 44
def rake_namespace
  @namespace
end

Private Instance Methods

define_task() click to toggle source

Use the Rake DSL to add tasks

# File lib/graphql/rake_task.rb, line 122
def define_task
  namespace(@namespace) do
    namespace("schema") do
      desc("Dump the schema to IDL in #{idl_path}")
      task :idl => @dependencies do
        load_rails_environment_if_defined
        write_outfile(:to_definition, idl_path)
        puts "Schema IDL dumped into #{idl_path}"
      end

      desc("Dump the schema to JSON in #{json_path}")
      task :json => @dependencies do
        load_rails_environment_if_defined
        write_outfile(:to_json, json_path)
        puts "Schema JSON dumped into #{json_path}"
      end

      desc("Dump the schema to JSON and IDL")
      task :dump => [:idl, :json]
    end
  end
end
idl_path() click to toggle source
# File lib/graphql/rake_task.rb, line 107
def idl_path
  File.join(@directory, @idl_outfile)
end
json_path() click to toggle source
# File lib/graphql/rake_task.rb, line 111
def json_path
  File.join(@directory, @json_outfile)
end
load_rails_environment_if_defined() click to toggle source
# File lib/graphql/rake_task.rb, line 115
def load_rails_environment_if_defined
  if Rake::Task.task_defined?('environment')
    Rake::Task['environment'].invoke
  end
end
write_outfile(method_name, file) click to toggle source

Use the provided `method_name` to generate a string from the specified schema then write it to `file`.

# File lib/graphql/rake_task.rb, line 95
def write_outfile(method_name, file)
  schema = @load_schema.call(self)
  context = @load_context.call(self)
  result = schema.public_send(method_name, only: @only, except: @except, context: context)
  dir = File.dirname(file)
  FileUtils.mkdir_p(dir)
  if !result.end_with?("\n")
    result += "\n"
  end
  File.write(file, result)
end