headzoo/graphql-php

查询 Deskpro GraphQL API 的 PHP 库。

v1.0.5 2018-02-09 17:30 UTC

This package is not auto-updated.

Last update: 2024-09-24 23:06:02 UTC


README

查询 Deskpro GraphQL API 的 PHP 库。

Build Status

需求

  • PHP 5.5+ 与 Composer

安装

composer require deskpro/graphql-php

查询

可以使用原始字符串。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = '
    query GetNews ($id: ID!) {
        content_get_news(id: $id) {
                title
                content
        }
    }
';

$data = $client->execute($query, [
    'id' => 1
]);
print_r($data);

查询构建器

使用查询构建器。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = $client->createQuery('GetNews', [
    '$id' => GraphQL\Type::id(false)
]);
$query->field('content_get_news', 'id: $id', [
    'title',
    'content'
]);

$data = $query->execute([
    'id' => 1
]);
print_r($data);

构建器创建的查询。

query GetNews ($id: ID!) {
    content_get_news(id: $id) {
            title
            content
    }
}

一旦构建了一个查询,就可以多次调用它,并传递不同的参数。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = $client->createQuery('GetNews', [
        '$id' => GraphQL\Type::id(false)
    ])
    ->field('content_get_news', 'id: $id', [
        'title',
        'content'
    ]);

$ids = [1, 2, 3];
foreach($ids as $id) {
    $data = $query->execute(['id' => $id]);
    print_r($data);
}

多个字段

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = $client->createQuery('GetNews', [
        '$newsId'    => GraphQL\Type::id(false),
        '$articleId' => GraphQL\Type::id(false)
    ])
    ->field('content_get_news', 'id: $newsId', [
        'title',
        'content'
    ])
    ->field('content_get_articles', 'id: $articleId', [
        'title',
        'content',
        'categories' => [
            'id',
            'title'
        ]
    ]);

$data = $query->execute([
    'newsId'    => 1,
    'articleId' => 100
]);
print_r($data);

构建器创建的查询。

query GetNews ($newsId: ID!, $articleId: ID!) {
    content_get_news(id: $newsId) {
            title
            content
    }

    content_get_articles(id: $articleId) {
            title
            content
            categories {
                id
                title
            }
    }
}

别名

当查询具有相同名称的多个字段时,必须使用别名。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = $client->createQuery('GetNews', [
        '$id1' => GraphQL\Type::id(false),
        '$id2' => GraphQL\Type::id(false)
    ])
    ->field('news1: content_get_news', 'id: $id1', [
        'title',
        'content'
    ])
    ->field('news2: content_get_news', 'id: $id2', [
        'title',
        'content'
    ]);

$data = $query->execute([
    'id1' => 1,
    'id2' => 2
]);
print_r($data);

构建器创建的查询。

query GetNews ($id2: ID!, $id2: ID!) {
    news1: content_get_news(id: $id1) {
            title
            content
    }
    
    news2: content_get_news(id: $id2) {
            title
            content
    }
}

片段

可以使用片段来描述返回字段。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$fragment = new GraphQL\Fragment('news_fragment', 'News', [
   'title',
   'content' 
]);

$query = $client->createQuery('GetNews', [
        '$id1' => GraphQL\Type::id(false),
        '$id2' => GraphQL\Type::id(false)
    ])
    ->field('news1: content_get_news', 'id: $id1', $fragment)
    ->field('news2: content_get_news', 'id: $id2', $fragment);

$data = $query->execute([
    'id1' => 1,
    'id2' => 100
]);
print_r($data);

构建器创建的查询。

query GetNews ($id2: ID!, $id2: ID!) {
    news1: content_get_news(id: $id1) {
            ...news_fragment
    }
    
    news2: content_get_news(id: $id2) {
            ...news_fragment
    }
}

fragment news_fragment on News {
    title
    content
}

还可以使用片段快捷方法 fragment()

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$fragment = $query->fragment('news_fragment', 'News', [
   'title',
   'content'
]);

$query = $client->createQuery('GetNews', [
        '$id1' => GraphQL\Type::id(false),
        '$id2' => GraphQL\Type::id(false)
    ])
    ->field('news1: content_get_news', 'id: $id1', $fragment)
    ->field('news2: content_get_news', 'id: $id2', $fragment);

指令

使用 @include@skip 指令来控制返回哪些字段。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = $client->createQuery('GetNews', [
    '$id'             => GraphQL\Type::id(false),
    '$withCategories' => GraphQL\Type::boolean(false),
    '$skipTags'       => GraphQL\Type::boolean(false)
]);
$query->field('content_get_articles', 'id: $id', [
    'title',
    'categories' => new GraphQL\Directive('@include', 'if: $withCategories', [
        'id',
        'title'
    ]),
    'tags' => new GraphQL\Directive('@skip', '$skipTags', [
        'title'
    ])
]);

