vendor/webonyx/graphql-php/src/GraphQL.php line 79

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