graphql 2.1.7 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -16,30 +16,30 @@ module GraphQL
16
16
  # @return [Integer] The first line of the definition (not the description)
17
17
  attr_reader :definition_line
18
18
 
19
- def initialize(options = {})
20
- @definition_line = options.delete(:definition_line)
21
- super(options)
19
+ def initialize(definition_line: nil, **_rest)
20
+ @definition_line = definition_line
21
+ super(**_rest)
22
22
  end
23
23
  end
24
24
 
25
- attr_reader :line, :col, :filename
25
+ attr_reader :filename
26
26
 
27
- # Initialize a node by extracting its position,
28
- # then calling the class's `initialize_node` method.
29
- # @param options [Hash] Initial attributes for this node
30
- def initialize(options = {})
31
- if options.key?(:position_source)
32
- position_source = options.delete(:position_source)
33
- @line = position_source[1]
34
- @col = position_source[2]
35
- else
36
- @line = options.delete(:line)
37
- @col = options.delete(:col)
38
- end
27
+ def line
28
+ @line ||= (@source_string && @pos) ? @source_string[0..@pos].count("\n") + 1 : nil
29
+ end
39
30
 
40
- @filename = options.delete(:filename)
31
+ def col
32
+ @col ||= if @source_string && @pos
33
+ if @pos == 0
34
+ 1
35
+ else
36
+ @source_string[0..@pos].split("\n").last.length
37
+ end
38
+ end
39
+ end
41
40
 
42
- initialize_node(**options)
41
+ def definition_line
42
+ @definition_line ||= (@source_string && @definition_pos) ? @source_string[0..@definition_pos].count("\n") + 1 : nil
43
43
  end
44
44
 
45
45
  # Value equality
@@ -196,8 +196,8 @@ module GraphQL
196
196
  module_eval <<-RUBY, __FILE__, __LINE__
197
197
  # Singular method: create a node with these options
198
198
  # and return a new `self` which includes that node in this list.
