class GraphQL::RakeTask
GraphQL::RakeTask.new(schema_name: “MySchema”, include_is_one_of: true)
require “graphql/rake_task”
@example Providing arguments to build the introspection query<br><br>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
Use ‘load_context:` and `visible?` to dump schemas under certain visibility constraints.
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 = case method_name when :to_json schema.to_json( include_is_one_of: include_is_one_of, include_deprecated_args: include_deprecated_args, include_is_repeatable: include_is_repeatable, include_specified_by_url: include_specified_by_url, include_schema_description: include_schema_description, context: context ) when :to_definition schema.to_definition(context: context) else raise ArgumentError, "Unexpected schema dump method: #{method_name.inspect}" end dir = File.dirname(file) FileUtils.mkdir_p(dir) if !result.end_with?("\n") result += "\n" end File.write(file, result) end