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, only: nil, except: 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
# File 'lib/graphql/language/document_from_schema_definition.rb', line 16

def initialize(
  schema, context: nil, only: nil, except: 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

  @warden = GraphQL::Schema::Warden.new(
    GraphQL::Filter.new(only: only, except: except),
    schema: @schema,
    context: context,
  )
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.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/graphql/language/document_from_schema_definition.rb', line 126

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.name,
    description: argument.description,
    type: build_type_name_node(argument.type),
    default_value: default_value,
  )

  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.



233
234
235
236
237
# File 'lib/graphql/language/document_from_schema_definition.rb', line 233

def build_argument_nodes(arguments)
  arguments
    .map { |arg| build_argument_node(arg) }
    .sort_by(&:name)
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.



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
# File 'lib/graphql/language/document_from_schema_definition.rb', line 185

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

  case type
  when GraphQL::ScalarType
    type.coerce_isolated_result(default_value)
  when EnumType
    GraphQL::Language::Nodes::Enum.new(name: type.coerce_isolated_result(default_value))
  when InputObjectType
    GraphQL::Language::Nodes::InputObject.new(
      arguments: default_value.to_h.map do |arg_name, arg_value|
        arg_type = type.input_fields.fetch(arg_name.to_s).type
        GraphQL::Language::Nodes::Argument.new(
          name: arg_name,
          value: build_default_value(arg_value, arg_type)
        )
      end
    )
  when NonNullType
    build_default_value(default_value, type.of_type)
  when ListType
    default_value.to_a.map { |v| build_default_value(v, type.of_type) }
  else
    raise NotImplementedError, "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.



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

def build_definition_nodes
  definitions = []
  definitions << build_schema_node if include_schema_node?
  definitions += build_directive_nodes(warden.directives)
  definitions += build_type_definition_nodes(warden.types)
  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.



164
165
166
167
168
# File 'lib/graphql/language/document_from_schema_definition.rb', line 164

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.



160
161
162
# File 'lib/graphql/language/document_from_schema_definition.rb', line 160

def build_directive_location_nodes(locations)
  locations.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.



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

def build_directive_node(directive)
  GraphQL::Language::Nodes::DirectiveDefinition.new(
    name: directive.name,
    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.



239
240
241
242
243
244
245
246
247
# File 'lib/graphql/language/document_from_schema_definition.rb', line 239

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

  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.



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

def build_enum_type_node(enum_type)
  GraphQL::Language::Nodes::EnumTypeDefinition.new(
    name: enum_type.name,
    values: warden.enum_values(enum_type).sort_by(&:name).map do |enum_value|
      build_enum_value_node(enum_value)
    end,
    description: enum_type.description,
  )
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.



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

def build_enum_value_node(enum_value)
  enum_value_node = GraphQL::Language::Nodes::EnumValueDefinition.new(
    name: enum_value.name,
    description: enum_value.description,
  )

  if enum_value.deprecation_reason
    enum_value_node = enum_value_node.merge_directive(
      name: GraphQL::Directive::DeprecatedDirective.name,
      arguments: [GraphQL::Language::Nodes::Argument.new(name: "reason", value: enum_value.deprecation_reason)]
    )
  end

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



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

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

  if field.deprecation_reason
    field_node = field_node.merge_directive(
      name: GraphQL::Directive::DeprecatedDirective.name,
      arguments: [GraphQL::Language::Nodes::Argument.new(name: "reason", value: field.deprecation_reason)]
    )
  end

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



271
272
273
274
275
# File 'lib/graphql/language/document_from_schema_definition.rb', line 271

def build_field_nodes(fields)
  fields
    .map { |field| build_field_node(field) }
    .sort_by(&:name)
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.



143
144
145
146
147
148
149
# File 'lib/graphql/language/document_from_schema_definition.rb', line 143

def build_input_object_node(input_object)
  GraphQL::Language::Nodes::InputObjectTypeDefinition.new(
    name: input_object.name,
    fields: build_argument_nodes(warden.arguments(input_object)),
    description: input_object.description,
  )
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.



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

def build_interface_type_node(interface_type)
  GraphQL::Language::Nodes::InterfaceTypeDefinition.new(
    name: interface_type.name,
    description: interface_type.description,
    fields: build_field_nodes(warden.fields(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.



50
51
52
53
54
55
56
57
# File 'lib/graphql/language/document_from_schema_definition.rb', line 50

def build_object_type_node(object_type)
  GraphQL::Language::Nodes::ObjectTypeDefinition.new(
    name: object_type.name,
    interfaces: warden.interfaces(object_type).sort_by(&:name).map { |iface| build_type_name_node(iface) },
    fields: build_field_nodes(warden.fields(object_type)),
    description: object_type.description,
  )
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.



119
120
121
122
123
124
# File 'lib/graphql/language/document_from_schema_definition.rb', line 119

def build_scalar_type_node(scalar_type)
  GraphQL::Language::Nodes::ScalarTypeDefinition.new(
    name: scalar_type.name,
    description: scalar_type.description,
  )
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.



39
40
41
42
43
44
45
46
47
48
# File 'lib/graphql/language/document_from_schema_definition.rb', line 39

def build_schema_node
  GraphQL::Language::Nodes::SchemaDefinition.new(
    query: warden.root_type_for_operation("query"),
    mutation: warden.root_type_for_operation("mutation"),
    subscription: warden.root_type_for_operation("subscription"),
    # This only supports directives from parsing,
    # use a custom printer to add to this list.
    directives: @schema.ast_node ? @schema.ast_node.directives : [],
  )
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.



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/graphql/language/document_from_schema_definition.rb', line 214

def build_type_definition_node(type)
  case type
  when GraphQL::ObjectType
    build_object_type_node(type)
  when GraphQL::UnionType
    build_union_type_node(type)
  when GraphQL::InterfaceType
    build_interface_type_node(type)
  when GraphQL::ScalarType
    build_scalar_type_node(type)
  when GraphQL::EnumType
    build_enum_type_node(type)
  when GraphQL::InputObjectType
    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.



257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/graphql/language/document_from_schema_definition.rb', line 257

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



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/graphql/language/document_from_schema_definition.rb', line 170

def build_type_name_node(type)
  case type
  when GraphQL::ListType
    GraphQL::Language::Nodes::ListType.new(
      of_type: build_type_name_node(type.of_type)
    )
  when GraphQL::NonNullType
    GraphQL::Language::Nodes::NonNullType.new(
      of_type: build_type_name_node(type.of_type)
    )
  else
    GraphQL::Language::Nodes::TypeName.new(name: type.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.



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

def build_union_type_node(union_type)
  GraphQL::Language::Nodes::UnionTypeDefinition.new(
    name: union_type.name,
    description: union_type.description,
    types: warden.possible_types(union_type).sort_by(&:name).map { |type| build_type_name_node(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.



33
34
35
36
37
# File 'lib/graphql/language/document_from_schema_definition.rb', line 33

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