euautomation/graphql-client

此包已被废弃,不再维护。未建议替代包。

一个简单的客户端,用于在 GraphQL 中执行查询和突变。

v0.3.0 2020-01-05 14:54 UTC

README

一个简单的包,用于消费 GraphQL API。

安装

composer require euautomation/graphql-client

使用

创建一个 EUAutomation\GraphQL\Client 实例

new Client($graphQLURL);

或者

$client = new Client();
$client->setUrl($graphQLURL);

响应类

传入您的查询、可选变量和头信息(例如 bearer 令牌),$variables 和 $headers 是可选的

$response = $client->response($query, $variables, $headers);

all()

使用 $response->all(); 获取响应中返回的所有数据

errors()

使用 $response->errors(); 获取响应中返回的所有错误

hasErrors()

使用 $response->hasErrors(); 检查响应中是否包含任何错误

响应类中的特定数据

例如,假设您想要获取所有类别的列表并执行此查询。

{
    allCategories(first:10) {
        category {
            id,
            name,
            slug,
            description
        }
    }
}

现在为了从响应类中获取一些有意义的数据,您可以执行以下操作

$categories = $response->allCategories->category;

foreach($categories as $category) {
    // Do something with the data?
    $category->id;
    $category->name;
    $category->slug;
    $category->description;
}

您还可以在响应类中设置、取消设置或检查数据。

其他响应

原始 guzzle 响应

传入您的查询、可选变量和头信息(例如 bearer 令牌),$variables 和 $headers 是可选的

$client->raw($query, $variables, $headers);

JSON

传入您的查询、可选变量和头信息(例如 bearer 令牌),$variables 和 $headers 是可选的

$client->json($query, $variables, $headers);