Class: GraphQL::Language::DocumentFromSchemaDefinition Private

Inherits:
Object
  • Object
show all
Defined in:
lib/graphql/language/document_from_schema_definition.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

DocumentFromSchemaDefinition is used to convert a Schema object To a Document AST node.

Instance Method Summary collapse

Constructor Details

#initialize(schema, context: nil, include_introspection_types: false, include_built_in_directives: false, include_built_in_scalars: false, always_include_schema: false) ⇒ DocumentFromSchemaDefinition

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of DocumentFromSchemaDefinition.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/graphql/language/document_from_schema_definition.rb', line 16

def initialize(
  schema, context: nil, include_introspection_types: false,
  include_built_in_directives: false, include_built_in_scalars: false, always_include_schema: false
)
  @schema = schema
  @always_include_schema = always_include_schema
  @include_introspection_types = include_introspection_types
  @include_built_in_scalars = include_built_in_scalars
  @include_built_in_directives = include_built_in_directives
  @include_one_of = false

  schema_context = schema.context_class.new(query: nil, object: nil, schema: schema, values: context)


  @warden = @schema.warden_class.new(
    schema: @schema,
    context: schema_context,
  )

  schema_context.warden = @warden
end

Instance Method Details

#build_argument_node(argument) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/graphql/language/document_from_schema_definition.rb', line 131

def build_argument_node(argument)
  if argument.default_value?
    default_value = build_default_value(argument.default_value, argument.type)
  else
    default_value = nil
  end

  argument_node = GraphQL::Language::Nodes::InputValueDefinition.new(
    name: argument.graphql_name,
    description: argument.description,
    type: build_type_name_node(argument.type),
    default_value: default_value,
    directives: directives(argument),
  )

  argument_node
end

#build_argument_nodes(arguments) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



246
247
248
249
250
251
252
253
254
# File 'lib/graphql/language/document_from_schema_definition.rb', line 246

def build_argument_nodes(arguments)
  if arguments.any?
    nodes = arguments.map { |arg| build_argument_node(arg) }
    nodes.sort_by!(&:name)
    nodes
  else
    arguments
  end
end

#build_default_value(default_value, type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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/language/document_from_schema_definition.rb', line 194

def build_default_value(default_value, type)
  if default_value.nil?
    return GraphQL::Language::Nodes::NullValue.new(name: "null")
  end

  case type.kind.name
  when "SCALAR"
    type.coerce_isolated_result(default_value)
  when "ENUM"
    GraphQL::Language::Nodes::Enum.new(name: type.coerce_isolated_result(default_value))
  when "INPUT_OBJECT"
    GraphQL::Language::Nodes::InputObject.new(
      arguments: default_value.to_h.map do |arg_name, arg_value|
        args = @warden.arguments(type)
        arg = args.find { |a| a.keyword.to_s == arg_name.to_s }
        if arg.nil?
          raise ArgumentError, "No argument definition on #{type.graphql_name} for argument: #{arg_name.inspect} (expected one of: #{args.map(&:keyword)})"
        end
        GraphQL::Language::Nodes::Argument.new(
          name: arg.graphql_name.to_s,
          value: build_default_value(arg_value, arg.type)
        )
      end
    )
  when "NON_NULL"
    build_default_value(default_value, type.of_type)
  when "LIST"
    default_value.to_a.map { |v| build_default_value(v, type.of_type) }
  else
    raise GraphQL::RequiredImplementationMissingError, "Unexpected default value type #{type.inspect}"
  end
end

#build_definition_nodesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/graphql/language/document_from_schema_definition.rb', line 262

def build_definition_nodes
  dirs_to_build = warden.directives
  if !include_built_in_directives
    dirs_to_build = dirs_to_build.reject { |directive| directive.default_directive? }
  end
  definitions = build_directive_nodes(dirs_to_build)

  type_nodes = build_type_definition_nodes(warden.reachable_types + schema.extra_types)
  if @include_one_of
    # This may have been set to true when iterating over all types
    definitions.concat(build_directive_nodes([GraphQL::Schema::Directive::OneOf]))
  end

  definitions.concat(type_nodes)
  if include_schema_node?
    definitions.unshift(build_schema_node)
  end

  definitions