$data = $query->execute([
    'id'             => 1,
    'withCategories' => true,
    'skipTags'       => false
]);
print_r($data);

构建器创建的查询。

query GetNews ($id: ID!, $withCategories: Boolean!, $skipTags: Boolean!) {
    content_get_articles(id: $id) {
            title
            categories @include(if: $withCategories) {
                id
                title
            }
            tags @skip(id: $skipTags) {
                id
            }
    }
}

还可以使用指令快捷方法 includeIf()skipIf()

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$query = $client->createQuery('GetNews', [
    '$id'             => GraphQL\Type::id(false),
    '$withCategories' => GraphQL\Type::boolean(false),
    '$skipTags'       => GraphQL\Type::boolean(false)
]);

$query->field('content_get_articles', 'id: $id', [
    'title',
    'categories' => $query->includeIf('$withCategories', [
        'id',
        'title'
    ]),
    'tags' => $query->skipIf('$skipTags', [
        'title'
    ])
]);

突变

可以使用原始字符串。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$mutation = '
    mutation UpdateArticle ($id: Int, $article: ArticleTypeInput!) {
        content_update_articles(id: $id, article: $article)
    }
';

$data = $client->execute($mutation, [
    'id'      => 100,
    'article' => [
        'title' => 'Hello, World!'
    ]
]);
print_r($data);

突变构建器

使用突变构建器。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setAuthKey(1, 'dev-admin-code');

$mutation = $client->createMutation('UpdateArticle', [
    '$id'      => GraphQL\Type::int(),
    '$article' => GraphQL\Type::object('ArticleTypeInput', false)
]);
$mutation->field('content_update_articles', 'id: $id, article: $article');

$data = $mutation->execute([
    'id'      => 100,
    'article' => [
        'title' => 'Hello, World!'
    ]
]);
print_r($data);

构建器创建的突变。

mutation UpdateArticle ($id: Int, $article: ArticleTypeInput!) {
    content_update_articles(id: $id, article: $article)
}

类型

使用类型类来分配类型值。可用的类有 TypeIDTypeIntTypeFloatTypeStringTypeBooleanTypeObject。使用 TypeListOf 与类型一起定义列表。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');

$query = $client->createQuery('GetNews', [
    '$newsId'    => new GraphQL\TypeID(false),
    '$articleId' => new GraphQL\TypeID(false),
    '$ticket'    => new GraphQL\TypeObject('Ticket')
]);

$query = $client->createQuery('GetNewsItems', [
    '$ids'=> new GraphQL\TypeListOf(new GraphQL\TypeID())
]);

还可以使用快捷静态方法。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');

$query = $client->createQuery('GetNews', [
    '$newsId'    => GraphQL\Type::id(false),
    '$articleId' => GraphQL\Type::id(false),
    '$ticket'    => GraphQL\Type::object('Ticket')
]);

$query = $client->createQuery('GetNewsItems', [
    '$ids'=> GraphQL\Type::listOf(GraphQL\Type::id())
]);

也可以使用普通字符串。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');

$query = $client->createQuery('GetNews', [
    '$newsId'    => 'ID!',
    '$articleId' => 'ID!',
    '$ticket'    => 'Ticket'
]);

$query = $client->createQuery('GetNewsItems', [
    '$ids'=> '[ID]'
]);

默认头部

可以通过传递给 setDefaultHeaders() 方法来在每个请求中发送自定义头部。

<?php
use Deskpro\API\GraphQL;

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setDefaultHeaders([
    'X-Custom-Value' => 'foo'
]);

日志记录

可以通过将 Psr\Log\LoggerInterface 实例传递给 setLogger() 方法来记录请求。

<?php
use Deskpro\API\GraphQL;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$logger = new Logger('GraphQL');
$logger->pushHandler(new StreamHandler('php:/stdout', Logger::DEBUG));

$client = new GraphQL\Client('https://deskpro.company.com');
$client->setLogger($logger);

Guzzle

使用 Guzzle 来进行 HTTP 请求。除非提供,否则将使用默认的 Guzzle 客户端。

<?php
use Deskpro\API\GraphQL;
use GuzzleHttp\Client;

$httpClient = new Client([
    'timeout' => 60
]);

$client = new GraphQL\Client('https://deskpro.company.com', $httpClient);

测试

运行 PHPUnit 测试的 composer "test" 脚本。

composer test