Skip to content

Commit 1218552

Browse files
Add Graphql support (#32)
* Add Graphql support * use withHeaders instead of withHeader * Formatting --------- Co-authored-by: Vincent Boon <[email protected]>
1 parent 54f6f77 commit 1218552

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function __construct(
164164
$client = app(\JustBetter\MagentoClient\Client\Magento::class);
165165
```
166166

167-
After you got an instance you can use the `get`, `post`, `put` and `patch` methods to use the Magento API.
167+
After you got an instance you can use the `graphql`, `get`, `post`, `put` and `patch` methods to use the Magento API.
168168

169169
### SearchCriteria / Filtering
170170

@@ -198,6 +198,30 @@ $client->connection('connection_one')->get('products');
198198
$client->connection('connection_two')->get('products');
199199
```
200200

201+
### GraphQL
202+
203+
You can run authenticated GraphQL queries using the `graphql` method on the client.
204+
205+
```php
206+
/** @var \JustBetter\MagentoClient\Client\Magento $client */
207+
$client = app(\JustBetter\MagentoClient\Client\Magento::class);
208+
209+
$client->graphql(
210+
'query searchProducts($search: String) {
211+
products(
212+
search: $search
213+
) {
214+
items {
215+
sku
216+
}
217+
}
218+
}',
219+
[
220+
'search' => 'test'
221+
]
222+
);
223+
```
224+
201225
### More Examples
202226

203227
You can view the tests for more examples.

config/magento.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
/* Base path, only modify if your API is not at /rest */
1313
'base_path' => env('MAGENTO_BASE_PATH', 'rest'),
1414

15+
/* Graphql path, only modify if your API is not at /graphql */
16+
'graphql_path' => env('MAGENTO_GRAPHQL_PATH', 'graphql'),
17+
1518
/* Store code, modify if you want to set a store by default. */
1619
'store_code' => env('MAGENTO_STORE_CODE', 'all'),
1720

src/Client/Magento.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace JustBetter\MagentoClient\Client;
44

55
use Generator;
6+
use Illuminate\Http\Client\PendingRequest;
67
use Illuminate\Http\Client\Response;
78
use Illuminate\Support\Enumerable;
89
use Illuminate\Support\LazyCollection;
@@ -35,6 +36,22 @@ public function store(?string $store = null): static
3536
return $this;
3637
}
3738

39+
public function graphql(string $query, array $variables = []): Response
40+
{
41+
/** @var string $endpoint */
42+
$endpoint = config("magento.connections.{$this->connection}.graphql_path");
43+
44+
/** @var Response $response */
45+
$response = $this->request->build($this->connection)
46+
->when($this->storeCode !== null, fn (PendingRequest $request): PendingRequest => $request->withHeaders(['Store' => $this->storeCode]))
47+
->post($endpoint, [
48+
'query' => $query,
49+
'variables' => $variables,
50+
]);
51+
52+
return $response;
53+
}
54+
3855
public function get(string $path, array $data = []): Response
3956
{
4057
/** @var Response $response */
@@ -195,6 +212,7 @@ public static function fake(): void
195212
config()->set('magento.connections.default', [
196213
'base_url' => 'magento',
197214
'base_path' => 'rest',
215+
'graphql_path' => 'graphql',
198216
'store_code' => 'all',
199217
'version' => 'V1',
200218
'access_token' => '::token::',

tests/Client/ClientTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,35 @@
1010

1111
class ClientTest extends TestCase
1212
{
13+
public function test_it_can_make_a_graphql_call(): void
14+
{
15+
Http::fake([
16+
'graphql' => Http::response([
17+
'data' => [
18+
'currency' => [
19+
'base_currency_code' => 'EUR',
20+
],
21+
],
22+
]),
23+
]);
24+
25+
/** @var Magento $magento */
26+
$magento = app(Magento::class);
27+
28+
$response = $magento->store('default')->graphql('query { currency { base_currency_code } }', ['testing']);
29+
30+
$this->assertTrue($response->ok());
31+
$this->assertEquals('EUR', $response->json('data.currency.base_currency_code'));
32+
33+
Http::assertSent(function (Request $request): bool {
34+
35+
return $request->method() === 'POST'
36+
&& $request->url() === 'magento/graphql'
37+
&& $request->body() === '{"query":"query { currency { base_currency_code } }","variables":["testing"]}'
38+
&& $request->header('Store') === ['default'];
39+
});
40+
}
41+
1342
public function test_it_can_make_a_get_call(): void
1443
{
1544
Http::fake([

0 commit comments

Comments
 (0)