Module: GraphQL::StaticValidation::FieldsWillMerge

Defined in:
lib/graphql/static_validation/rules/fields_will_merge.rb

Overview

-- @requires_ancestor: GraphQL::StaticValidation::BaseVisitor @requires_ancestor: GraphQL::StaticValidation::BaseVisitor::ContextMethods

Defined Under Namespace

Classes: Field

Constant Summary collapse

NO_ARGS =

Validates that a selection set is valid if all fields (including spreading any fragments) either correspond to distinct response names or can be merged without ambiguity.

Optimized algorithm based on: https://tech.new-work.se/graphql-overlapping-fields-can-be-merged-fast-ea6e92e0a01

Instead of comparing fields, fields-vs-fragments, and fragments-vs-fragments separately (which leads to exponential recursion through nested fragments), we flatten all fragment spreads into a single field map and compare within it.

GraphQL::EmptyObjects::EMPTY_HASH
EXCLUSIVE_COMPARISON =
1
NONEXCLUSIVE_COMPARISON =
2

Instance Method Summary collapse

Instance Method Details

#initialize ⇒ Object



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
# File 'lib/graphql/static_validation/rules/fields_will_merge.rb', line 53

def initialize(*)
  super
  @conflict_count = 0
  @max_errors = context.max_errors
  @fragments = context.fragments
  # Track which sub-selection node pairs have been compared to prevent
  # infinite recursion with cyclic fragments
  @compared_sub_selections = {}.compare_by_identity
  @compared_field_groups = {}
  @field_group_signatures = {}.compare_by_identity
  @field_selection_signatures = {}.compare_by_identity
  # Cache collect_fields results for sub-selection comparison
  @sub_fields_cache = {}.compare_by_identity

  @allow_legacy_invalid_field_conflicts = @schema.allow_legacy_invalid_field_conflicts
  # Unless legacy conflicts are rejected, use the legacy check, which is the same as previous versions.
  # It uses `legacy_mutually_exclusive?` and the caches above. The spec check uses `@spec_caches`.
  @legacy_exclusivity = @allow_legacy_invalid_field_conflicts != false
  @spec_caches = nil
  @mutually_exclusive_cache = nil
  # True when the spec check can find conflicts which the legacy check doesn't find
  @spec_check_needed = false
  # During the legacy check, true when the spec rule treats the parent fields as mutually exclusive
  @spec_inherited_exclusive = false
  # The pairs of nodes which have a conflict in the legacy check
  @legacy_conflict_nodes = nil
  # When this is true, record conflicts in `@spec_conflicts` instead of adding errors
  @find_spec_conflicts = false
  @spec_conflicts = nil
end

#on_field(node, _parent) ⇒ Object



89
90
91
92
93
94
# File 'lib/graphql/static_validation/rules/fields_will_merge.rb', line 89

def on_field(node, _parent)
  if !node.selections.empty? && selections_may_conflict?(node.selections)
    check_selection_set(node, type_definition)
  end
  super
end

#on_operation_definition(node, _parent) ⇒ Object



84
85
86
87
# File 'lib/graphql/static_validation/rules/fields_will_merge.rb', line 84

def on_operation_definition(node, _parent)
  check_selection_set(node, type_definition)
  super
end