# frozen_string_literal: truerequire"fileutils"require"rake"require"graphql/rake_task/validate"moduleGraphQL# 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## @example Providing arguments to build the introspection query# require "graphql/rake_task"# GraphQL::RakeTask.new(schema_name: "MySchema", include_is_one_of: true)classRakeTaskincludeRake::DSLDEFAULT_OPTIONS={namespace: "graphql",dependencies: nil,schema_name: nil,load_schema: ->(task){Object.const_get(task.schema_name)},load_context: ->(task){{}},only: nil,except: nil,directory: ".",idl_outfile: "schema.graphql",json_outfile: "schema.json",include_deprecated_args: true,include_schema_description: false,include_is_repeatable: false,include_specified_by_url: false,include_is_one_of: false}# @return [String] Namespace for generated tasksattr_writer:namespacedefrake_namespace@namespaceend# @return [Array<String>]attr_accessor:dependencies# @return [String] By default, used to find the schema as a constant.# @see {#load_schema} for loading a schema another wayattr_accessor:schema_name# @return [<#call(task)>] A proc for loading the target GraphQL schemaattr_accessor:load_schema# @return [<#call(task)>] A callable for loading the query contextattr_accessor:load_context# @return [<#call(member, ctx)>, nil] A filter for this taskattr_accessor:only# @return [<#call(member, ctx)>, nil] A filter for this taskattr_accessor:except# @return [String] target for IDL taskattr_accessor:idl_outfile# @return [String] target for JSON taskattr_accessor:json_outfile# @return [String] directory for IDL & JSON filesattr_accessor:directory# @return [Boolean] Options for additional fields in the introspection query JSON response# @see GraphQL::Schema.as_jsonattr_accessor:include_deprecated_args,:include_schema_description,:include_is_repeatable,:include_specified_by_url,:include_is_one_of# Set the parameters of this task by passing keyword arguments# or assigning attributes inside the blockdefinitialize(options={})all_options=DEFAULT_OPTIONS.merge(options)all_options.eachdo|k,v|self.public_send("#{k}=",v)endifblock_given?yield(self)enddefine_taskendprivate# Use the provided `method_name` to generate a string from the specified schema# then write it to `file`.defwrite_outfile(method_name,file)schema=@load_schema.call(self)context=@load_context.call(self)result=casemethod_namewhen:to_jsonschema.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,only: @only,except: @except,context: context)when:to_definitionschema.to_definition(only: @only,except: @except,context: context)elseraiseArgumentError,"Unexpected schema dump method: #{method_name.inspect}"enddir=File.dirname(file)FileUtils.mkdir_p(dir)if!result.end_with?("\n")result+="\n"endFile.write(file,result)enddefidl_pathFile.join(@directory,@idl_outfile)enddefjson_pathFile.join(@directory,@json_outfile)enddefload_rails_environment_if_definedifRake::Task.task_defined?('environment')Rake::Task['environment'].invokeendend# Use the Rake DSL to add tasksdefdefine_tasknamespace(@namespace)donamespace("schema")dodesc("Dump the schema to IDL in #{idl_path}")task:idl=>@dependenciesdoload_rails_environment_if_definedwrite_outfile(:to_definition,idl_path)puts"Schema IDL dumped into #{idl_path}"enddesc("Dump the schema to JSON in #{json_path}")task:json=>@dependenciesdoload_rails_environment_if_definedwrite_outfile(:to_json,json_path)puts"Schema JSON dumped into #{json_path}"enddesc("Dump the schema to JSON and IDL")task:dump=>[:idl,:json]endendendendend