Passed
Pull Request — master (#49)
by Quang
02:10
created

executor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Digia\GraphQL;
4
5
use Digia\GraphQL\Error\InvariantException;
6
use Digia\GraphQL\Execution\Execution;
7
use Digia\GraphQL\Execution\ExecutionInterface;
8
use Digia\GraphQL\Execution\ExecutionResult;
9
use Digia\GraphQL\Language\AST\Node\DocumentNode;
10
use Digia\GraphQL\Language\AST\Node\NodeInterface;
11
use Digia\GraphQL\Language\AST\Schema\SchemaBuilderInterface;
12
use Digia\GraphQL\Language\LexerInterface;
13
use Digia\GraphQL\Language\ParserInterface;
14
use Digia\GraphQL\Language\PrinterInterface;
15
use Digia\GraphQL\Language\Source;
16
use Digia\GraphQL\Type\SchemaInterface;
17
use Digia\GraphQL\Util\SerializationInterface;
18
19
/**
20
 * @param string|Source $source
21
 * @param array         $options
22
 * @return NodeInterface|DocumentNode|SerializationInterface
23
 * @throws InvariantException
24
 */
25
function parse($source, array $options = []): NodeInterface
26
{
27
    return GraphQL::get(ParserInterface::class)->parse(
28
        GraphQL::get(LexerInterface::class)
29
            ->setSource($source instanceof Source ? $source : new Source($source))
30
            ->setOptions($options)
31
    );
32
}
33
34
/**
35
 * @param string|Source $source
36
 * @param array         $options
37
 * @return NodeInterface|SerializationInterface
38
 * @throws InvariantException
39
 */
40
function parseValue($source, array $options = []): NodeInterface
41
{
42
    return GraphQL::get(ParserInterface::class)->parseValue(
43
        GraphQL::get(LexerInterface::class)
44
            ->setSource($source instanceof Source ? $source : new Source($source))
45
            ->setOptions($options)
46
    );
47
}
48
49
/**
50
 * @param string|Source $source
51
 * @param array         $options
52
 * @return NodeInterface|SerializationInterface
53
 * @throws InvariantException
54
 */
55
function parseType($source, array $options = []): NodeInterface
56
{
57
    return GraphQL::get(ParserInterface::class)->parseType(
58
        GraphQL::get(LexerInterface::class)
59
            ->setSource($source instanceof Source ? $source : new Source($source))
60
            ->setOptions($options)
61
    );
62
}
63
64
/**
65
 * @param string $source
66
 * @param array  $options
67
 * @return SchemaInterface
68
 * @throws InvariantException
69
 */
70
function buildSchema(string $source, array $options = []): SchemaInterface
71
{
72
    return GraphQL::get(SchemaBuilderInterface::class)->build(parse($source, $options));
73
}
74
75
/**
76
 * @param NodeInterface $node
77
 * @return string
78
 */
79
function printNode(NodeInterface $node): string
80
{
81
    return GraphQL::getInstance()->get(PrinterInterface::class)->print($node);
82
}
83
84
/**
85
 * @param SchemaInterface $schema
86
 * @param string          $source
87
 * @param null            $rootValue
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $operationName is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $contextValue is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $variableValues is correct as it would always require null to be passed?
Loading history...
Documentation Bug introduced by
Are you sure the doc-type for parameter $rootValue is correct as it would always require null to be passed?
Loading history...
88
 * @param null            $contextValue
89
 * @param null            $variableValues
90
 * @param null            $operationName
91
 * @param callable|null   $fieldResolver
92
 * @return ExecutionResult
93
 * @throws InvariantException
94
 */
95
function graphql(
96
    SchemaInterface $schema,
97
    string $source,
98
    $rootValue = null,
99
    $contextValue = null,
100
    $variableValues = null,
101
    $operationName = null,
102
    callable $fieldResolver = null
103
): ExecutionResult {
104
    /** @noinspection PhpParamsInspection */
105
    return executor()->execute(
106
        $schema,
107
        parse($source),
108
        $rootValue,
109
        $contextValue,
110
        $variableValues,
111
        $operationName,
112
        $fieldResolver
113
    );
114
}
115
116
/**
117
 * @return ExecutionInterface
118
 */
119
function executor(): ExecutionInterface
120
{
121
    return GraphQL::getInstance()->get(ExecutionInterface::class);
122
}
123