Module: GraphQLDocs::Helpers

Included in:
Generator, Parser, Renderer
Defined in:
lib/graphql-docs/helpers.rb

Constant Summary collapse

SLUGIFY_PRETTY_REGEXP =
Regexp.new("[^[:alnum:]._~!$&'()+,;=@]+").freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#templatesObject

Returns the value of attribute templates.



9
10
11
# File 'lib/graphql-docs/helpers.rb', line 9

def templates
  @templates
end

Instance Method Details

#graphql_directive_typesObject



65
66
67
# File 'lib/graphql-docs/helpers.rb', line 65

def graphql_directive_types
  @parsed_schema[:directive_types] || []
end

#graphql_enum_typesObject



49
50
51
# File 'lib/graphql-docs/helpers.rb', line 49

def graphql_enum_types
  @parsed_schema[:enum_types] || []
end

#graphql_input_object_typesObject



57
58
59
# File 'lib/graphql-docs/helpers.rb', line 57

def graphql_input_object_types
  @parsed_schema[:input_object_types] || []
end

#graphql_interface_typesObject



45
46
47
# File 'lib/graphql-docs/helpers.rb', line 45

def graphql_interface_types
  @parsed_schema[:interface_types] || []
end

#graphql_mutation_typesObject



37
38
39
# File 'lib/graphql-docs/helpers.rb', line 37

def graphql_mutation_types
  @parsed_schema[:mutation_types] || []
end

#graphql_object_typesObject



41
42
43
# File 'lib/graphql-docs/helpers.rb', line 41

def graphql_object_types
  @parsed_schema[:object_types] || []
end

#graphql_operation_typesObject



33
34
35
# File 'lib/graphql-docs/helpers.rb', line 33

def graphql_operation_types
  @parsed_schema[:operation_types] || []
end

#graphql_root_typesObject



29
30
31
# File 'lib/graphql-docs/helpers.rb', line 29

def graphql_root_types
  @parsed_schema[:root_types] || []
end

#graphql_scalar_typesObject



61
62
63
# File 'lib/graphql-docs/helpers.rb', line 61

def graphql_scalar_types
  @parsed_schema[:scalar_types] || []
end

#graphql_union_typesObject



53
54
55
# File 'lib/graphql-docs/helpers.rb', line 53

def graphql_union_types
  @parsed_schema[:union_types] || []
end

#has_yaml?(contents) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/graphql-docs/helpers.rb', line 90

def has_yaml?(contents)
  contents =~ /\A-{3,5}\s*$/
end

#include(filename, opts = {}) ⇒ Object



17
18
19
20
21
# 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) ⇒ Object



23
24
25
26
27
# 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) ⇒ Object



11
12
13
14
15
# File 'lib/graphql-docs/helpers.rb', line 11

def slugify(str)
  slug = str.gsub(SLUGIFY_PRETTY_REGEXP, '-')
  slug.gsub!(%r!^\-|\-$!i, '')
  slug.downcase
end

#split_into_metadata_and_contents(contents, parse: true) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/graphql-docs/helpers.rb', line 69

def (contents, parse: true)
  opts = {}
  pieces = yaml_split(contents)
  if pieces.size < 4
    raise RuntimeError.new(
      "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.",
    )
  end
  # Parse
  begin
    if parse
      meta = YAML.load(pieces[2]) || {}
    else
      meta = 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_split(contents) ⇒ Object



94
95
96
# File 'lib/graphql-docs/helpers.rb', line 94

def yaml_split(contents)
  contents.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3)
end