Class: GraphQLDocs::Parser

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

Overview

Parses GraphQL schemas into a structured format for documentation generation.

The Parser takes a GraphQL schema (as a string or schema class) and transforms it into a normalized hash structure that's easier to work with during HTML generation. It extracts all types, fields, arguments, and metadata from the schema.

Examples:

Basic usage

parser = GraphQLDocs::Parser.new(schema_string, options)
parsed = parser.parse

Accessing parsed data

parsed[:object_types] # => Array of object type hashes
parsed[:query_types]  # => Array of query hashes

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_query_types, #graphql_root_types, #graphql_scalar_types, #graphql_union_types, #include, #markdownify, #slugify, #split_into_metadata_and_contents, #yaml?, #yaml_split

Constructor Details

#initialize(schema, options) ⇒ Parser

Initializes a new Parser instance.

Options Hash (options):

  • :notices (Proc)

    Proc for adding custom notices to schema members



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/graphql-docs/parser.rb', line 31

def initialize(schema, options)
  @options = options

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

  @schema = if schema.is_a?(String)
              GraphQL::Schema.from_definition(schema)
            elsif schema < GraphQL::Schema
              schema
            end

  @processed_schema = {
    operation_types: [],
    query_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.



24
25
26
# File 'lib/graphql-docs/parser.rb', line 24

def processed_schema
  @processed_schema
end

Instance Method Details

#parseHash

Parses the GraphQL schema into a structured hash.

This method processes the entire schema and extracts all types, including:

  • Operation types (Query, Mutation)
  • Object types
  • Interface types
  • Enum types
  • Union types
  • Input object types
  • Scalar types
  • Directive types

Each type includes its fields, arguments, deprecation notices, and custom notices.

Raises:

  • (TypeError)

    If an unknown GraphQL type is encountered



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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/graphql-docs/parser.rb', line 84

def parse
  root_types = {}
  %w[query mutation].each do |operation|
    root_types[operation] = @schema.root_type_for_operation(operation).graphql_name unless @schema.root_type_for_operation(operation).nil?
  end
  @processed_schema[:root_types] = root_types

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

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

    if object < ::GraphQL::Schema::Object
      data[:name] = object.graphql_name
      data[:description] = object.description

      if data[:name] == root_types['query']
        data[:interfaces] = object.interfaces.map(&:graphql_name).sort
        data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)
        @processed_schema[:operation_types] << data

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

          h[:notices] = @options[:notices].call([object.graphql_name, query.graphql_name].join('.'))
          h[:name] = query.graphql_name
          h[:description] = query.description
          if query.respond_to?(:deprecation_reason) && !query.deprecation_reason.nil?
            h[:is_deprecated] = true
            h[:deprecation_reason] = query.deprecation_reason
          end
          h[:arguments], = fetch_fields(query.arguments, [object.graphql_name, query.graphql_name].join('.'))

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

          @processed_schema[:query_types] << h
        end
      elsif data[:name] == root_types['mutation']
        @processed_schema[:operation_types] << data

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

          h[:notices] = @options[:notices].call([object.graphql_name, mutation.graphql_name].join('.'))
          h[:name] = mutation.graphql_name
          h[:description] = mutation.description
          if mutation.respond_to?(:deprecation_reason) && !mutation.deprecation_reason.nil?
            h[:is_deprecated] = true
            h[:deprecation_reason] = mutation.deprecation_reason
          end
          h[:input_fields], = fetch_fields(mutation.arguments, [object.graphql_name, mutation.graphql_name].join('.'))

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

          @processed_schema[:mutation_types] << h
        end
      else
        data[:interfaces] = object.interfaces.map(&:graphql_name).sort
        data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)

        @processed_schema[:object_types] << data
      end
    elsif object < ::GraphQL::Schema::Interface
      data[:name] = object.graphql_name
      data[:description] = object.description
      data[:fields], data[:connections] = fetch_fields(object.fields, object.graphql_name)

      @processed_schema[:interface_types] << data
    elsif object < ::GraphQL::Schema::Enum
      data[:name] = object.graphql_name
      data[:description] = object.description

      data[:values] = object.values.values.map do |val|
        h = {}
        h[:notices] = @options[:notices].call([object.graphql_name, val.graphql_name].join('.'))
        h[:name] = val.graphql_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
    elsif object < ::GraphQL::Schema::Union
      data[:name] = object.graphql_name
      data[:description] = object.description
      data[:possible_types] = object.possible_types.map(&:graphql_name).sort

      @processed_schema[:union_types] << data
    elsif object < GraphQL::Schema::InputObject
      data[:name] = object.graphql_name
      data[:description] = object.description

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

      @processed_schema[:input_object_types] << data
    elsif object < GraphQL::Schema::Scalar
      data[:name] = object.graphql_name
      data[:description] = object.description

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

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

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

    data[:arguments], = fetch_fields(directive.arguments, directive.graphql_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|
      interface[:implemented_by] << obj[:name] if obj[:interfaces].include?(interface[:name])
    end
  end

  @processed_schema
end