1 | <?php |
||
17 | final class RequestProcessor |
||
18 | { |
||
19 | /** |
||
20 | * @var RequestParserInterface |
||
21 | */ |
||
22 | private $requestParser; |
||
23 | |||
24 | /** |
||
25 | * @var MutationFieldsProviderInterface |
||
26 | */ |
||
27 | private $mutationFieldsProvider; |
||
28 | |||
29 | /** |
||
30 | * @var QueryFieldsProviderInterface |
||
31 | */ |
||
32 | private $queryFieldsProvider; |
||
33 | |||
34 | 2 | public function __construct(RequestParserInterface $requestParser) |
|
35 | { |
||
36 | 2 | $this->requestParser = $requestParser; |
|
37 | 2 | } |
|
38 | |||
39 | 2 | public function setMutationFieldsProvider(MutationFieldsProviderInterface $mutationFieldsProvider): void |
|
40 | { |
||
41 | 2 | $this->mutationFieldsProvider = $mutationFieldsProvider; |
|
42 | 2 | } |
|
43 | |||
44 | 2 | public function setQueryFieldsProvider(QueryFieldsProviderInterface $queryFieldsProvider): void |
|
45 | { |
||
46 | 2 | $this->queryFieldsProvider = $queryFieldsProvider; |
|
47 | 2 | } |
|
48 | |||
49 | /** |
||
50 | * @param mixed|null $context |
||
51 | * @param array|null $allowedQueries |
||
52 | * @param array|null $allowedMutations |
||
53 | */ |
||
54 | 1 | public function process( |
|
55 | array $rootValue = [], |
||
56 | $context = NULL, |
||
57 | ?array $allowedQueries = NULL, |
||
58 | ?array $allowedMutations = NULL |
||
59 | ): array { |
||
60 | try { |
||
61 | 1 | $result = GraphQL::executeQuery( |
|
62 | 1 | $this->createSchema($allowedQueries, $allowedMutations), |
|
63 | 1 | $this->requestParser->getQuery(), |
|
64 | 1 | $rootValue, |
|
65 | 1 | $context, |
|
66 | 1 | $this->requestParser->getVariables() |
|
67 | ); |
||
68 | |||
69 | 1 | $output = $result->toArray($this->isDebug()); |
|
70 | } catch (Throwable $exception) { |
||
71 | $output = [ |
||
72 | 'error' => [ |
||
73 | 'message' => $exception->getMessage(), |
||
74 | ], |
||
75 | ]; |
||
76 | } |
||
77 | |||
78 | 1 | return $output; |
|
79 | } |
||
80 | |||
81 | 1 | private function createSchema(?array $allowedQueries = NULL, ?array $allowedMutations = NULL): Schema |
|
82 | { |
||
83 | 1 | return new Schema([ |
|
84 | 1 | 'query' => $this->createQueryObject($allowedQueries), |
|
85 | 1 | 'mutation' => $this->createMutationObject($allowedMutations), |
|
86 | ]); |
||
87 | } |
||
88 | |||
89 | 1 | private function createQueryObject(?array $allowedQueries = NULL): ObjectType |
|
96 | |||
97 | 1 | private function createMutationObject(?array $allowedMutations = NULL): ObjectType |
|
98 | { |
||
99 | 1 | return new ObjectType([ |
|
100 | 1 | 'name' => 'Mutation', |
|
101 | 1 | 'fields' => $this->mutationFieldsProvider->convertFieldsToArray($allowedMutations), |
|
104 | |||
105 | 1 | private function isDebug(): int |
|
109 | } |
||
110 |