Completed
Push — master ( 8c0519...031fc2 )
by Tomáš
03:59
created

RequestProcessor::setQueryFieldsProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Portiny\GraphQL\GraphQL;
6
7
use GraphQL\Error\Debug;
8
use GraphQL\Executor\Promise\Promise;
9
use GraphQL\Executor\Promise\PromiseAdapter;
10
use GraphQL\GraphQL;
11
use GraphQL\Type\Definition\ObjectType;
12
use GraphQL\Type\Schema;
13
use Portiny\GraphQL\Contract\Http\Request\RequestParserInterface;
14
use Portiny\GraphQL\Contract\Provider\MutationFieldsProviderInterface;
15
use Portiny\GraphQL\Contract\Provider\QueryFieldsProviderInterface;
16
use Throwable;
17
use Tracy\Debugger;
18
19
final class RequestProcessor
20
{
21
	/**
22
	 * @var MutationFieldsProviderInterface
23
	 */
24
	private $mutationFieldsProvider;
25
26
	/**
27
	 * @var QueryFieldsProviderInterface
28
	 */
29
	private $queryFieldsProvider;
30
31 2
	public function setMutationFieldsProvider(MutationFieldsProviderInterface $mutationFieldsProvider): void
32
	{
33 2
		$this->mutationFieldsProvider = $mutationFieldsProvider;
34 2
	}
35
36 2
	public function setQueryFieldsProvider(QueryFieldsProviderInterface $queryFieldsProvider): void
37
	{
38 2
		$this->queryFieldsProvider = $queryFieldsProvider;
39 2
	}
40
41
	/**
42
	 * @param mixed|null $context
43
	 * @param array|null $allowedQueries
44
	 * @param array|null $allowedMutations
45
	 */
46 1
	public function process(
47
		RequestParserInterface $requestParser,
48
		array $rootValue = [],
49
		$context = NULL,
50
		?array $allowedQueries = NULL,
51
		?array $allowedMutations = NULL
52
	): array {
53
		try {
54 1
			$result = GraphQL::executeQuery(
55 1
				$this->createSchema($allowedQueries, $allowedMutations),
56 1
				$requestParser->getQuery(),
57 1
				$rootValue,
58 1
				$context,
59 1
				$requestParser->getVariables()
60
			);
61
62 1
			$output = $result->toArray($this->isDebug());
63
		} catch (Throwable $exception) {
64
			$output = [
65
				'error' => [
66
					'message' => $exception->getMessage(),
67
				],
68
			];
69
		}
70
71 1
		return $output;
72
	}
73
74
	/**
75
	 * @param mixed|null $context
76
	 * @param array|null $allowedQueries
77
	 * @param array|null $allowedMutations
78
	 */
79
	public function processViaPromise(
80
		PromiseAdapter $promiseAdapter,
81
		RequestParserInterface $requestParser,
82
		array $rootValue = [],
83
		$context = NULL,
84
		?array $allowedQueries = NULL,
85
		?array $allowedMutations = NULL
86
	): Promise {
87
		try {
88
			return GraphQL::promiseToExecute(
89
				$promiseAdapter,
90
				$this->createSchema($allowedQueries, $allowedMutations),
91
				$requestParser->getQuery(),
92
				$rootValue,
93
				$context,
94
				$requestParser->getVariables()
95
			);
96
		} catch (Throwable $exception) {
97
			return $promiseAdapter->createRejected($exception);
98
		}
99
	}
100
101 1
	private function createSchema(?array $allowedQueries = NULL, ?array $allowedMutations = NULL): Schema
102
	{
103 1
		return new Schema([
104 1
			'query' => $this->createQueryObject($allowedQueries),
105 1
			'mutation' => $this->createMutationObject($allowedMutations),
106
		]);
107
	}
108
109 1
	private function createQueryObject(?array $allowedQueries = NULL): ObjectType
110
	{
111 1
		return new ObjectType([
112 1
			'name' => 'Query',
113 1
			'fields' => $this->queryFieldsProvider->convertFieldsToArray($allowedQueries),
114
		]);
115
	}
116
117 1
	private function createMutationObject(?array $allowedMutations = NULL): ObjectType
118
	{
119 1
		return new ObjectType([
120 1
			'name' => 'Mutation',
121 1
			'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations),
122
		]);
123
	}
124
125 1
	private function isDebug(): int
126
	{
127 1
		return ! Debugger::$productionMode ? Debug::INCLUDE_DEBUG_MESSAGE | Debug::INCLUDE_TRACE : 0;
128
	}
129
}
130