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

Constructor Details

#initialize(schema, options) ⇒ Parser

Returns a new instance of Parser.



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

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: [],
    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.



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

def processed_schema
  @processed_schema
end

Instance Method Details

#parseObject



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
# File 'lib/graphql-docs/parser.rb', line 35

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
      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
          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