Error
2 months ago
Executor
2 months ago
Language
2 months ago
Server
2 months ago
Type
2 months ago
Utils
2 months ago
Validator
2 months ago
Deferred.php
2 months ago
GraphQL.php
2 months ago
GraphQL.php
266 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\GraphQL; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\GraphQL\Error\Error; |
| 6 | use Automattic\WooCommerce\Vendor\GraphQL\Error\InvariantViolation; |
| 7 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\ExecutionResult; |
| 8 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Executor; |
| 9 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\Adapter\SyncPromiseAdapter; |
| 10 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\Promise; |
| 11 | use Automattic\WooCommerce\Vendor\GraphQL\Executor\Promise\PromiseAdapter; |
| 12 | use Automattic\WooCommerce\Vendor\GraphQL\Language\AST\DocumentNode; |
| 13 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Parser; |
| 14 | use Automattic\WooCommerce\Vendor\GraphQL\Language\Source; |
| 15 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Directive; |
| 16 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\ScalarType; |
| 17 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Definition\Type; |
| 18 | use Automattic\WooCommerce\Vendor\GraphQL\Type\Schema as SchemaType; |
| 19 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\DocumentValidator; |
| 20 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules\QueryComplexity; |
| 21 | use Automattic\WooCommerce\Vendor\GraphQL\Validator\Rules\ValidationRule; |
| 22 | |
| 23 | /** |
| 24 | * This is the primary facade for fulfilling Automattic\WooCommerce\Vendor\GraphQL operations. |
| 25 | * See [related documentation](executing-queries.md). |
| 26 | * |
| 27 | * @phpstan-import-type ArgsMapper from Executor |
| 28 | * @phpstan-import-type FieldResolver from Executor |
| 29 | * |
| 30 | * @see \Automattic\WooCommerce\Vendor\GraphQL\Tests\GraphQLTest |
| 31 | */ |
| 32 | class GraphQL |
| 33 | { |
| 34 | /** |
| 35 | * Executes graphql query. |
| 36 | * |
| 37 | * More sophisticated Automattic\WooCommerce\Vendor\GraphQL servers, such as those which persist queries, |
| 38 | * may wish to separate the validation and execution phases to a static time |
| 39 | * tooling step, and a server runtime step. |
| 40 | * |
| 41 | * Available options: |
| 42 | * |
| 43 | * schema: |
| 44 | * The Automattic\WooCommerce\Vendor\GraphQL type system to use when validating and executing a query. |
| 45 | * source: |
| 46 | * A Automattic\WooCommerce\Vendor\GraphQL language formatted string representing the requested operation. |
| 47 | * rootValue: |
| 48 | * The value provided as the first argument to resolver functions on the top |
| 49 | * level type (e.g. the query object type). |
| 50 | * contextValue: |
| 51 | * The context value is provided as an argument to resolver functions after |
| 52 | * field arguments. It is used to pass shared information useful at any point |
| 53 | * during executing this query, for example the currently logged in user and |
| 54 | * connections to databases or other services. |
| 55 | * If the passed object implements the `ScopedContext` interface, |
| 56 | * its `clone()` method will be called before passing the context down to a field. |
| 57 | * This allows passing information to child fields in the query tree without affecting sibling or parent fields. |
| 58 | * variableValues: |
| 59 | * A mapping of variable name to runtime value to use for all variables |
| 60 | * defined in the requestString. |
| 61 | * operationName: |
| 62 | * The name of the operation to use if requestString contains multiple |
| 63 | * possible operations. Can be omitted if requestString contains only |
| 64 | * one operation. |
| 65 | * fieldResolver: |
| 66 | * A resolver function to use when one is not provided by the schema. |
| 67 | * If not provided, the default field resolver is used (which looks for a |
| 68 | * value on the source value with the field's name). |
| 69 | * validationRules: |
| 70 | * A set of rules for query validation step. Default value is all available rules. |
| 71 | * Empty array would allow to skip query validation (may be convenient for persisted |
| 72 | * queries which are validated before persisting and assumed valid during execution) |
| 73 | * |
| 74 | * @param string|DocumentNode $source |
| 75 | * @param mixed $rootValue |
| 76 | * @param mixed $contextValue |
| 77 | * @param array<string, mixed>|null $variableValues |
| 78 | * @param array<ValidationRule>|null $validationRules |
| 79 | * |
| 80 | * @api |
| 81 | * |
| 82 | * @throws \Exception |
| 83 | * @throws InvariantViolation |
| 84 | */ |
| 85 | public static function executeQuery( |
| 86 | SchemaType $schema, |
| 87 | $source, |
| 88 | $rootValue = null, |
| 89 | $contextValue = null, |
| 90 | ?array $variableValues = null, |
| 91 | ?string $operationName = null, |
| 92 | ?callable $fieldResolver = null, |
| 93 | ?array $validationRules = null |
| 94 | ): ExecutionResult { |
| 95 | $promiseAdapter = new SyncPromiseAdapter(); |
| 96 | |
| 97 | $promise = self::promiseToExecute( |
| 98 | $promiseAdapter, |
| 99 | $schema, |
| 100 | $source, |
| 101 | $rootValue, |
| 102 | $contextValue, |
| 103 | $variableValues, |
| 104 | $operationName, |
| 105 | $fieldResolver, |
| 106 | $validationRules |
| 107 | ); |
| 108 | |
| 109 | return $promiseAdapter->wait($promise); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Same as executeQuery(), but requires PromiseAdapter and always returns a Promise. |
| 114 | * Useful for Async PHP platforms. |
| 115 | * |
| 116 | * @param string|DocumentNode $source |
| 117 | * @param mixed $rootValue |
| 118 | * @param mixed $context |
| 119 | * @param array<string, mixed>|null $variableValues |
| 120 | * @param array<ValidationRule>|null $validationRules Defaults to using all available rules |
| 121 | * |
| 122 | * @api |
| 123 | * |
| 124 | * @throws \Exception |
| 125 | */ |
| 126 | public static function promiseToExecute( |
| 127 | PromiseAdapter $promiseAdapter, |
| 128 | SchemaType $schema, |
| 129 | $source, |
| 130 | $rootValue = null, |
| 131 | $context = null, |
| 132 | ?array $variableValues = null, |
| 133 | ?string $operationName = null, |
| 134 | ?callable $fieldResolver = null, |
| 135 | ?array $validationRules = null |
| 136 | ): Promise { |
| 137 | try { |
| 138 | $documentNode = $source instanceof DocumentNode |
| 139 | ? $source |
| 140 | : Parser::parse(new Source($source, 'GraphQL')); |
| 141 | |
| 142 | if ($validationRules === null) { |
| 143 | $queryComplexity = DocumentValidator::getRule(QueryComplexity::class); |
| 144 | assert($queryComplexity instanceof QueryComplexity, 'should not register a different rule for QueryComplexity'); |
| 145 | |
| 146 | $queryComplexity->setRawVariableValues($variableValues); |
| 147 | } else { |
| 148 | foreach ($validationRules as $rule) { |
| 149 | if ($rule instanceof QueryComplexity) { |
| 150 | $rule->setRawVariableValues($variableValues); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | $validationErrors = DocumentValidator::validate($schema, $documentNode, $validationRules); |
| 156 | |
| 157 | if ($validationErrors !== []) { |
| 158 | return $promiseAdapter->createFulfilled( |
| 159 | new ExecutionResult(null, $validationErrors) |
| 160 | ); |
| 161 | } |
| 162 | |
| 163 | return Executor::promiseToExecute( |
| 164 | $promiseAdapter, |
| 165 | $schema, |
| 166 | $documentNode, |
| 167 | $rootValue, |
| 168 | $context, |
| 169 | $variableValues, |
| 170 | $operationName, |
| 171 | $fieldResolver |
| 172 | ); |
| 173 | } catch (Error $e) { |
| 174 | return $promiseAdapter->createFulfilled( |
| 175 | new ExecutionResult(null, [$e]) |
| 176 | ); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns directives defined in Automattic\WooCommerce\Vendor\GraphQL spec. |
| 182 | * |
| 183 | * @deprecated use {@see Directive::builtInDirectives()} |
| 184 | * |
| 185 | * @throws InvariantViolation |
| 186 | * |
| 187 | * @return array<string, Directive> |
| 188 | * |
| 189 | * @api |
| 190 | */ |
| 191 | public static function getStandardDirectives(): array |
| 192 | { |
| 193 | return Directive::builtInDirectives(); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Returns built-in scalar types defined in Automattic\WooCommerce\Vendor\GraphQL spec. |
| 198 | * |
| 199 | * @deprecated use {@see Type::builtInScalars()} |
| 200 | * |
| 201 | * @throws InvariantViolation |
| 202 | * |
| 203 | * @return array<string, ScalarType> |
| 204 | * |
| 205 | * @api |
| 206 | */ |
| 207 | public static function getStandardTypes(): array |
| 208 | { |
| 209 | return Type::builtInScalars(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Replaces standard types with types from this list (matching by name). |
| 214 | * |
| 215 | * Standard types not listed here remain untouched. |
| 216 | * |
| 217 | * @deprecated prefer per-schema scalar overrides via {@see \Automattic\WooCommerce\Vendor\GraphQL\Type\SchemaConfig::$types} or {@see \Automattic\WooCommerce\Vendor\GraphQL\Type\SchemaConfig::$typeLoader} |
| 218 | * |
| 219 | * @param array<string, ScalarType> $types |
| 220 | * |
| 221 | * @api |
| 222 | * |
| 223 | * @throws InvariantViolation |
| 224 | */ |
| 225 | public static function overrideStandardTypes(array $types): void |
| 226 | { |
| 227 | Type::overrideStandardTypes($types); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns standard validation rules implementing Automattic\WooCommerce\Vendor\GraphQL spec. |
| 232 | * |
| 233 | * @return array<class-string<ValidationRule>, ValidationRule> |
| 234 | * |
| 235 | * @api |
| 236 | */ |
| 237 | public static function getStandardValidationRules(): array |
| 238 | { |
| 239 | return DocumentValidator::defaultRules(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Set default resolver implementation. |
| 244 | * |
| 245 | * @phpstan-param FieldResolver $fn |
| 246 | * |
| 247 | * @api |
| 248 | */ |
| 249 | public static function setDefaultFieldResolver(callable $fn): void |
| 250 | { |
| 251 | Executor::setDefaultFieldResolver($fn); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Set default args mapper implementation. |
| 256 | * |
| 257 | * @phpstan-param ArgsMapper $fn |
| 258 | * |
| 259 | * @api |
| 260 | */ |
| 261 | public static function setDefaultArgsMapper(callable $fn): void |
| 262 | { |
| 263 | Executor::setDefaultArgsMapper($fn); |
| 264 | } |
| 265 | } |
| 266 |