Passed
Push — master ( c180c0...233e00 )
by Divine Niiquaye
12:12
created

GraphQL::run()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\GraphQL;
19
20
use Biurad\Http\Response\JsonResponse;
21
use GraphQL\Language\AST\DocumentNode;
0 ignored issues
show
Bug introduced by
The type GraphQL\Language\AST\DocumentNode was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use GraphQL\Server\Helper;
0 ignored issues
show
Bug introduced by
The type GraphQL\Server\Helper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use GraphQL\Server\ServerConfig;
0 ignored issues
show
Bug introduced by
The type GraphQL\Server\ServerConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use GraphQL\Type\Schema;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Rade\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...
Bug introduced by
The type GraphQL\Type\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use GraphQL\Utils\BuildSchema;
0 ignored issues
show
Bug introduced by
The type GraphQL\Utils\BuildSchema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Psr\Http\Message\RequestInterface;
27
28
/**
29
 * Represents a GraphQL schema.
30
 *
31
 * @author Divine Niiquaye Ibok <[email protected]>
32
 */
33
class GraphQL
34
{
35
    private Schema $schema;
36
37
    public function __construct(DocumentNode|Schema $schema, callable $typeConfigDecorator = null, array $options = [])
38
    {
39
        if ($schema instanceof DocumentNode) {
40
            $schema = (new BuildSchema($schema, $typeConfigDecorator, $options))->buildSchema();
41
        }
42
43
        $this->schema = $schema;
44
    }
45
46
    public function getScheme(): Schema
47
    {
48
        return $this->schema;
49
    }
50
51
    /**
52
     * @param mixed|callable $context
53
     */
54
    public function run(RequestInterface $request, $context): JsonResponse
55
    {
56
        $config = new ServerConfig();
57
        $config->setSchema($this->schema);
58
        $config->setContext($context);
59
60
        $helper = new Helper();
61
        $parsedBody = $helper->parsePsrRequest($request);
62
63
        if (\is_array($parsedBody)) {
64
            $result = $helper->executeBatch($config, $parsedBody);
65
        }
66
67
        return new JsonResponse($result ?? $helper->executeOperation($config, $parsedBody));
68
    }
69
}
70