Class: GraphQLDocs::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql-docs/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response, options) ⇒ Parser

Returns a new instance of Parser.



5
6
7
8
9
# File 'lib/graphql-docs/parser.rb', line 5

def initialize(response, options)
  @options = options
  @schema = JSON.parse(response)['data']
  @processed_schema = @schema.dup['__schema']
end

Instance Attribute Details

#processed_schemaObject (readonly)

Returns the value of attribute processed_schema.



3
4
5
# File 'lib/graphql-docs/parser.rb', line 3

def processed_schema
  @processed_schema
end

#schemaObject (readonly)

Returns the value of attribute schema.



3
4
5
# File 'lib/graphql-docs/parser.rb', line 3

def schema
  @schema
end

Instance Method Details

#parseObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/graphql-docs/parser.rb', line 11

def parse
  # sort the types
  @processed_schema['types'] = @processed_schema['types'].sort_by { |key, _| key['name'] }

  # fetch the connections
  @processed_schema['types'].each do |object|
    next if object['fields'].nil?
    object['connections'] = object['fields'].select { |f| next if f.is_a?(Array); connection?(f) }
  end

  # fetch the kinds of items
  type_kinds = @processed_schema['types'].map { |h| h['kind'] }.uniq
  type_kinds.each do |kind|
    @processed_schema["#{kind.downcase}_types"] = @processed_schema['types'].select { |t| t['kind'] == kind }
  end
  mutation_types = @processed_schema['object_types'].select do |t|
    t['name'] == 'Mutation'
  end

  @processed_schema['mutation_types'] = if mutation_types.empty?
                                          []
                                        else
                                          @processed_schema['mutation_types'] = mutation_types.first['fields']
                                        end

  # TODO: should the 'types' key be deleted now?

  @processed_schema
end