Class: GraphQL::InternalRepresentation::Rewrite
- Inherits:
-
Object
- Object
- GraphQL::InternalRepresentation::Rewrite
- Includes:
- Language
- Defined in:
- lib/graphql/internal_representation/rewrite.rb
Overview
Convert an AST into a tree of Nodes
This rides along with StaticValidation, building a tree of nodes.
However, if any errors occurred during validation, the resulting tree is bogus.
(For example, `nil` could have been pushed instead of a type.)
Instance Attribute Summary collapse
-
#operations ⇒ Hash<String => InternalRepresentation::Node>
readonly
Internal representation of each query root (operation, fragment).
Instance Method Summary collapse
-
#initialize ⇒ Rewrite
constructor
A new instance of Rewrite.
- #validate(context) ⇒ Object
Constructor Details
#initialize ⇒ Rewrite
Returns a new instance of Rewrite.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 15 def initialize # { String => Node } Tracks the roots of the query @operations = {} @fragments = {} # [String...] fragments which don't have fragments inside them @independent_fragments = [] # Tracks the current node during traversal # Stack<InternalRepresentation::Node> @nodes = [] # This tracks dependencies from fragment to Node where it was used # { frag_name => [dependent_node, dependent_node]} @fragment_spreads = Hash.new { |h, k| h[k] = []} # [Nodes::Directive ... ] directive affecting the current scope @parent_directives = [] end |
Instance Attribute Details
#operations ⇒ Hash<String => InternalRepresentation::Node> (readonly)
Returns internal representation of each query root (operation, fragment).
13 14 15 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 13 def operations @operations end |
Instance Method Details
#validate(context) ⇒ Object
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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 |
# File 'lib/graphql/internal_representation/rewrite.rb', line 31 def validate(context) visitor = context.visitor visitor[Nodes::OperationDefinition].enter << -> (ast_node, prev_ast_node) { node = Node.new( return_type: context.type_definition.unwrap, ast_node: ast_node, name: ast_node.name, parent: nil, ) @nodes.push(node) @operations[ast_node.name] = node } visitor[Nodes::Field].enter << -> (ast_node, prev_ast_node) { parent_node = @nodes.last node_name = ast_node.alias || ast_node.name # This node might not be novel, eg inside an inline fragment # but it could contain new type information, which is captured below. # (StaticValidation ensures that merging fields is fair game) node = parent_node.children[node_name] ||= begin Node.new( return_type: context.type_definition && context.type_definition.unwrap, ast_node: ast_node, name: node_name, definition_name: ast_node.name, parent: parent_node, ) end object_type = context.parent_type_definition.unwrap node.definitions[object_type] = context.field_definition @nodes.push(node) @parent_directives.push([]) } visitor[Nodes::InlineFragment].enter << -> (ast_node, prev_ast_node) { @parent_directives.push([]) } visitor[Nodes::Directive].enter << -> (ast_node, prev_ast_node) { # It could be a query error where a directive is somewhere it shouldn't be if @parent_directives.any? @parent_directives.last << Node.new( name: ast_node.name, definition_name: ast_node.name, ast_node: ast_node, definitions: [context.directive_definition], # This isn't used, the directive may have many parents in the case of inline fragment parent: nil, ) end } visitor[Nodes::FragmentSpread].enter << -> (ast_node, prev_ast_node) { parent_node = @nodes.last # Record _both sides_ of the dependency spread_node = Node.new( parent: parent_node, name: ast_node.name, ast_node: ast_node, ) # The parent node has a reference to the fragment parent_node.spreads.push(spread_node) # And keep a reference from the fragment to the parent node @fragment_spreads[ast_node.name].push(parent_node) @nodes.push(spread_node) @parent_directives.push([]) } visitor[Nodes::FragmentDefinition].enter << -> (ast_node, prev_ast_node) { node = Node.new( parent: nil, name: ast_node.name, return_type: context.type_definition, ast_node: ast_node, ) @nodes.push(node) @fragments[ast_node.name] = node } visitor[Nodes::InlineFragment].leave << -> (ast_node, prev_ast_node) { @parent_directives.pop } visitor[Nodes::FragmentSpread].leave << -> (ast_node, prev_ast_node) { # Capture any directives that apply to this spread # so that they can be applied to fields when # the fragment is merged in later spread_node = @nodes.pop spread_node.directives.merge(@parent_directives.flatten) @parent_directives.pop } visitor[Nodes::FragmentDefinition].leave << -> (ast_node, prev_ast_node) { # This fragment doesn't depend on any others, # we should save it as the starting point for dependency resolution frag_node = @nodes.pop if !any_fragment_spreads?(frag_node) @independent_fragments << frag_node end } visitor[Nodes::OperationDefinition].leave << -> (ast_node, prev_ast_node) { @nodes.pop } visitor[Nodes::Field].leave << -> (ast_node, prev_ast_node) { # Pop this field's node # and record any directives that were visited # during this field & before it (eg, inline fragments) field_node = @nodes.pop field_node.directives.merge(@parent_directives.flatten) @parent_directives.pop } visitor[Nodes::Document].leave << -> (ast_node, prev_ast_node) { # Resolve fragment dependencies. Start with fragments with no # dependencies and work along the spreads. while fragment_node = @independent_fragments.pop fragment_usages = @fragment_spreads[fragment_node.name] while dependent_node = fragment_usages.pop # remove self from dependent_node.spreads rejected_spread_nodes = dependent_node.spreads.select { |spr| spr.name == fragment_node.name } rejected_spread_nodes.each { |r_node| dependent_node.spreads.delete(r_node) } # resolve the dependency (merge into dependent node) deep_merge(dependent_node, fragment_node, rejected_spread_nodes.first.directives) owner = dependent_node.owner if owner.ast_node.is_a?(Nodes::FragmentDefinition) && !any_fragment_spreads?(owner) @independent_fragments.push(owner) end end end } end |