arrowsphere/catalog-graphql-client

ArrowSphere 目录 GraphQL API 的官方 PHP 客户端

0.7.3 2024-09-06 15:11 UTC

README

Latest Stable Version Minimum PHP Version Build Status

此包提供了一个用于 ArrowSphere 目录 GraphQL API 的 PHP 客户端。它应该是使用 PHP 代码调用 ArrowSphere 目录 GraphQL API 的唯一方式。

要使用此包,您需要有效的 ArrowSphere 访问权限,以及来自 ArrowSphere 认证平台的有效令牌。

安装

使用以下命令安装最新版本

$ composer require arrowsphere/catalog-graphql-client

基本用法

<?php

use ArrowSphere\CatalogGraphQLClient\CatalogGraphQLClient;
use ArrowSphere\CatalogGraphQLClient\Input\SearchBody;
use ArrowSphere\CatalogGraphQLClient\Types\ArrowsphereIdentifier;
use ArrowSphere\CatalogGraphQLClient\Types\Identifiers;
use ArrowSphere\CatalogGraphQLClient\Types\Product;
use ArrowSphere\CatalogGraphQLClient\Types\Program;
use ArrowSphere\CatalogGraphQLClient\Types\VendorIdentifier;

const URL = 'https://your-url-to-arrowsphere.example.com';

$token = 'my token'; // The logic to get the token is not implemented in this package

$client = new CatalogGraphQLClient(URL, $token);

// The filters are defined as a nested array
// They allow you to limit the data you want to see
$filters = [
    Product::CLASSIFICATION => 'SaaS',
    Product::IDENTIFIERS => [
        Identifiers::VENDOR => [
            VendorIdentifier::SKU => '031C9E47-4802-4248-838E-778FB1D2CC05',
        ],
    ],
    Product::PROGRAM => [
        Program::LEGACY_CODE => 'microsoft',
    ],
];

// The fields are also defined as a nested array
// They allow you to limit the fields returned by the GraphQL API, to see only the necessary fields for your need
$fields = [
    Product::NAME,
    Product::IDENTIFIERS => [
        Identifiers::ARROWSPHERE => [
            ArrowsphereIdentifier::ORDERABLE_SKU,
        ],
        Identifiers::VENDOR => [
            VendorIdentifier::SKU,
        ]
    ]
];

$searchBody = [
    SearchBody::MARKETPLACE => 'US',
    SearchBody::FILTERS     => $filters,
];

$result = $client->find($searchBody, $fields);

$products = $result->getProducts();
if (count($products) === 1) {
    $product = $products[0];
    echo sprintf(
        "Product SKU %s : name = %s, orderable SKU = %s",
        $product->getIdentifiers()->getVendor()->getSku(),
        $product->getName(),
        $product->getIdentifiers()->getArrowsphere()->getOrderableSku()
     ) . PHP_EOL;
}

更多信息

此库基于在 ArrowSphere\CatalogGraphQLClient\Types 命名空间中定义的实体返回结果。

CatalogGraphQLClient 类中的 find 方法是一个简化方法,它调用 API 中的 getProducts 查询,并返回一个 PaginatedProducts 实例,允许您访问原始查询中请求的任何字段。

请注意,每个字段都是可空的,您需要请求字段以使其由 API 填充。

此外,在 CatalogGraphQLClient 类中还有一个通用的 call 方法,允许您在 GraphQL API 上执行任何查询。此方法不提供任何帮助,因此直接使用比较复杂。建议使用 find 方法。