Completed
Pull Request — master (#49)
by Quang
02:36 queued 24s
created

execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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