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_directive_types, #graphql_enum_types, #graphql_input_object_types, #graphql_interface_types, #graphql_mutation_types, #graphql_object_types, #graphql_operation_types, #graphql_root_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

Returns a new instance of Parser.



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

def initialize(schema, options)
  @options = options

  @options[:notices] ||= -> (schema_member_path) { [] }

  if schema.is_a?(GraphQL::Schema)
    @schema = schema
  else
    @schema = GraphQL::Schema.from_definition(schema)
  end

  @processed_schema = {
    operation_types: [],
    mutation_types: [],
    object_types: [],
    interface_types: [],
    enum_types: [],
    union_types: [],
    input_object_types: [],
    scalar_types: [],
    directive_types: [],
  }
end

Instance Attribute Details

#processed_schemaObject (readonly)

Returns the value of attribute processed_schema.



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

def processed_schema
  @processed_schema
end

Instance Method Details

#parseObject



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/graphql-docs/parser.rb', line 34

def parse

  root_types = {}
  ['query', 'mutation'].each do |operation|
    unless @schema.root_type_for_operation(operation).nil?
      root_types[operation] = @schema.root_type_for_operation(operation).name
    end
  end
  @processed_schema[:root_types] = root_types

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

    data[:notices] = @options[:notices].call(object.name)

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

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

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

        @processed_schema[:operation_types] << data

        object.fields.each_value do |mutation|
          h = {}

          h[:notices] = @options[:notices].call([object.name, mutation.name].join('.'))
          h[:name] = mutation.name
          h[:description] = mutation.description
          h[:input_fields], _ = fetch_fields(mutation.arguments, [object.name, mutation.name].join('.'))

          return_type = mutation.type
          if return_type.unwrap.respond_to?(:fields)
            h[:return_fields], _ = fetch_fields(return_type.unwrap.fields, return_type.name)
          else # it is a scalar return type
            h[:return_fields], _ = fetch_fields({ "#{return_type.name}" => mutation }, return_type.name)
          end

          @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, object.name)

        @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, object.name)

      @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[:notices] = @options[:notices].call([object.name, val.name].join('.'))
        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, object.name)

      @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

  @schema.directives.each_value do |directive|
    data = {}
    data[:notices] = @options[:notices].call(directive.name)

    data[:name] = directive.name
    data[:description] = directive.description
    data[:locations] = directive.locations

    data[:arguments], _ = fetch_fields(directive.arguments, directive.name)

    @processed_schema[:directive_types] << data
  end

  sort_by_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