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
@return [Array<String>]
@return [String] directory for IDL & JSON files
@return [<#call(member, ctx)>, nil] A filter for this task
@return [String] target for IDL task
@return [String] target for JSON task
@return [<#call(task)>] A callable for loading the query context
@return [<#call(task)>] A proc for loading the target GraphQL
schema
@return [String] Namespace for generated tasks
@return [<#call(member, ctx)>, nil] A filter for this task
@return [String] By default, used to find the schema as a constant. @see {#load_schema} for loading a schema another way
Public Class Methods
Set the parameters of this task by passing keyword arguments or assigning attributes inside the block
# File lib/graphql/rake_task.rb, line 79 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
# File lib/graphql/rake_task.rb, line 45 def rake_namespace @namespace end
Private Instance Methods
Use the Rake DSL to add tasks
# File lib/graphql/rake_task.rb, line 123 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
# File lib/graphql/rake_task.rb, line 108 def idl_path File.join(@directory, @idl_outfile) end
# File lib/graphql/rake_task.rb, line 112 def json_path File.join(@directory, @json_outfile) end
# File lib/graphql/rake_task.rb, line 116 def load_rails_environment_if_defined if Rake::Task.task_defined?('environment') Rake::Task['environment'].invoke end end
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 96 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