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.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 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 4 weeks ago ClassResolver.php 4 weeks ago GraphQLControllerBase.php 4 weeks ago Main.php 4 weeks ago MetadataController.php 4 weeks ago Principal.php 4 weeks ago PrincipalResolver.php 4 weeks ago QueryInfoExtractor.php 4 weeks ago ResolverHelpers.php 4 weeks ago
QueryInfoExtractor.php
187 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 => '...TypeName' prefix key
26 * - Named fragment spreads => expanded inline (merged into the parent as
27 * siblings of the other selections), matching how GraphQL evaluates them
28 * - Top-level query args included via '__args'
29 */
30 class QueryInfoExtractor {
31 /**
32 * Extract query info from a resolver's ResolveInfo and top-level args.
33 *
34 * @param ResolveInfo $info The GraphQL resolve info.
35 * @param array $args The top-level query arguments.
36 * @return array The unified query info tree.
37 */
38 public static function extract_from_info( ResolveInfo $info, array $args ): array {
39 $result = self::extract( $info->fieldNodes[0]->selectionSet ?? null, $info->variableValues, $info->fragments );
40 if ( ! empty( $args ) ) {
41 $result['__args'] = $args;
42 }
43 return $result;
44 }
45
46 /**
47 * Recursively extract query info from a selection set.
48 *
49 * @internal Recursive helper exposed only for internal callers and tests;
50 * the engine-decoupled entry point for autogenerated resolvers
51 * is {@see self::extract_from_info()}.
52 *
53 * @param ?SelectionSetNode $selection_set The selection set to process.
54 * @param array $variable_values Variable values for resolving arguments.
55 * @param array<string, FragmentDefinitionNode> $fragments Named fragment definitions from the document.
56 * @return array The query info tree for the selection set.
57 */
58 public static function extract( ?SelectionSetNode $selection_set, array $variable_values, array $fragments = array() ): array {
59 if ( null === $selection_set ) {
60 return array();
61 }
62
63 $result = array();
64
65 foreach ( $selection_set->selections as $selection ) {
66 if ( $selection instanceof FieldNode ) {
67 $field_name = $selection->name->value;
68 $result[ $field_name ] = self::build_field_entry( $selection, $variable_values, $fragments );
69 } elseif ( $selection instanceof InlineFragmentNode ) {
70 $type_name = $selection->typeCondition->name->value;
71 $key = '...' . $type_name;
72 $result[ $key ] = self::extract( $selection->selectionSet, $variable_values, $fragments );
73 } elseif ( $selection instanceof FragmentSpreadNode ) {
74 // Expand named fragment spreads inline: their fields become
75 // siblings of the other selections, matching how GraphQL
76 // evaluates them. Consumers of _query_info (mappers that
77 // check array_key_exists for specific fields) see them the
78 // same as if the fragment had been written inline. Use a
79 // recursive merge so overlapping selections are unioned
80 // rather than replaced — `array_merge` would drop the
81 // existing sub-selection under the same field name.
82 $fragment = $fragments[ $selection->name->value ] ?? null;
83 if ( null === $fragment ) {
84 continue;
85 }
86 $spread = self::extract( $fragment->selectionSet, $variable_values, $fragments );
87 $result = self::merge_selections( $result, $spread );
88 }
89 }
90
91 return $result;
92 }
93
94 /**
95 * Build the entry for a single field node.
96 *
97 * @param FieldNode $field The field node.
98 * @param array $variable_values Variable values for resolving arguments.
99 * @param array<string, FragmentDefinitionNode> $fragments Named fragment definitions from the document.
100 * @return array|bool True for leaf fields, associative array otherwise.
101 */
102 private static function build_field_entry( FieldNode $field, array $variable_values, array $fragments ): array|bool {
103 $has_args = ! empty( $field->arguments ) && count( $field->arguments ) > 0;
104 $has_sub_selection = null !== $field->selectionSet;
105
106 if ( ! $has_args && ! $has_sub_selection ) {
107 return true;
108 }
109
110 $entry = array();
111
112 if ( $has_args ) {
113 $args = array();
114 foreach ( $field->arguments as $arg ) {
115 $args[ $arg->name->value ] = self::resolve_argument_value( $arg, $variable_values );
116 }
117 $entry['__args'] = $args;
118 }
119
120 if ( $has_sub_selection ) {
121 $sub = self::extract( $field->selectionSet, $variable_values, $fragments );
122 $entry = self::merge_selections( $entry, $sub );
123 }
124
125 return $entry;
126 }
127
128 /**
129 * Recursively merge two selection trees produced by extract()/build_field_entry().
130 *
131 * Used wherever selections from different sources are combined under
132 * the same key (notably: named fragment spreads expanded inline). Matches
133 * GraphQL's selection-set merge semantics — overlapping fields have their
134 * sub-selections unioned rather than one replacing the other, which a
135 * shallow `array_merge` would do.
136 *
137 * Rules:
138 * - Key only in one side: kept verbatim.
139 * - Both sides arrays: recurse, unioning children.
140 * - One array, one `true` (leaf): keep the array — it carries the
141 * sub-selection detail, and its presence already implies the field
142 * was requested.
143 * - Both `true`: keep `true`.
144 * - `__args` collisions (same field with different argument values):
145 * the second operand wins. Conflicting field args are a GraphQL
146 * validation error upstream of us, so this path is defensive.
147 *
148 * @param array $a First selection tree.
149 * @param array $b Second selection tree, merged into $a.
150 * @return array The merged tree.
151 */
152 private static function merge_selections( array $a, array $b ): array {
153 foreach ( $b as $key => $value ) {
154 if ( ! array_key_exists( $key, $a ) ) {
155 $a[ $key ] = $value;
156 continue;
157 }
158 $existing = $a[ $key ];
159 if ( is_array( $existing ) && is_array( $value ) ) {
160 $a[ $key ] = self::merge_selections( $existing, $value );
161 } elseif ( is_array( $value ) ) {
162 // One side is `true`, the other is a sub-selection array — keep the array.
163 $a[ $key ] = $value;
164 }
165 // Both true, or existing-array + new-true: keep existing.
166 }
167 return $a;
168 }
169
170 /**
171 * Resolve the value of a single argument node, handling variables.
172 *
173 * @param ArgumentNode $arg The argument node.
174 * @param array $variable_values Variable values.
175 * @return mixed The resolved argument value.
176 */
177 private static function resolve_argument_value( ArgumentNode $arg, array $variable_values ): mixed {
178 $value_node = $arg->value;
179
180 if ( $value_node instanceof \Automattic\WooCommerce\Vendor\GraphQL\Language\AST\VariableNode ) {
181 return $variable_values[ $value_node->name->value ] ?? null;
182 }
183
184 return \Automattic\WooCommerce\Vendor\GraphQL\Utils\AST::valueFromASTUntyped( $value_node, $variable_values );
185 }
186 }
187