end

#build_directive_location_node(location) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



172
173
174
175
176
# File 'lib/graphql/language/document_from_schema_definition.rb', line 172

def build_directive_location_node(location)
  GraphQL::Language::Nodes::DirectiveLocation.new(
    name: location.to_s
  )
end

#build_directive_location_nodes(locations) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



168
169
170
# File 'lib/graphql/language/document_from_schema_definition.rb', line 168

def build_directive_location_nodes(locations)
  locations.sort.map { |location| build_directive_location_node(location) }
end

#build_directive_node(directive) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



158
159
160
161
162
163
164
165
166
# File 'lib/graphql/language/document_from_schema_definition.rb', line 158

def build_directive_node(directive)
  GraphQL::Language::Nodes::DirectiveDefinition.new(
    name: directive.graphql_name,
    repeatable: directive.repeatable?,
    arguments: build_argument_nodes(warden.arguments(directive)),
    locations: build_directive_location_nodes(directive.locations),
    description: directive.description,
  )
end

#build_directive_nodes(directives) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



256
257
258
259
260
# File 'lib/graphql/language/document_from_schema_definition.rb', line 256

def build_directive_nodes(directives)
  directives
    .map { |directive| build_directive_node(directive) }
    .sort_by(&:name)
end

#build_enum_type_node(enum_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



104
105
106
107
108
109
110
111
112
113
# File 'lib/graphql/language/document_from_schema_definition.rb', line 104

def build_enum_type_node(enum_type)
  GraphQL::Language::Nodes::EnumTypeDefinition.new(
    name: enum_type.graphql_name,
    values: warden.enum_values(enum_type).sort_by(&:graphql_name).map do |enum_value|
      build_enum_value_node(enum_value)
    end,
    description: enum_type.description,
    directives: directives(enum_type),
  )
end

#build_enum_value_node(enum_value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



115
116
117
118
119
120
121
# File 'lib/graphql/language/document_from_schema_definition.rb', line 115

def build_enum_value_node(enum_value)
  GraphQL::Language::Nodes::EnumValueDefinition.new(
    name: enum_value.graphql_name,
    description: enum_value.description,
    directives: directives(enum_value),
  )
end

#build_field_node(field) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



75
76
77
78
79
80
81
82
83
# File 'lib/graphql/language/document_from_schema_definition.rb', line 75

def build_field_node(field)
  GraphQL::Language::Nodes::FieldDefinition.new(
    name: field.graphql_name,
    arguments: build_argument_nodes(warden.arguments(field)),
    type: build_type_name_node(field.type),
    description: field.description,
    directives: directives(field),
  )
end

#build_field_nodes(fields) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



297
298
299
300
301
# File 'lib/graphql/language/document_from_schema_definition.rb', line 297

def build_field_nodes(fields)
  f_nodes = fields.map { |field| build_field_node(field) }
  f_nodes.sort_by!(&:name)
  f_nodes
end

#build_input_object_node(input_object) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



149
150
151
152
153
154
155
156
# File 'lib/graphql/language/document_from_schema_definition.rb', line 149

def build_input_object_node(input_object)
  GraphQL::Language::Nodes::InputObjectTypeDefinition.new(
    name: input_object.graphql_name,
    fields: build_argument_nodes(warden.arguments(input_object)),
    description: input_object.description,
    directives: directives(input_object),
  )
end

#build_interface_type_node(interface_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



94
95
96
97
98
99
100
101
102
# File 'lib/graphql/language/document_from_schema_definition.rb', line 94

def build_interface_type_node(interface_type)
  GraphQL::Language::Nodes::InterfaceTypeDefinition.new(
    name: interface_type.graphql_name,
    interfaces: warden.interfaces(interface_type).sort_by(&:graphql_name).map { |type| build_type_name_node(type) },
    description: interface_type.description,
    fields: build_field_nodes(warden.fields(interface_type)),
    directives: directives(interface_type),
  )
