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 5.2.0-beta.1 10.0.2 5.2.0-rc.1 10.0.3 5.2.0-rc.2 10.0.4 5.2.1 10.0.5 5.2.2 10.0.6 5.2.3 10.1.0 5.2.4 10.1.0-rc.1 5.2.5 10.1.0-rc.2 5.3.0 10.1.0-rc.3 5.3.0-beta.1 10.1.0-rc.4 5.3.0-rc.1 10.1.1 5.3.0-rc.2 10.1.2 5.3.1 10.1.3 5.3.2 10.1.4 5.3.3 10.2.0 5.4.0 10.2.0-beta.1 5.4.0-beta.1 10.2.0-beta.2 5.4.0-rc.1 10.2.0-rc.1 5.4.1 10.2.1 5.4.2 10.2.2 5.4.3 10.2.3 5.4.4 10.2.4 5.4.5 10.3.0 5.5.0 10.3.0-beta.1 5.5.0-beta.1 10.3.0-beta.2 5.5.0-rc.1 10.3.0-rc.1 5.5.0-rc.2 10.3.0-rc.2 5.5.1 10.3.1 5.5.2 10.3.2 5.5.3 10.3.3 5.5.4 10.3.4 5.5.5 10.3.5 5.6.0 10.3.6 5.6.0-beta.1 10.3.7 5.6.0-rc.1 10.3.8 5.6.0-rc.2 10.4.0 5.6.1 10.4.0-beta.1 5.6.2 10.4.0-beta.2 5.6.3 10.4.0-rc.1 5.7.0 10.4.1 5.7.0-beta.1 10.4.2 5.7.0-rc.1 10.4.3 5.7.1 10.4.4 5.7.2 10.5.0 5.7.3 10.5.0-beta.1 5.8.0 10.5.0-beta.2 5.8.0-beta.1 10.5.0-rc.1 5.8.0-beta.2 10.5.0-rc.2 5.8.0-rc.1 10.5.0-rc.3 5.8.1 10.5.1 5.8.2 10.5.2 5.9.0 10.5.3 5.9.0-beta.1 10.6.0 5.9.0-rc.1 10.6.0-beta.1 5.9.0-rc.2 10.6.0-beta.2 5.9.1 10.6.0-rc.1 5.9.2 10.6.1 6.0.0 10.6.2 6.0.0-beta.1 10.7.0 6.0.0-rc.1 10.7.0-beta.1 6.0.1 10.7.0-beta.2 6.0.2 10.7.0-rc.1 6.1.0 3.0.0 6.1.0-beta.1 3.0.1 6.1.0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7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Api / Infrastructure / QueryInfoExtractor.php
woocommerce / src / Api / Infrastructure Last commit date
Schema 2 months ago ClassResolver.php 2 months ago GraphQLControllerBase.php 2 months ago Main.php 2 months ago MetadataController.php 2 months ago Principal.php 2 months ago PrincipalResolver.php 2 months ago QueryInfoExtractor.php 2 days ago ResolverHelpers.php 2 months ago
QueryInfoExtractor.php
242 lines
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Automattic\WooCommerce\Api\Infrastructure;
6
7 use Automattic\WooCommerce\Api\Infrastructure\Schema\ResolveInfo;
8 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\ArgumentNode;
9 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FieldNode;
10 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FragmentDefinitionNode;
11 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\FragmentSpreadNode;
12 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\InlineFragmentNode;
13 use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\SelectionSetNode;
14
15 /**
16 * Extracts a unified query info tree from a GraphQL ResolveInfo.
17 *
18 * The resulting array captures the full query structure: fields, arguments,
19 * sub-selections, inline fragments, and named fragment spreads.
20 *
21 * Structure rules:
22 * - Leaf field (no args, no sub-selection) => true
23 * - Field with sub-selections => nested associative array
24 * - Field arguments => '__args' reserved key
25 * - Inline fragments with a type condition => '...TypeName' prefix key
26 * - Inline fragments without a type condition and named fragment spreads =>
27 * expanded inline (merged into the parent as siblings of the other
28 * selections), matching how GraphQL evaluates them
29 * - Top-level query args included via '__args'
30 */
31 class QueryInfoExtractor {
32 /**
33 * Extract query info from a resolver's ResolveInfo and top-level args.
34 *
35 * @param ResolveInfo $info The GraphQL resolve info.
36 * @param array $args The top-level query arguments.
37 * @return array The unified query info tree.
38 */
39 public static function extract_from_info( ResolveInfo $info, array $args ): array {
40 $result = self::extract( $info->fieldNodes[0]->selectionSet ?? null, $info->variableValues, $info->fragments );
41 if ( ! empty( $args ) ) {
42 $result['__args'] = $args;
43 }
44 return $result;
45 }
46
47 /**
48 * Recursively extract query info from a selection set.
49 *
50 * @internal Recursive helper exposed only for internal callers and tests;
51 * the engine-decoupled entry point for autogenerated resolvers
52 * is {@see self::extract_from_info()}.
53 *
54 * @param ?SelectionSetNode $selection_set The selection set to process.
55 * @param array $variable_values Variable values for resolving arguments.
56 * @param array<string, FragmentDefinitionNode> $fragments Named fragment definitions from the document.
57 * @return array The query info tree for the selection set.
58 */
59 public static function extract( ?SelectionSetNode $selection_set, array $variable_values, array $fragments = array() ): array {
60 $expanded_fragments = array();
61
62 return self::extract_selection_set( $selection_set, $variable_values, $fragments, $expanded_fragments );
63 }
64
65 /**
66 * Recursive worker behind {@see self::extract()}.
67 *
68 * Named fragments are expanded once per extract() call and the result is
69 * reused for every further spread, so the work stays proportional to the
70 * size of the document. This runs after validation, whose limits don't
71 * bound how often a fragment is spread.
72 *
73 * @param ?SelectionSetNode $selection_set The selection set to process.
74 * @param array $variable_values Variable values for resolving arguments.
75 * @param array<string, FragmentDefinitionNode> $fragments Named fragment definitions from the document.
76 * @param array<string, array> $expanded_fragments Memoized expansions, keyed by fragment name. Passed by reference so the whole walk shares one cache.
77 * @return array The query info tree for the selection set.
78 */
79 private static function extract_selection_set( ?SelectionSetNode $selection_set, array $variable_values, array $fragments, array &$expanded_fragments ): array {
80 if ( null === $selection_set ) {
81 return array();
82 }
83
84 $result = array();
85
86 foreach ( $selection_set->selections as $selection ) {
87 if ( $selection instanceof FieldNode ) {
88 $field_name = $selection->name->value;
89 $result[ $field_name ] = self::build_field_entry( $selection, $variable_values, $fragments, $expanded_fragments );
90 } elseif ( $selection instanceof InlineFragmentNode ) {
91 $sub = self::extract_selection_set( $selection->selectionSet, $variable_values, $fragments, $expanded_fragments );
92 if ( null === $selection->typeCondition ) {
93 // No `on Type` clause (e.g. `... @include(if: $flag) { ... }`):
94 // the fragment applies to the parent type, so merge it like
95 // a named fragment spread.
96 $result = self::merge_selections( $result, $sub );
97 } else {
98 $result[ '...' . $selection->typeCondition->name->value ] = $sub;
99 }
100 } elseif ( $selection instanceof FragmentSpreadNode ) {
101 // Expand named fragment spreads inline: their fields become
102 // siblings of the other selections, matching how GraphQL
103 // evaluates them. Consumers of _query_info (mappers that
104 // check array_key_exists for specific fields) see them the
105 // same as if the fragment had been written inline. Use a
106 // recursive merge so overlapping selections are unioned
107 // rather than replaced — `array_merge` would drop the
108 // existing sub-selection under the same field name.
109 $spread = self::expand_fragment( $selection->name->value, $variable_values, $fragments, $expanded_fragments );
110 if ( null === $spread ) {
111 continue;
112 }
113 $result = self::merge_selections( $result, $spread );
114 }
115 }
116
117 return $result;
118 }
119
120 /**
121 * Expand a named fragment into its query info tree, memoizing the result.
122 *
123 * @param string $name The fragment name.
124 * @param array $variable_values Variable values for resolving arguments.
125 * @param array<string, FragmentDefinitionNode> $fragments Named fragment definitions from the document.
126 * @param array<string, array> $expanded_fragments Memoized expansions, keyed by fragment name.
127 * @return ?array The expanded tree, or null when the fragment is not defined.
128 */
129 private static function expand_fragment( string $name, array $variable_values, array $fragments, array &$expanded_fragments ): ?array {
130 if ( array_key_exists( $name, $expanded_fragments ) ) {
131 return $expanded_fragments[ $name ];
132 }
133
134 $fragment = $fragments[ $name ] ?? null;
135 if ( null === $fragment ) {
136 return null;
137 }
138
139 // Seed the entry before recursing so a fragment cycle expands to nothing
140 // instead of recursing forever (defensive: NoFragmentCycles rejects
141 // such documents during validation).
142 $expanded_fragments[ $name ] = array();
143 $expanded_fragments[ $name ] = self::extract_selection_set( $fragment->selectionSet, $variable_values, $fragments, $expanded_fragments );
144
145 return $expanded_fragments[ $name ];
146 }
147
148 /**
149 * Build the entry for a single field node.
150 *
151 * @param FieldNode $field The field node.
152 * @param array $variable_values Variable values for resolving arguments.
153 * @param array<string, FragmentDefinitionNode> $fragments Named fragment definitions from the document.
154 * @param array<string, array> $expanded_fragments Memoized fragment expansions, keyed by fragment name.
155 * @return array|bool True for leaf fields, associative array otherwise.
156 */
157 private static function build_field_entry( FieldNode $field, array $variable_values, array $fragments, array &$expanded_fragments ): array|bool {
158 $has_args = ! empty( $field->arguments ) && count( $field->arguments ) > 0;
159 $has_sub_selection = null !== $field->selectionSet;
160
161 if ( ! $has_args && ! $has_sub_selection ) {
162 return true;
163 }
164
165 $entry = array();
166
167 if ( $has_args ) {
168 $args = array();
169 foreach ( $field->arguments as $arg ) {
170 $args[ $arg->name->value ] = self::resolve_argument_value( $arg, $variable_values );
171 }
172 $entry['__args'] = $args;
173 }
174
175 if ( $has_sub_selection ) {
176 $sub = self::extract_selection_set( $field->selectionSet, $variable_values, $fragments, $expanded_fragments );
177 $entry = self::merge_selections( $entry, $sub );
178 }
179
180 return $entry;
181 }
182
183 /**
184 * Recursively merge two selection trees produced by extract()/build_field_entry().
185 *
186 * Used wherever selections from different sources are combined under
187 * the same key (notably: named fragment spreads expanded inline). Matches
188 * GraphQL's selection-set merge semantics — overlapping fields have their
189 * sub-selections unioned rather than one replacing the other, which a
190 * shallow `array_merge` would do.
191 *
192 * Rules:
193 * - Key only in one side: kept verbatim.
194 * - Both sides arrays: recurse, unioning children.
195 * - One array, one `true` (leaf): keep the array — it carries the
196 * sub-selection detail, and its presence already implies the field
197 * was requested.
198 * - Both `true`: keep `true`.
199 * - `__args` collisions (same field with different argument values):
200 * the second operand wins. Conflicting field args are a GraphQL
201 * validation error upstream of us, so this path is defensive.
202 *
203 * @param array $a First selection tree.
204 * @param array $b Second selection tree, merged into $a.
205 * @return array The merged tree.
206 */
207 private static function merge_selections( array $a, array $b ): array {
208 foreach ( $b as $key => $value ) {
209 if ( ! array_key_exists( $key, $a ) ) {
210 $a[ $key ] = $value;
211 continue;
212 }
213 $existing = $a[ $key ];
214 if ( is_array( $existing ) && is_array( $value ) ) {
215 $a[ $key ] = self::merge_selections( $existing, $value );
216 } elseif ( is_array( $value ) ) {
217 // One side is `true`, the other is a sub-selection array — keep the array.
218 $a[ $key ] = $value;
219 }
220 // Both true, or existing-array + new-true: keep existing.
221 }
222 return $a;
223 }
224
225 /**
226 * Resolve the value of a single argument node, handling variables.
227 *
228 * @param ArgumentNode $arg The argument node.
229 * @param array $variable_values Variable values.
230 * @return mixed The resolved argument value.
231 */
232 private static function resolve_argument_value( ArgumentNode $arg, array $variable_values ): mixed {
233 $value_node = $arg->value;
234
235 if ( $value_node instanceof \Automattic\WooCommerce\Vendor\GraphQL\Language\AST\VariableNode ) {
236 return $variable_values[ $value_node->name->value ] ?? null;
237 }
238
239 return \Automattic\WooCommerce\Vendor\GraphQL\Utils\AST::valueFromASTUntyped( $value_node, $variable_values );
240 }
241 }
242