Completed
Pull Request — master (#49)
by Quang
02:07
created

execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 7
dl 0
loc 19
rs 9.4285
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 = [],
101
    $operationName = null,
102
    callable $fieldResolver = null
103
): ExecutionResult {
104
    /** @noinspection PhpParamsInspection */
105
    return GraphQL::get(ExecutionInterface::class)
106
        ->execute(
107
            $schema,
108
            parse($source),
109
            $rootValue,
110
            $contextValue,
111
            $variableValues,
112
            $operationName,
113
            $fieldResolver
114
        );
115
}
116
117
/**
118
 * Note that this is temporary needed
119
 * We can remove this later
120
 *
121
 * @param SchemaInterface $schema
122
 * @param DocumentNode    $documentNode
123
 * @param null            $rootValue
0 ignored issues
show
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 $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 $rootValue is correct as it would always require null to be passed?
Loading history...
124
 * @param null            $contextValue
125
 * @param null            $variableValues
126
 * @param null            $operationName
127
 * @param callable|null   $fieldResolver
128
 * @return ExecutionResult
129
 */
130
function execute(
131
    SchemaInterface $schema,
132
    DocumentNode $documentNode,
133
    $rootValue = null,
134
    $contextValue = null,
135
    $variableValues = [],
136
    $operationName = null,
137
    callable $fieldResolver = null
138
): ExecutionResult
139
{
140
    return GraphQL::get(ExecutionInterface::class)
141
        ->execute(
142
            $schema,
143
            $documentNode,
144
            $rootValue,
145
            $contextValue,
146
            $variableValues,
147
            $operationName,
148
            $fieldResolver
149
        );
150
}
151