convenia / graphql-client

此包的最新版本(2)没有提供许可证信息。

用于执行 GraphQL 请求的客户端

2 2018-03-28 21:11 UTC

This package is not auto-updated.

Last update: 2024-09-17 03:26:44 UTC


README

此包允许您使用纯PHP执行GraphQL请求。

实现

此包包含两个基本类,QueryMutation,在实现查询或变异时需要使用。在两种情况下,您都需要传递一个 GuzzleHttp\Client 的实例和基础URL。每个查询或变异还需要设置名称和所需的输出参数。

示例

use Convenia\GraphQLClient\Mutation;

class UpdateUserMutation extends Mutation
{
    protected $queryName = 'createUser';

    protected $outputParams = [
        'id',
        'name',
        'last_name'
    ];
}

使用方法

在调用变异的方法时,您可以选择使用在模型中设置的输出参数,或者也可以传递所需的参数。

示例

class User
{
    protected $client = new GuzzleHttp\Client();

    protected $baseUrl = 'http://www.mygraphqlapi/v1/graphql'

    public static function create($userId, $data, $returnParams = [])
    {
        $mutation = new UpdateUserMutation($this->baseUrl);

        $mutation->update($userId, $data, $returnParams);
    }
}

响应

此包有一个处理响应的处理程序。此响应类还处理可能的GraphQL错误。