199
- def merge_#{method_name.to_s.sub(/s$/, "")}(node_opts)
200
- merge(#{method_name}: #{method_name} + [#{node_type.name}.new(node_opts)])
199
+ def merge_#{method_name.to_s.sub(/s$/, "")}(**node_opts)
200
+ merge(#{method_name}: #{method_name} + [#{node_type.name}.new(**node_opts)])
201
201
  end
202
202
  RUBY
203
203
  end
@@ -226,13 +226,14 @@ module GraphQL
226
226
  end
227
227
 
228
228
  if defined?(@scalar_methods)
229
- if !method_defined?(:initialize_node)
230
- generate_initialize_node
229
+ if !@initialize_was_generated
230
+ @initialize_was_generated = true
231
+ generate_initialize
231
232
  else
232
233
  # This method was defined manually
233
234
  end
234
235
  else
235
- raise "Can't generate_initialize_node because scalar_methods wasn't called; call it before children_methods"
236
+ raise "Can't generate_initialize because scalar_methods wasn't called; call it before children_methods"
236
237
  end
237
238
  end
238
239
 
@@ -261,7 +262,15 @@ module GraphQL
261
262
  end
262
263
  end
263
264
 
264
- def generate_initialize_node
265
+ DEFAULT_INITIALIZE_OPTIONS = [
266
+ "line: nil",
267
+ "col: nil",
268
+ "pos: nil",
269
+ "filename: nil",
270
+ "source_string: nil",
271
+ ]
272
+
273
+ def generate_initialize
265
274
  scalar_method_names = @scalar_methods
266
275
  # TODO: These probably should be scalar methods, but `types` returns an array
267
276
  [:types, :description].each do |extra_method|
@@ -277,16 +286,27 @@ module GraphQL
277
286
  return
278
287
  else
279
288
  arguments = scalar_method_names.map { |m| "#{m}: nil"} +
280
- @children_methods.keys.map { |m| "#{m}: NO_CHILDREN" }
289
+ @children_methods.keys.map { |m| "#{m}: NO_CHILDREN" } +
290
+ DEFAULT_INITIALIZE_OPTIONS
281
291
 
282
292
  assignments = scalar_method_names.map { |m| "@#{m} = #{m}"} +
283
293
  @children_methods.keys.map { |m| "@#{m} = #{m}.freeze" }
284
294
 
295
+ if name.end_with?("Definition") && name != "FragmentDefinition"
296
+ arguments << "definition_pos: nil"
297
+ assignments << "@definition_pos = definition_pos"
298
+ end
299
+
285
300
  keywords = scalar_method_names.map { |m| "#{m}: #{m}"} +
286
301
  @children_methods.keys.map { |m| "#{m}: #{m}" }
287
302
 
288
303
  module_eval <<-RUBY, __FILE__, __LINE__
289
- def initialize_node #{arguments.join(", ")}
304
+ def initialize(#{arguments.join(", ")})
305
+ @line = line
306
+ @col = col
307
+ @pos = pos
308
+ @filename = filename
309
+ @source_string = source_string
290
310
  #{assignments.join("\n")}
291
311
  end
292
312
 
@@ -336,7 +356,6 @@ module GraphQL
336
356
  end
337
357
 
338
358
  class DirectiveDefinition < AbstractNode
339
- include DefinitionNode
340
359
  attr_reader :description
341
360
  scalar_methods :name, :repeatable
342
361
  children_methods(
@@ -365,17 +384,22 @@ module GraphQL
365
384
  # @!attribute selections
366
385
  # @return [Array<Nodes::Field>] Selections on this object (or empty array if this is a scalar field)
367
386
 
368
- def initialize_node(attributes)
369
- @name = attributes[:name]
370
- @arguments = attributes[:arguments] || NONE
371
- @directives = attributes[:directives] || NONE
372
- @selections = attributes[:selections] || NONE
387
+ def initialize(name: nil, arguments: NONE, directives: NONE, selections: NONE, field_alias: nil, line: nil, col: nil, pos: nil, filename: nil, source_string: nil)
388
+ @name = name
389
+ @arguments = arguments || NONE
390
+ @directives = directives || NONE
391
+ @selections = selections || NONE
373
392
  # oops, alias is a keyword:
374
- @alias = attributes[:alias]
393
+ @alias = field_alias
394
+ @line = line
395
+ @col = col
396
+ @pos = pos
397
+ @filename = filename
398
+ @source_string = source_string
375
399
  end
376
400
 
377
- def self.from_a(filename, line, col, graphql_alias, name, arguments, directives, selections) # rubocop:disable Metrics/ParameterLists
378
- self.new(filename: filename, line: line, col: col, alias: graphql_alias, name: name, arguments: arguments, directives: directives, selections: selections)
401
+ def self.from_a(filename, line, col, field_alias, name, arguments, directives, selections) # rubocop:disable Metrics/ParameterLists
402
+ self.new(filename: filename, line: line, col: col, field_alias: field_alias, name: name, arguments: arguments, directives: directives, selections: selections)
379
403
  end
380
404
 
381
405
  # Override this because default is `:fields`
@@ -384,29 +408,33 @@ module GraphQL
384
408
 
385
409
  # A reusable fragment, defined at document-level.
386
410
  class FragmentDefinition < AbstractNode
411
+ scalar_methods :name, :type
412
+ children_methods({
413
+ selections: GraphQL::Language::Nodes::Field,
414
+ directives: GraphQL::Language::Nodes::Directive,
415
+ })
416
+
417
+ self.children_method_name = :definitions
387
418
  # @!attribute name
388
419
  # @return [String] the identifier for this fragment, which may be applied with `...#{name}`
389
420
 
390
421
  # @!attribute type
391
422
  # @return [String] the type condition for this fragment (name of type which it may apply to)
392
- def initialize_node(name: nil, type: nil, directives: [], selections: [])
423
+ def initialize(name: nil, type: nil, directives: NONE, selections: NONE, filename: nil, pos: nil, source_string: nil, line: nil, col: nil)
393
424
  @name = name
394
425
  @type = type
395
426
  @directives = directives
396
427
  @selections = selections
428
+ @filename = filename
429
+ @pos = pos
430
+ @source_string = source_string
431
+ @line = line
432
+ @col = col
397
433
  end
398
434
 
399
435
  def self.from_a(filename, line, col, name, type, directives, selections)
400
436
  self.new(filename: filename, line: line, col: col, name: name, type: type, directives: directives, selections: selections)
401
437
  end
402
-
403
- scalar_methods :name, :type
404
- children_methods({
405
- selections: GraphQL::Language::Nodes::Field,
406
- directives: GraphQL::Language::Nodes::Directive,
407
- })
408
-
409
- self.children_method_name = :definitions
410
438
  end
411
439
 
412
440
  # Application of a named fragment in a selection
@@ -562,7 +590,6 @@ module GraphQL
562
590
  end
563
591
 
564
592
  class SchemaDefinition < AbstractNode
565
- include DefinitionNode
566
593
  scalar_methods :query, :mutation, :subscription
567
594
  children_methods({
568
595
  directives: GraphQL::Language::Nodes::Directive,
@@ -579,7 +606,6 @@ module GraphQL
579
606
  end
580
607
 
581
608
  class ScalarTypeDefinition < AbstractNode
582
- include DefinitionNode
583
609
  attr_reader :description
584
610
  scalar_methods :name
585
611
  children_methods({
@@ -597,7 +623,6 @@ module GraphQL
597
623
  end
598
624
 
599
625
  class InputValueDefinition < AbstractNode
600
- include DefinitionNode
601
626
  attr_reader :description
602
627
  scalar_methods :name, :type, :default_value
603
628
  children_methods({
@@ -607,7 +632,6 @@ module GraphQL
607
632
  end
608
633
 
609
634
  class FieldDefinition < AbstractNode
610
- include DefinitionNode
611
635
  attr_reader :description
612
636
  scalar_methods :name, :type
613
637
  children_methods({
@@ -628,7 +652,6 @@ module GraphQL
628
652
  end
629
653
 
630
654
  class ObjectTypeDefinition < AbstractNode
631
- include DefinitionNode
632
655
  attr_reader :description
633
656
  scalar_methods :name, :interfaces
634
657
  children_methods({
@@ -648,7 +671,6 @@ module GraphQL
648
671
  end
649
672
 
650
673
  class InterfaceTypeDefinition < AbstractNode
651
- include DefinitionNode
652
674
  attr_reader :description
653
675
  scalar_methods :name
654
676
  children_methods({
@@ -670,7 +692,6 @@ module GraphQL
670
692
  end
671
693
 
672
694
  class UnionTypeDefinition < AbstractNode
673
- include DefinitionNode
674
695
  attr_reader :description, :types
675
696
  scalar_methods :name
676
697
  children_methods({
@@ -689,7 +710,6 @@ module GraphQL
689
710
  end
690
711
 
691
712
  class EnumValueDefinition < AbstractNode
692
- include DefinitionNode
693
713
  attr_reader :description
694
714
  scalar_methods :name
695
715
  children_methods({
@@ -699,7 +719,6 @@ module GraphQL
699
719
  end
700
720
 
701
721
  class EnumTypeDefinition < AbstractNode
702
- include DefinitionNode
703
722
  attr_reader :description
704
723
  scalar_methods :name
705
724
  children_methods({
@@ -719,7 +738,6 @@ module GraphQL
719
738
  end
720
739
 
721
740
  class InputObjectTypeDefinition < AbstractNode
722
- include DefinitionNode
723
741
  attr_reader :description
724
742
  scalar_methods :name
725
743
  children_methods({