Class: GraphQL::RakeTask

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/graphql/rake_task.rb

Overview

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.

Examples:

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

Invoking the task from Ruby

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

Constant Summary collapse

DEFAULT_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",
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RakeTask

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/graphql/rake_task.rb', line 76

def initialize(options = {})
  default_dependencies = if Rake::Task.task_defined?("environment")
    [:environment]
  else
    []
  end

  all_options = DEFAULT_OPTIONS
    .merge(dependencies: default_dependencies)
    .merge(options)
  all_options.each do |k, v|
    self.public_send("#{k}=", v)
  end

  if block_given?
    yield(self)
  end

  define_task
end

Instance Attribute Details

#dependenciesArray<String>



47
48
49
# File 'lib/graphql/rake_task.rb', line 47

def dependencies
  @dependencies
end

#directoryString



72
73
74
# File 'lib/graphql/rake_task.rb', line 72

def directory
  @directory
end

#except<#call(member, ctx)>?



63
64
65
# File 'lib/graphql/rake_task.rb', line 63

def except
  @except
end

#idl_outfileString



66
67
68
# File 'lib/graphql/rake_task.rb', line 66

def idl_outfile
  @idl_outfile
end

#json_outfileString



69
70
71
# File 'lib/graphql/rake_task.rb', line 69

def json_outfile
  @json_outfile
end

#load_context<#call(task)>



57
58
59
# File 'lib/graphql/rake_task.rb', line 57

def load_context
  @load_context
end

#load_schema<#call(task)>



54
55
56
# File 'lib/graphql/rake_task.rb', line 54

def load_schema
  @load_schema
end

#namespace=(value) ⇒ String (writeonly)



40
41
42
# File 'lib/graphql/rake_task.rb', line 40

def namespace=(value)
  @namespace = value
end

#only<#call(member, ctx)>?



60
61
62
# File 'lib/graphql/rake_task.rb', line 60

def only
  @only
end

#schema_nameString

Returns By default, used to find the schema as a constant.

See Also:

  • for loading a schema another way


51
52
53
# File 'lib/graphql/rake_task.rb', line 51

def schema_name
  @schema_name
end

Instance Method Details

#rake_namespaceObject



42
43
44
# File 'lib/graphql/rake_task.rb', line 42

def rake_namespace
  @namespace
end