wpjshop/graphql-client

WPJ GraphQL API 的 GraphQL 客户端。

v2.0.1 2024-02-28 09:39 UTC

This package is auto-updated.

Last update: 2024-09-28 11:04:51 UTC


README

安装

composer require wpjshop/graphql-client

文档

GraphQL API 文档在这里

实例化客户端

<?php
$client = new \WpjShop\GraphQL\Client('https://your-domain/admin/graphql', '<authentication-token>');

使用 GraphQL 客户端

简单使用

<?php
// Returns product data by ID.
$result = $client->product->get(1);
// Returns products collection.
$result = $client->product->list();
// Returns an array with result status and created product.
$result = $client->product->create(['title' => 'New product']);
// Returns an array with result status and updated product.
$result = $client->product->update(1, ['price' => ['priceWithoutVat' => 100]]);

// Returns parameter data by ID.
$result = $client->parameter->get(1);
// ...

使用自定义字段选择使用

<?php
// Returns an array with data defined by `setSelection` method
$result = $client->product
    ->setSelection(
        [
            'id',
            'variations' => [
                'id',
                'price' => [
                    'withVat',
                    'vat',
                    'currency' => [
                        'code',
                    ],
                ],
            ],
        ]
)->get(1);

GraphQL 客户端参考

每个服务至少有 4 个基本方法

<?php
$client->{service}->get(int $id); // gets item by ID
$client->{service}->list(int $offset = 0, int $limit = 100); // items collection
$client->{service}->create(array $data); // create new item
$client->{service}->update(int $id, array $data); // updates item by ID

服务可能还有针对它们特定的额外方法。例如,更新产品参数。

<?php
$client->product->updateParameter(int $productId, int $parameterId, array $values, bool $append = false);

您可以使用这个库构建自定义查询,这个库是客户端的一部分。

<?php
$client->runQuery($query, bool $resultsAsArray = false, array $variables = []);
$client->runRawQuery(string $queryString, $resultsAsArray = false, array $variables = []);