|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Biurad opensource projects. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright 2019 Biurad Group (https://biurad.com/) |
|
7
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Flange\GraphQL; |
|
14
|
|
|
|
|
15
|
|
|
use GraphQL\Executor\ExecutionResult; |
|
16
|
|
|
use GraphQL\Executor\Promise\Promise; |
|
17
|
|
|
use GraphQL\Language\AST\DocumentNode; |
|
18
|
|
|
use GraphQL\Server\Helper; |
|
19
|
|
|
use GraphQL\Server\ServerConfig; |
|
20
|
|
|
use GraphQL\Type\Schema; |
|
|
|
|
|
|
21
|
|
|
use GraphQL\Utils\BuildSchema; |
|
22
|
|
|
use Psr\Http\Message\RequestInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Represents a GraphQL schema. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class GraphQL |
|
30
|
|
|
{ |
|
31
|
|
|
private Schema $schema; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(DocumentNode|Schema $schema, callable $typeConfigDecorator = null, array $options = []) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($schema instanceof DocumentNode) { |
|
36
|
|
|
$schema = (new BuildSchema($schema, $typeConfigDecorator, $options))->buildSchema(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$this->schema = $schema; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function getScheme(): Schema |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->schema; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed|callable $context |
|
49
|
|
|
* |
|
50
|
|
|
* @return ExecutionResult|ExecutionResult[]|Promise |
|
51
|
|
|
*/ |
|
52
|
|
|
public function run(RequestInterface $request, $context) |
|
53
|
|
|
{ |
|
54
|
|
|
$config = new ServerConfig(); |
|
55
|
|
|
$config->setSchema($this->schema); |
|
56
|
|
|
$config->setContext($context); |
|
57
|
|
|
|
|
58
|
|
|
$helper = new Helper(); |
|
59
|
|
|
$parsedBody = $helper->parsePsrRequest($request); |
|
60
|
|
|
|
|
61
|
|
|
if (\is_array($parsedBody)) { |
|
62
|
|
|
$result = $helper->executeBatch($config, $parsedBody); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $result ?? $helper->executeOperation($config, $parsedBody); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: