class Graphqlmd::Graphqlmd

Public Class Methods

new(options) click to toggle source
# File lib/graphqlmd.rb, line 10
def initialize options
  @url = options[:url]
  @title = options[:title]
  @note = options[:note]
  @is_hide_client_mutation_id = options[:is_hide_client_mutation_id]
  @is_hide_table_of_contents = options[:is_hide_table_of_contents]
  @is_hide_deprecated = options[:is_hide_deprecated]
  @is_hide_scalar = options[:is_hide_scalar]
  @is_allow_links = options[:is_allow_links]
  @is_vuepress = options[:is_vuepress]
  @ignored_queries = options[:ignored_queries]
  @ignored_mutations = options[:ignored_mutations]
  @ignored_objects = options[:ignored_objects]
end

Public Instance Methods

call() click to toggle source
# File lib/graphqlmd.rb, line 25
def call
  response = fetch_schema
  data = JSON.parse(response.body).dig 'data', 'schema'

  puts_title
  puts_note
  puts_top_table_of_contents
  puts_queries data['queries']
  puts_mutations data['mutations']
  puts_objects data['types']
end

Private Instance Methods

body() click to toggle source
# File lib/graphqlmd.rb, line 231
def body
  { query: SCHEMA_QUERY, variables: nil }.to_json
end
fetch_schema() click to toggle source
# File lib/graphqlmd.rb, line 215
def fetch_schema
  uri = URI.parse @url

  Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
    request = Net::HTTP::Post.new uri.request_uri, headers
    request.body = body
    http.request request
  end
end
headers() click to toggle source
# File lib/graphqlmd.rb, line 225
def headers
  {
    'Content-Type' => 'application/json'
  }
end
print_args(args) click to toggle source
print_deprecation(data) click to toggle source
print_description(value) click to toggle source
print_enum_values(values) click to toggle source

TODO: Print deprecationReason for Enum Values

print_fields(fields) click to toggle source
print_header(value) click to toggle source
puts_mutations(data) click to toggle source
# File lib/graphqlmd.rb, line 80
def puts_mutations data
  print_header 'Mutations'
  mutations = data['fields']
    .reject { |v| @ignored_mutations.include? v['name'] }
    .sort_by { |v| v['name'] }
  mutations = mutations.reject { |v| v['isDeprecated'] } if @is_hide_deprecated

  puts_table_of_contents mutations

  mutations.each do |v|
    puts "\n### #{v['name']}\n"
    print_deprecation v
    puts "Return Type | Description\n"
    puts "-|-\n"
    puts "#{type_as_string v['type']} | #{print_description(v['description'])}\n"
    print_args v['args']
  end
end
puts_note() click to toggle source
# File lib/graphqlmd.rb, line 46
def puts_note
  return if @note.nil?
  return if @note.empty?

  puts "#{@note}\n\n"
end
puts_objects(data) click to toggle source
# File lib/graphqlmd.rb, line 99
def puts_objects data
  print_header 'Objects'
  objects = data
    .reject { |v| IGNORED_OBJECT_NAMES.include? v['name'] }
    .reject { |v| @ignored_objects.include? v['name'] }
    .sort_by { |v| v['name'] }
  objects = objects.reject { |v| v['kind'] == SCALAR } if @is_hide_scalar

  puts_table_of_contents objects

  objects.each do |v|
    puts "\n### #{v['name']}\n"
    puts "#{print_description v['description']}\n" unless v['description'].nil?
    print_fields v['fields'] || v['inputFields']
    print_enum_values v['enumValues']
  end

  nil
end
puts_queries(data) click to toggle source
# File lib/graphqlmd.rb, line 62
def puts_queries data
  print_header 'Queries'
  queries = data['fields']
    .reject { |v| @ignored_queries.include? v['name'] }
    .sort_by { |v| v['name'] }

  puts_table_of_contents queries

  queries.each do |v|
    puts "\n### #{v['name']}\n"
    print_deprecation v
    puts "Return Type | Description\n"
    puts "-|-\n"
    puts "#{type_as_string v['type']} | #{print_description(v['description'])}\n"
    print_args v['args']
  end
end
puts_table_of_contents(items) click to toggle source
# File lib/graphqlmd.rb, line 123
def puts_table_of_contents items
  return if @is_hide_table_of_contents

  items.each do |v|
    puts "- [#{v['name']}](##{v['name'].downcase})"
  end
end
puts_title() click to toggle source
# File lib/graphqlmd.rb, line 39
def puts_title
  return if @title.nil?
  return if @title.empty?

  puts "# #{@title}\n\n"
end
puts_top_table_of_contents() click to toggle source
# File lib/graphqlmd.rb, line 53
def puts_top_table_of_contents
  return if @is_hide_table_of_contents

  puts '- [Queries](#queries)'
  puts '- [Mutations](#mutations)'
  puts '- [Objects](#objects)'
  puts "\n"
end
required_postfix(is_required) click to toggle source
# File lib/graphqlmd.rb, line 204
def required_postfix is_required
  is_required ? '!' : ''
end
type_as_string(type) click to toggle source
# File lib/graphqlmd.rb, line 191
def type_as_string type
  is_required = type['kind'] == NON_NULL
  is_list = type['kind'] == LIST

  if type['name']
    "#{name_as_link(type)}#{required_postfix(is_required)}"
  elsif is_list
    "[#{type_as_string(type['ofType'])}#{required_postfix(is_required)}]"
  else
    "#{type_as_string(type['ofType'])}#{required_postfix(is_required)}"
  end
end