end

#build_object_type_node(object_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/graphql/language/document_from_schema_definition.rb', line 59

def build_object_type_node(object_type)
  ints = warden.interfaces(object_type)
  if ints.any?
    ints.sort_by!(&:graphql_name)
    ints.map! { |iface| build_type_name_node(iface) }
  end

  GraphQL::Language::Nodes::ObjectTypeDefinition.new(
    name: object_type.graphql_name,
    interfaces: ints,
    fields: build_field_nodes(warden.fields(object_type)),
    description: object_type.description,
    directives: directives(object_type),
  )
end

#build_scalar_type_node(scalar_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



123
124
125
126
127
128
129
# File 'lib/graphql/language/document_from_schema_definition.rb', line 123

def build_scalar_type_node(scalar_type)
  GraphQL::Language::Nodes::ScalarTypeDefinition.new(
    name: scalar_type.graphql_name,
    description: scalar_type.description,
    directives: directives(scalar_type),
  )
end

#build_schema_nodeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/graphql/language/document_from_schema_definition.rb', line 44

def build_schema_node
  if !schema_respects_root_name_conventions?(@schema)
    GraphQL::Language::Nodes::SchemaDefinition.new(
      query: (q = warden.root_type_for_operation("query")) && q.graphql_name,
      mutation: (m = warden.root_type_for_operation("mutation")) && m.graphql_name,
      subscription: (s = warden.root_type_for_operation("subscription")) && s.graphql_name,
      directives: definition_directives(@schema, :schema_directives)
    )
  else
    # A plain `schema ...` _must_ include root type definitions.
    # If the only difference is directives, then you have to use `extend schema`
    GraphQL::Language::Nodes::SchemaExtension.new(directives: definition_directives(@schema, :schema_directives))
  end
end

#build_type_definition_node(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/graphql/language/document_from_schema_definition.rb', line 227

def build_type_definition_node(type)
  case type.kind.name
  when "OBJECT"
    build_object_type_node(type)
  when "UNION"
    build_union_type_node(type)
  when "INTERFACE"
    build_interface_type_node(type)
  when "SCALAR"
    build_scalar_type_node(type)
  when "ENUM"
    build_enum_type_node(type)
  when "INPUT_OBJECT"
    build_input_object_node(type)
  else
    raise TypeError
  end
end

#build_type_definition_nodes(types) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/graphql/language/document_from_schema_definition.rb', line 283

def build_type_definition_nodes(types)
  if !include_introspection_types
    types = types.reject { |type| type.introspection? }
  end

  if !include_built_in_scalars
    types = types.reject { |type| type.kind.scalar? && type.default_scalar? }
  end

  types
    .map { |type| build_type_definition_node(type) }
    .sort_by(&:name)
end

#build_type_name_node(type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/graphql/language/document_from_schema_definition.rb', line 178

def build_type_name_node(type)
  case type.kind.name
  when "LIST"
    GraphQL::Language::Nodes::ListType.new(
      of_type: build_type_name_node(type.of_type)
    )
  when "NON_NULL"
    GraphQL::Language::Nodes::NonNullType.new(
      of_type: build_type_name_node(type.of_type)
    )
  else
    @cached_type_name_nodes ||= {}
    @cached_type_name_nodes[type.graphql_name] ||= GraphQL::Language::Nodes::TypeName.new(name: type.graphql_name)
  end
end

#build_union_type_node(union_type) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
91
92
# File 'lib/graphql/language/document_from_schema_definition.rb', line 85

def build_union_type_node(union_type)
  GraphQL::Language::Nodes::UnionTypeDefinition.new(
    name: union_type.graphql_name,
    description: union_type.description,
    types: warden.possible_types(union_type).sort_by(&:graphql_name).map { |type| build_type_name_node(type) },
    directives: directives(union_type),
  )
end

#documentObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



38
39
40
41
42
# File 'lib/graphql/language/document_from_schema_definition.rb', line 38

def document
  GraphQL::Language::Nodes::Document.new(
    definitions: build_definition_nodes
  )
end