class GraphQL::RakeTask
Rake::Task.invoke
require “rake”
@example Invoking the task from Ruby
# Schema JSON dumped to ./schema.json
# Schema IDL dumped to ./schema.graphql
# $ rake graphql:schema:dump
GraphQL::RakeTask.new(schema_name: “MySchema”)
require “graphql/rake_task”
@example Dump a Schema to .graphql + .json files
you can keep an eye on how filters affect your schema.
‘load_context:`, `only:` and `except:` are supported so that
You can provide a `load_schema` function to return your schema another way.
By default, schemas are looked up by name as constants using `schema_name:`.
A rake task for dumping a schema as IDL or JSON.
def define_task
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
def idl_path
def idl_path File.join(@directory, @idl_outfile) end
def initialize(options = {})
Set the parameters of this task by passing keyword arguments
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
def json_path
def json_path File.join(@directory, @json_outfile) end
def load_rails_environment_if_defined
def load_rails_environment_if_defined if Rake::Task.task_defined?('environment') Rake::Task['environment'].invoke end end
def rake_namespace
def rake_namespace @namespace end
def write_outfile(method_name, file)
Use the provided `method_name` to generate a string from the specified schema
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) File.write(file, result) end