module GraphQLDocs::Helpers

Constants

SLUGIFY_PRETTY_REGEXP

Attributes

templates[RW]

Public Instance Methods

graphql_directive_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 66
def graphql_directive_types
  @parsed_schema[:directive_types] || []
end
graphql_enum_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 50
def graphql_enum_types
  @parsed_schema[:enum_types] || []
end
graphql_input_object_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 58
def graphql_input_object_types
  @parsed_schema[:input_object_types] || []
end
graphql_interface_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 46
def graphql_interface_types
  @parsed_schema[:interface_types] || []
end
graphql_mutation_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 38
def graphql_mutation_types
  @parsed_schema[:mutation_types] || []
end
graphql_object_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 42
def graphql_object_types
  @parsed_schema[:object_types] || []
end
graphql_operation_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 34
def graphql_operation_types
  @parsed_schema[:operation_types] || []
end
graphql_root_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 30
def graphql_root_types
  @parsed_schema[:root_types] || []
end
graphql_scalar_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 62
def graphql_scalar_types
  @parsed_schema[:scalar_types] || []
end
graphql_union_types() click to toggle source
# File lib/graphql-docs/helpers.rb, line 54
def graphql_union_types
  @parsed_schema[:union_types] || []
end
include(filename, opts = {}) click to toggle source
# File lib/graphql-docs/helpers.rb, line 17
def include(filename, opts = {})
  template = fetch_include(filename)
  opts = { base_url: @options[:base_url], classes: @options[:classes] }.merge(opts)
  template.result(OpenStruct.new(opts.merge(helper_methods)).instance_eval { binding })
end
markdownify(string) click to toggle source
# File lib/graphql-docs/helpers.rb, line 23
def markdownify(string)
  return '' if string.nil?

  type = @options[:pipeline_config][:context][:unsafe] ? :UNSAFE : :DEFAULT
  ::CommonMarker.render_html(string, type).strip
end
slugify(str) click to toggle source
# File lib/graphql-docs/helpers.rb, line 11
def slugify(str)
  slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-')
  slug.gsub!(/^\-|\-$/i, '')
  slug.downcase
end
split_into_metadata_and_contents(contents, parse: true) click to toggle source
# File lib/graphql-docs/helpers.rb, line 70
def split_into_metadata_and_contents(contents, parse: true)
  pieces = yaml_split(contents)
  raise "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format." if pieces.size < 4

  # Parse
  begin
    meta = if parse
             YAML.safe_load(pieces[2]) || {}
           else
             pieces[2]
           end
  rescue Exception => e # rubocop:disable Lint/RescueException
    raise "Could not parse YAML for #{name}: #{e.message}"
  end
  [meta, pieces[4]]
end
yaml?(contents) click to toggle source
# File lib/graphql-docs/helpers.rb, line 87
def yaml?(contents)
  contents =~ /\A-{3,5}\s*$/
end
yaml_split(contents) click to toggle source
# File lib/graphql-docs/helpers.rb, line 91
def yaml_split(contents)
  contents.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3)
end

Private Instance Methods

fetch_include(filename) click to toggle source
# File lib/graphql-docs/helpers.rb, line 97
def fetch_include(filename)
  @templates ||= {}

  return @templates[filename] unless @templates[filename].nil?

  contents = File.read(File.join(@options[:templates][:includes], filename))

  @templates[filename] = ERB.new(contents)
end
helper_methods() click to toggle source
# File lib/graphql-docs/helpers.rb, line 107
def helper_methods
  return @helper_methods if defined?(@helper_methods)

  @helper_methods = {}

  Helpers.instance_methods.each do |name|
    next if name == :helper_methods

    @helper_methods[name] = method(name)
  end

  @helper_methods
end