1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Digia\GraphQL; |
4
|
|
|
|
5
|
|
|
use Digia\GraphQL\Error\GraphQLError; |
6
|
|
|
use Digia\GraphQL\Execution\Execution; |
7
|
|
|
use Digia\GraphQL\Execution\ExecutionResult; |
8
|
|
|
use Digia\GraphQL\Language\AST\Node\DocumentNode; |
9
|
|
|
use Digia\GraphQL\Language\AST\Node\NodeInterface; |
10
|
|
|
use Digia\GraphQL\Language\AST\Schema\SchemaBuilderInterface; |
11
|
|
|
use Digia\GraphQL\Language\LexerInterface; |
12
|
|
|
use Digia\GraphQL\Language\ParserInterface; |
13
|
|
|
use Digia\GraphQL\Language\PrinterInterface; |
14
|
|
|
use Digia\GraphQL\Language\Source; |
15
|
|
|
use Digia\GraphQL\Type\SchemaInterface; |
16
|
|
|
use Digia\GraphQL\Util\SerializationInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param string|Source $source |
20
|
|
|
* @param array $options |
21
|
|
|
* @return NodeInterface|DocumentNode|SerializationInterface |
22
|
|
|
* @throws GraphQLError |
23
|
|
|
* @throws \Exception |
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 GraphQLError |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
function parseValue($source, array $options = []): NodeInterface |
42
|
|
|
{ |
43
|
|
|
return GraphQL::get(ParserInterface::class)->parseValue( |
44
|
|
|
GraphQL::get(LexerInterface::class) |
45
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
46
|
|
|
->setOptions($options) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string|Source $source |
52
|
|
|
* @param array $options |
53
|
|
|
* @return NodeInterface|SerializationInterface |
54
|
|
|
* @throws GraphQLError |
55
|
|
|
* @throws \Exception |
56
|
|
|
*/ |
57
|
|
|
function parseType($source, array $options = []): NodeInterface |
58
|
|
|
{ |
59
|
|
|
return GraphQL::get(ParserInterface::class)->parseType( |
60
|
|
|
GraphQL::get(LexerInterface::class) |
61
|
|
|
->setSource($source instanceof Source ? $source : new Source($source)) |
62
|
|
|
->setOptions($options) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $source |
68
|
|
|
* @param array $options |
69
|
|
|
* @return SchemaInterface |
70
|
|
|
* @throws \Digia\GraphQL\Error\GraphQLError |
71
|
|
|
* @throws \Exception |
72
|
|
|
*/ |
73
|
|
|
function buildSchema(string $source, array $options = []): SchemaInterface |
74
|
|
|
{ |
75
|
|
|
return GraphQL::get(SchemaBuilderInterface::class)->build(parse($source, $options)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param NodeInterface $node |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
function printNode(NodeInterface $node): string |
83
|
|
|
{ |
84
|
|
|
return GraphQL::getInstance()->get(PrinterInterface::class)->print($node); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param SchemaInterface $schema |
89
|
|
|
* @param string $source |
90
|
|
|
* @param null $rootValue |
|
|
|
|
91
|
|
|
* @param null $contextValue |
92
|
|
|
* @param null $variableValues |
93
|
|
|
* @param null $operationName |
94
|
|
|
* @param callable|null $fieldResolver |
95
|
|
|
* @return ExecutionResult |
96
|
|
|
* @throws Error\GraphQLError |
97
|
|
|
* @throws \Exception |
98
|
|
|
*/ |
99
|
|
|
function graphql( |
100
|
|
|
SchemaInterface $schema, |
101
|
|
|
string $source, |
102
|
|
|
$rootValue = null, |
103
|
|
|
$contextValue = null, |
104
|
|
|
$variableValues = null, |
105
|
|
|
$operationName = null, |
106
|
|
|
callable $fieldResolver = null |
107
|
|
|
): ExecutionResult { |
108
|
|
|
/** @noinspection PhpParamsInspection */ |
109
|
|
|
return Execution::execute( |
110
|
|
|
$schema, |
111
|
|
|
parse($source), |
112
|
|
|
$rootValue, |
113
|
|
|
$contextValue, |
114
|
|
|
$variableValues, |
115
|
|
|
$operationName, |
116
|
|
|
$fieldResolver |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|