Class: GraphQLDocs::Generator

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/graphql-docs/generator.rb

Constant Summary

Constants included from Helpers

Helpers::SLUGIFY_PRETTY_REGEXP

Instance Attribute Summary collapse

Attributes included from Helpers

#templates

Instance Method Summary collapse

Methods included from Helpers

#graphql_enum_types, #graphql_input_object_types, #graphql_interface_types, #graphql_mutation_types, #graphql_object_types, #graphql_scalar_types, #graphql_union_types, #include, #markdown, #slugify

Constructor Details

#initialize(parsed_schema, options) ⇒ Generator

Returns a new instance of Generator.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/graphql-docs/generator.rb', line 9

def initialize(parsed_schema, options)
  @parsed_schema = parsed_schema
  @options = options

  @renderer = @options[:renderer].new(@options, @parsed_schema)

  @graphql_object_template = ERB.new(File.read(@options[:templates][:objects]))
  @graphql_mutations_template = ERB.new(File.read(@options[:templates][:mutations]))
  @graphql_interfaces_template = ERB.new(File.read(@options[:templates][:interfaces]))
  @graphql_enums_template = ERB.new(File.read(@options[:templates][:enums]))
  @graphql_unions_template = ERB.new(File.read(@options[:templates][:unions]))
  @graphql_input_objects_template = ERB.new(File.read(@options[:templates][:input_objects]))
  @graphql_scalars_template = ERB.new(File.read(@options[:templates][:scalars]))
end

Instance Attribute Details

#parsed_schemaObject

Returns the value of attribute parsed_schema.



7
8
9
# File 'lib/graphql-docs/generator.rb', line 7

def parsed_schema
  @parsed_schema
end

Instance Method Details

#create_graphql_enum_pagesObject



70
71
72
73
74
75
76
77
# File 'lib/graphql-docs/generator.rb', line 70

def create_graphql_enum_pages
  graphql_enum_types.each do |enum_type|
    opts = { type: enum_type }.merge(helper_methods)

    contents = @graphql_enums_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('enum', enum_type['name'], contents)
  end
end

#create_graphql_input_object_pagesObject



88
89
90
91
92
93
94
95
# File 'lib/graphql-docs/generator.rb', line 88

def create_graphql_input_object_pages
  graphql_input_object_types.each do |input_object_type|
    opts = { type: input_object_type }.merge(helper_methods)

    contents = @graphql_input_objects_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('input_object', input_object_type['name'], contents)
  end
end

#create_graphql_interface_pagesObject



61
62
63
64
65
66
67
68
# File 'lib/graphql-docs/generator.rb', line 61

def create_graphql_interface_pages
  graphql_interface_types.each do |interface_type|
    opts = { type: interface_type }.merge(helper_methods)

    contents = @graphql_interfaces_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('interface', interface_type['name'], contents)
  end
end

#create_graphql_mutation_pagesObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/graphql-docs/generator.rb', line 47

def create_graphql_mutation_pages
  graphql_mutation_types.each do |mutation|
    input_name = mutation['args'].first['type']['ofType']['name']
    return_name = mutation['type']['name']
    input = graphql_input_object_types.find { |t| t['name'] == input_name }
    payload = graphql_object_types.find { |t| t['name'] == return_name }

    opts = { type: mutation, input_fields: input, return_fields: payload }.merge(helper_methods)

    contents = @graphql_mutations_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('mutation', mutation['name'], contents)
  end
end

#create_graphql_object_pagesObject



38
39
40
41
42
43
44
45
# File 'lib/graphql-docs/generator.rb', line 38

def create_graphql_object_pages
  graphql_object_types.each do |object_type|
    next if object_type['name'].start_with?('__')
    opts = { type: object_type }.merge(helper_methods)
    contents = @graphql_object_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('object', object_type['name'], contents)
  end
end

#create_graphql_scalar_pagesObject



97
98
99
100
101
102
103
104
# File 'lib/graphql-docs/generator.rb', line 97

def create_graphql_scalar_pages
  graphql_scalar_types.each do |scalar_type|
    opts = { type: scalar_type }.merge(helper_methods)

    contents = @graphql_scalars_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('scalar', scalar_type['name'], contents)
  end
end

#create_graphql_union_pagesObject



79
80
81
82
83
84
85
86
# File 'lib/graphql-docs/generator.rb', line 79

def create_graphql_union_pages
  graphql_union_types.each do |union_type|
    opts = { type: union_type }.merge(helper_methods)

    contents = @graphql_unions_template.result(OpenStruct.new(opts).instance_eval { binding })
    write_file('union', union_type['name'], contents)
  end
end

#generateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphql-docs/generator.rb', line 24

def generate
  FileUtils.rm_rf(@options[:output_dir]) if @options[:delete_output]

  create_graphql_object_pages
  create_graphql_mutation_pages
  create_graphql_interface_pages
  create_graphql_enum_pages
  create_graphql_union_pages
  create_graphql_input_object_pages
  create_graphql_scalar_pages

  true
end