Class: GraphQLDocs::Parser

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

Constant Summary

Constants included from Helpers

Helpers::SLUGIFY_PRETTY_REGEXP

Instance Attribute Summary collapse

Attributes included from Helpers

#templates

Instance Method Summary collapse

Methods included from Helpers

#graphql_enum_types, #graphql_input_object_types, #graphql_interface_types, #graphql_mutation_types, #graphql_object_types, #graphql_operation_types, #graphql_scalar_types, #graphql_union_types, #has_yaml?, #include, #markdownify, #slugify, #split_into_metadata_and_contents, #yaml_split

Constructor Details

#initialize(schema, options) ⇒ Parser



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/graphql-docs/parser.rb', line 9

def initialize(schema, options)
  @options = options
  @schema = GraphQL::Schema.from_definition(schema)
  @processed_schema = {
    operation_types: [],
    mutation_types: [],
    object_types: [],
    interface_types: [],
    enum_types: [],
    union_types: [],
    input_object_types: [],
    scalar_types: [],
  }
end

Instance Attribute Details

#processed_schemaObject (readonly)

Returns the value of attribute processed_schema



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

def processed_schema
  @processed_schema
end

Instance Method Details

#parseObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/graphql-docs/parser.rb', line 24

def parse
  @schema.types.each_value do |object|
    data = {}

    case object
    when ::GraphQL::ObjectType
      if object.name == 'Query'
        data[:name] = object.name
        data[:description] = object.description

        data[:interfaces] = object.interfaces.map(&:name).sort
        data[:fields], data[:connections] = fetch_fields(object.fields)

        @processed_schema[:operation_types] << data
      elsif object.name == 'Mutation'
          data[:name] = object.name
          data[:description] = object.description

          @processed_schema[:operation_types] << data

          object.fields.each_value do |mutation|
            h = {}
            h[:name] = mutation.name
            h[:description] = mutation.description
            h[:input_fields], _ = fetch_fields(mutation.arguments.values.first.type.unwrap.input_fields)
            h[:return_fields], _ = fetch_fields(mutation.type.unwrap.fields)

            @processed_schema[:mutation_types] << h
          end
      else
        data[:name] = object.name
        data[:description] = object.description

        data[:interfaces] = object.interfaces.map(&:name).sort
        data[:fields], data[:connections] = fetch_fields(object.fields)

        @processed_schema[:object_types] << data
      end
    when ::GraphQL::InterfaceType
      data[:name] = object.name
      data[:description] = object.description
      data[:fields], data[:connections] = fetch_fields(object.fields)

      @processed_schema[:interface_types] << data
    when ::GraphQL::EnumType
      data[:name] = object.name
      data[:description] = object.description

      data[:values] = object.values.values.map do |val|
        h = {}
        h[:name] = val.name
        h[:description] = val.description
        unless val.deprecation_reason.nil?
          h[:is_deprecated] = true
          h[:deprecation_reason] = val.deprecation_reason
        end
        h
      end

      @processed_schema[:enum_types] << data
    when ::GraphQL::UnionType
      data[:name] = object.name
      data[:description] = object.description
      data[:possible_types] = object.possible_types.map(&:name).sort

      @processed_schema[:union_types] << data
    when ::GraphQL::InputObjectType
      data[:name] = object.name
      data[:description] = object.description

      data[:input_fields], _ = fetch_fields(object.input_fields)

      @processed_schema[:input_object_types] << data
    when ::GraphQL::ScalarType
      data[:name] = object.name
      data[:description] = object.description

      @processed_schema[:scalar_types] << data
    else
      raise TypeError, "I'm not sure what #{object.class} is!"
    end
  end

  @processed_schema[:mutation_types].sort_by! { |o| o[:name] }
  @processed_schema[:object_types].sort_by! { |o| o[:name] }
  @processed_schema[:interface_types].sort_by! { |o| o[:name] }
  @processed_schema[:enum_types].sort_by! { |o| o[:name] }
  @processed_schema[:union_types].sort_by! { |o| o[:name] }
  @processed_schema[:input_object_types].sort_by! { |o| o[:name] }
  @processed_schema[:scalar_types].sort_by! { |o| o[:name] }

  @processed_schema[:interface_types].each do |interface|
    interface[:implemented_by] = []
    @processed_schema[:object_types].each do |obj|
      if obj[:interfaces].include?(interface[:name])
        interface[:implemented_by] << obj[:name]
      end
    end
  end

  @processed_schema
end