GraphQL::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 3
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Flange\GraphQL\Schema. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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