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.



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

def templates
  @templates
end

Instance Method Details

#graphql_directive_typesObject



67
68
69
# File 'lib/graphql-docs/helpers.rb', line 67

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

#graphql_enum_typesObject



51
52
53
# File 'lib/graphql-docs/helpers.rb', line 51

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

#graphql_input_object_typesObject



59
60
61
# File 'lib/graphql-docs/helpers.rb', line 59

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

#graphql_interface_typesObject



47
48
49
# File 'lib/graphql-docs/helpers.rb', line 47

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

#graphql_mutation_typesObject



39
40
41
# File 'lib/graphql-docs/helpers.rb', line 39

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

#graphql_object_typesObject



43
44
45
# File 'lib/graphql-docs/helpers.rb', line 43

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

#graphql_operation_typesObject



35
36
37
# File 'lib/graphql-docs/helpers.rb', line 35

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

#graphql_root_typesObject



31
32
33
# File 'lib/graphql-docs/helpers.rb', line 31

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

#graphql_scalar_typesObject



63
64
65
# File 'lib/graphql-docs/helpers.rb', line 63

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

#graphql_union_typesObject



55
56
57
# File 'lib/graphql-docs/helpers.rb', line 55

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

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



18
19
20
21
22
# File 'lib/graphql-docs/helpers.rb', line 18

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



24
25
26
27
28
29
# File 'lib/graphql-docs/helpers.rb', line 24

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



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

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

#split_into_metadata_and_contents(contents, parse: true) ⇒ Object



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

def (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) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/graphql-docs/helpers.rb', line 88

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

#yaml_split(contents) ⇒ Object



92
93
94
# File 'lib/graphql-docs/helpers.rb', line 92

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