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 |