Module: GraphQL::InternalRepresentation::Rewrite
- Includes:
- Language
- Defined in:
- lib/graphql/internal_representation/rewrite.rb
Overview
While visiting an AST, build a normalized, flattened tree of Nodes.
No unions or interfaces are present in this tree, only object types.
Selections from the AST are attached to the object types they apply to.
Inline fragments and fragment spreads are preserved in InternalRepresentation::Node#ast_spreads, where they can be used to check for the presence of directives. This might not be sufficient for future directives, since the selections' grouping is lost.
The rewritten query tree serves as the basis for the FieldsWillMerge
validation.
Constant Summary collapse
- NO_DIRECTIVES =
[].freeze
Instance Attribute Summary collapse
-
#rewrite_document ⇒ Object
readonly
InternalRepresentation::Document.
Instance Method Summary collapse
- #initialize ⇒ Object
- #on_field(ast_node, ast_parent) ⇒ Object
- #on_fragment_definition(ast_node, parent) ⇒ Object
- #on_fragment_spread(ast_node, ast_parent) ⇒ Object
- #on_inline_fragment(node, parent) ⇒ Object
- #on_operation_definition(ast_node, parent) ⇒ Object
-
#operations ⇒ Hash<String, Node>
Roots of this query.
- #push_root_node(ast_node, definitions) ⇒ Object
- #skip?(ast_node) ⇒ Boolean
Methods included from Language
Instance Attribute Details
#rewrite_document ⇒ Object (readonly)
Returns InternalRepresentation::Document.
22 23 24 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 22 def rewrite_document @rewrite_document end |
Instance Method Details
#initialize ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 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 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 24 def initialize(*) super @query = context.query @rewrite_document = InternalRepresentation::Document.new # Hash<Nodes::FragmentSpread => Set<InternalRepresentation::Node>> # A record of fragment spreads and the irep nodes that used them @rewrite_spread_parents = Hash.new { |h, k| h[k] = Set.new } # Hash<Nodes::FragmentSpread => Scope> @rewrite_spread_scopes = {} # Array<Set<InternalRepresentation::Node>> # The current point of the irep_tree during visitation @rewrite_nodes_stack = [] # Array<Scope> @rewrite_scopes_stack = [] @rewrite_skip_nodes = Set.new # Resolve fragment spreads. # Fragment definitions got their own irep trees during visitation. # Those nodes are spliced in verbatim (not copied), but this is OK # because fragments are resolved from the "bottom up", each fragment # can be shared between its usages. context.on_dependency_resolve do |defn_ast_node, spread_ast_nodes, frag_ast_node| frag_name = frag_ast_node.name fragment_node = @rewrite_document.fragment_definitions[frag_name] if fragment_node spread_ast_nodes.each do |spread_ast_node| parent_nodes = @rewrite_spread_parents[spread_ast_node] parent_scope = @rewrite_spread_scopes[spread_ast_node] parent_nodes.each do |parent_node| parent_node.deep_merge_node(fragment_node, scope: parent_scope, merge_self: false) end end end end end |
#on_field(ast_node, ast_parent) ⇒ Object
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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 120 def on_field(ast_node, ast_parent) if skip?(ast_node) @rewrite_skip_nodes.add(ast_node) end if @rewrite_skip_nodes.empty? node_name = ast_node.alias || ast_node.name parent_nodes = @rewrite_nodes_stack.last next_nodes = [] field_defn = context.field_definition if field_defn.nil? # It's a non-existent field new_scope = nil else field_return_type = field_defn.type @rewrite_scopes_stack.last.each do |scope_type| parent_nodes.each do |parent_node| node = parent_node.scoped_children[scope_type][node_name] ||= Node.new( parent: parent_node, name: node_name, owner_type: scope_type, query: @query, return_type: field_return_type, ) node.ast_nodes << ast_node node.definitions << field_defn next_nodes << node end end new_scope = Scope.new(@query, field_return_type.unwrap) end @rewrite_nodes_stack.push(next_nodes) @rewrite_scopes_stack.push(new_scope) end super if @rewrite_skip_nodes.empty? @rewrite_nodes_stack.pop @rewrite_scopes_stack.pop end if @rewrite_skip_nodes.include?(ast_node) @rewrite_skip_nodes.delete(ast_node) end end |
#on_fragment_definition(ast_node, parent) ⇒ Object
71 72 73 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 71 def on_fragment_definition(ast_node, parent) push_root_node(ast_node, @rewrite_document.fragment_definitions) { super } end |
#on_fragment_spread(ast_node, ast_parent) ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 169 def on_fragment_spread(ast_node, ast_parent) if @rewrite_skip_nodes.empty? && !skip?(ast_node) # Register the irep nodes that depend on this AST node: @rewrite_spread_parents[ast_node].merge(@rewrite_nodes_stack.last) @rewrite_spread_scopes[ast_node] = @rewrite_scopes_stack.last end super end |
#on_inline_fragment(node, parent) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 97 def on_inline_fragment(node, parent) # Inline fragments provide two things to the rewritten tree: # - They _may_ narrow the scope by their type condition # - They _may_ apply their directives to their children if skip?(node) @rewrite_skip_nodes.add(node) end if @rewrite_skip_nodes.empty? @rewrite_scopes_stack.push(@rewrite_scopes_stack.last.enter(context.type_definition)) end super if @rewrite_skip_nodes.empty? @rewrite_scopes_stack.pop end if @rewrite_skip_nodes.include?(node) @rewrite_skip_nodes.delete(node) end end |
#on_operation_definition(ast_node, parent) ⇒ Object
67 68 69 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 67 def on_operation_definition(ast_node, parent) push_root_node(ast_node, @rewrite_document.operation_definitions) { super } end |
#operations ⇒ Hash<String, Node>
Returns Roots of this query.
62 63 64 65 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 62 def operations GraphQL::Deprecation.warn "#{self.class}#operations is deprecated; use `document.operation_definitions` instead" @document.operation_definitions end |
#push_root_node(ast_node, definitions) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 75 def push_root_node(ast_node, definitions) # Either QueryType or the fragment type condition owner_type = context.type_definition defn_name = ast_node.name node = Node.new( parent: nil, name: defn_name, owner_type: owner_type, query: @query, ast_nodes: [ast_node], return_type: owner_type, ) definitions[defn_name] = node @rewrite_scopes_stack.push(Scope.new(@query, owner_type)) @rewrite_nodes_stack.push([node]) yield @rewrite_nodes_stack.pop @rewrite_scopes_stack.pop end |
#skip?(ast_node) ⇒ Boolean
178 179 180 181 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 178 def skip?(ast_node) dir = ast_node.directives dir.any? && !GraphQL::Execution::DirectiveChecks.include?(dir, @query) end |