PHP code example of edstevo / laravel-shopify-graph
1. Go to this page and download the library: Download edstevo/laravel-shopify-graph library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use EdStevo\LaravelShopifyGraph\LaravelShopifyGraphRequest;
class CreateBlogArticleRequest extends LaravelShopifyGraphRequest
{
public function __construct(public BlogArticle $article) {}
public function query(): string
{
return '
mutation CreateArticle($article: ArticleCreateInput!) {
articleCreate(article: $article) {
article {
id
}
userErrors {
code
field
message
}
}
}
';
}
public function variables(): array
{
return [
'article' => ArticleCreateInput::from($this->article->toShopifyPayload())->toArray(),
];
}
public function transformResponse(array $data): mixed
{
return $data['articleCreate']['article']['id'] ?? null;
}
}
$request = new CreateBlogArticleRequest(BlogArticle::first());
$shopifyId = $request->post('your-shop.myshopify.com', 'access_token');
use EdStevo\LaravelShopifyGraph\LaravelShopifyGraphJob;
use Illuminate\Contracts\Queue\ShouldQueue;
class CreateBlogArticleJob extends LaravelShopifyGraphJob implements ShouldQueue
{
public function __construct(
public BlogArticle $article,
public string $shopDomain,
public string $accessToken
) {}
public function getShopDomain(): string
{
return $this->shopDomain;
}
public function getAccessToken(): string
{
return $this->accessToken;
}
public function query(): string
{
return '
mutation CreateArticle($article: ArticleCreateInput!) {
articleCreate(article: $article) {
article {
id
}
userErrors {
code
field
message
}
}
}
';
}
public function variables(): array
{
return [
'article' => ArticleCreateInput::from($this->article->toShopifyPayload())->toArray(),
];
}
public function handleResponse(array $data): void
{
$this->article->update([
'shopify_id' => $data['articleCreate']['article']['id'] ?? null,
]);
}
}
CreateBlogArticleJob::dispatch(BlogArticle::first(), 'your-shop.myshopify.com', 'access_token');