Class: GraphQLDocs::Renderer

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

Constant Summary

Constants included from Helpers

Helpers::SLUGIFY_PRETTY_REGEXP

Instance Attribute Summary

Attributes included from Helpers

#templates

Instance Method Summary collapse

Methods included from Helpers

#format_type, #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(options, parsed_schema) ⇒ Renderer

Returns a new instance of Renderer.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/graphql-docs/renderer.rb', line 8

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

  unless @options[:templates][:default].nil?
    @graphql_default_layout = ERB.new(File.read(@options[:templates][:default]))
  end

  @pipeline_config = @options[:pipeline_config]

  filters = @pipeline_config[:pipeline].map do |f|
    if filter?(f)
      f
    else
      key = filter_key(f)
      filter = HTML::Pipeline.constants.find { |c| c.downcase == key }
      # possibly a custom filter
      if filter.nil?
        Kernel.const_get(f)
      else
        HTML::Pipeline.const_get(filter)
      end
    end
  end

  @pipeline = HTML::Pipeline.new(filters, @pipeline_config[:context])
end

Instance Method Details

#render(type, name, contents) ⇒ Object



36
37
38
39
40
41
# File 'lib/graphql-docs/renderer.rb', line 36

def render(type, name, contents)
  contents = @pipeline.to_html(contents)
  return contents if @graphql_default_layout.nil?
  opts = { base_url: @options[:base_url] }.merge({ contents: contents, type: type, name: name}).merge(helper_methods)
  @graphql_default_layout.result(OpenStruct.new(opts).instance_eval { binding })
end