|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author @jenschude <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
namespace Commercetools\Core\Request\GraphQL; |
|
8
|
|
|
|
|
9
|
|
|
use Commercetools\Core\Client\HttpMethod; |
|
10
|
|
|
use Commercetools\Core\Client\JsonRequest; |
|
11
|
|
|
use Commercetools\Core\Model\Common\Context; |
|
12
|
|
|
use Commercetools\Core\Request\AbstractApiRequest; |
|
13
|
|
|
use Commercetools\Core\Response\ResourceResponse; |
|
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
15
|
|
|
use Commercetools\Core\Model\Common\JsonObject; |
|
16
|
|
|
use Commercetools\Core\Response\ApiResponseInterface; |
|
17
|
|
|
use Commercetools\Core\Model\MapperInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @package Commercetools\Core\Request\GraphQL |
|
21
|
|
|
* |
|
22
|
|
|
* @method JsonObject mapResponse(ApiResponseInterface $response) |
|
23
|
|
|
* @method JsonObject mapFromResponse(ApiResponseInterface $response, MapperInterface $mapper = null) |
|
24
|
|
|
*/ |
|
25
|
|
|
class GraphQLQueryRequest extends AbstractApiRequest |
|
26
|
|
|
{ |
|
27
|
|
|
private $query; |
|
28
|
|
|
private $variables; |
|
29
|
|
|
private $operationName; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param Context $context |
|
33
|
|
|
*/ |
|
34
|
3 |
|
public function __construct(Context $context = null) |
|
35
|
|
|
{ |
|
36
|
3 |
|
$this->variables = []; |
|
37
|
3 |
|
parent::__construct(GraphQLEndpoint::endpoint(), $context); |
|
38
|
3 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function buildResponse(ResponseInterface $response) |
|
41
|
|
|
{ |
|
42
|
|
|
return new ResourceResponse($response, $this, $this->getContext()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
public function httpRequest() |
|
46
|
|
|
{ |
|
47
|
|
|
$body = [ |
|
48
|
2 |
|
'query' => $this->query |
|
49
|
|
|
]; |
|
50
|
2 |
|
if (count($this->variables) > 0) { |
|
51
|
1 |
|
$body['variables'] = $this->variables; |
|
52
|
|
|
} |
|
53
|
2 |
|
if (!is_null($this->operationName)) { |
|
54
|
|
|
$body['operationName'] = $this->operationName; |
|
55
|
|
|
} |
|
56
|
2 |
|
return new JsonRequest(HttpMethod::POST, $this->getPath(), $body); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $query |
|
61
|
|
|
* @return $this |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function query($query) |
|
64
|
|
|
{ |
|
65
|
2 |
|
$this->query = $query; |
|
66
|
|
|
|
|
67
|
2 |
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function setVariables(array $variables) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->variables = $variables; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function addVariable($name, $value) |
|
76
|
|
|
{ |
|
77
|
1 |
|
$this->variables[$name] = $value; |
|
78
|
1 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function operationName($name) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->operationName = $name; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
3 |
|
public static function of(Context $context = null) |
|
86
|
|
|
{ |
|
87
|
3 |
|
return new static($context); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|