Skip to content

Commit be08c3f

Browse files
Busfoxrobwittman
authored andcommitted
add-basic-graphql-functionality (robwittman#27)
1 parent 7579627 commit be08c3f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,40 @@ $service = new Shopify\Service\ProductService($client);
7979
$service->delete($productId);
8080
```
8181

82+
### GraphQL
83+
84+
#### Query
85+
```php
86+
$service = new Shopify\Service\GraphQLService($client);
87+
$service->graph(
88+
'{
89+
products(query: "created_at:<2019", first: 5) {
90+
edges {
91+
node {
92+
title
93+
description
94+
}
95+
}
96+
}
97+
}'
98+
);
99+
```
100+
101+
#### Mutation
102+
```php
103+
$service = new Shopify\Service\GraphQLService($client);
104+
$service->graph(
105+
'mutation productCreate($input: ProductInput!){
106+
productCreate(input: $input) {
107+
product {
108+
id
109+
}
110+
}
111+
}',
112+
['input' => ['title' => 'Sweet new product','productType' => 'Snowboard','vendor' => 'JadedPixel']]
113+
);
114+
```
115+
82116
## Authentication
83117
84118
Authentication to Shopify's API is done through access tokens, which are obtained through OAuth. To get a

lib/Service/GraphQLService.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Shopify\Service;
4+
5+
class GraphQLService extends AbstractService
6+
{
7+
8+
public function graph(string $query, array $variables = [])
9+
{
10+
// Build the request
11+
$request = ['query' => $query];
12+
if (count($variables) > 0) {
13+
$request['variables'] = $variables;
14+
}
15+
16+
// Create the request, pass the access token and optional parameters
17+
$endpoint = '/admin/api/graphql.json';
18+
$response = $this->request(
19+
$endpoint,
20+
'POST',
21+
$request
22+
);
23+
24+
return (object) [
25+
'response' => $response
26+
];
27+
}
28+
}

0 commit comments

Comments
 (0)