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 |