Skip to content

Commit c31e46c

Browse files
committed
Update: add configurable headers for graphql query
1 parent c330fd2 commit c31e46c

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

Client/GraphQLApiClient.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ public function query(GraphQLQuery $graphQlQuery, bool $cache = true): ?array
8989

9090
try {
9191
$response = $this->httpClient->request('POST', $graphQlQuery->hasEndpoint() ? $graphQlQuery->getEndpoint() : '', array_merge([
92-
'headers' => [
93-
'Accept-Encoding' => 'gzip',
94-
'Accept-Language' => $graphQlQuery->getLocale() ?? $this->translator->getLocale(),
95-
],
92+
'headers' => array_merge(
93+
[
94+
'Accept-Encoding' => 'gzip',
95+
'Accept-Language' => $graphQlQuery->getLocale() ?? $this->translator->getLocale(),
96+
],
97+
$graphQlQuery->getHeaders()
98+
),
9699
], $this->buildRequestParams($graphQlQuery)));
97100
} catch (RequestException $e) {
98101
$this->logger->error('Error in GraphQLApiClient', [

Query/GraphQLQuery.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ class GraphQLQuery
5151
/**
5252
* @var array
5353
*/
54-
private $files;
54+
private $files = [];
55+
56+
/**
57+
* @var array
58+
*/
59+
private $headers = [];
5560

5661
/**
5762
* @var string|null
@@ -118,6 +123,32 @@ public function hasFiles(): bool
118123
return !empty($this->files);
119124
}
120125

126+
public function getHeaders(): array
127+
{
128+
return $this->headers;
129+
}
130+
131+
public function setHeaders(array $headers): self
132+
{
133+
$this->headers = $headers;
134+
135+
return $this;
136+
}
137+
138+
public function addHeader(string $key, $value): self
139+
{
140+
$this->headers[$key] = $value;
141+
142+
return $this;
143+
}
144+
145+
public function removeHeader(string $key): self
146+
{
147+
unset($this->headers[$key]);
148+
149+
return $this;
150+
}
151+
121152
public function getAction(): string
122153
{
123154
return $this->action;

0 commit comments

Comments
 (0)