承接 headzoo/graphql-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

headzoo/graphql-php

Composer 安装命令:

composer require headzoo/graphql-php

包简介

PHP library that queries the Deskpro GraphQL API.

README 文档

README

PHP library that queries the Deskpro GraphQL API.

Build Status

Requirements

  • PHP 5.5+ with Composer

Installing

composer require deskpro/graphql-php

Queries

Raw strings may be used.

<?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);

Query Builder

Using the query builder.

<?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);

The query created by the builder.

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

Once built a query may be called multiple times with different arguments.

<?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);
}

Multiple Fields

<?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);

The query created by the builder.

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

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

Aliases

Aliases must be used when querying multiple fields with the same name.

<?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);

The query created by the builder.

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

Fragments

Fragments may be used to describe return fields.

<?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);

The query created by the builder.

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
}

The fragment shortcut method fragment() may also be used.

<?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);

Directives

Use the @include and @skip directives to control which fields are returned.

<?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);

The query created by the builder.

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
            }
    }
}

The directive shortcut methods includeIf() and skipIf() may also be used.

<?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'
    ])
]);

Mutations

Raw strings may be used.

<?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);

Mutations Builder

Using the mutations builder.

<?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);

The mutation created by the builder.

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

Types

Use type type classes to assign type values. Available classes are TypeID, TypeInt, TypeFloat, TypeString, TypeBoolean, and TypeObject. Use types types in conjunction with TypeListOf to define lists.

<?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())
]);

Shortcut static methods may also be used.

<?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())
]);

You may also use plain strings.

<?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]'
]);

Default Headers

Custom headers may be sent with each request by passing them to the setDefaultHeaders() method.

<?php
use Deskpro\API\GraphQL;

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

Logging

Requests may be logged by providing an instance of Psr\Log\LoggerInterface to the setLogger() method.

<?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 is used to make HTTP requests. A default Guzzle client will be used unless one is provided.

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

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

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

Testing

The composer "test" script runs the PHPUnit tests.

composer test

headzoo/graphql-php 适用场景与选型建议

headzoo/graphql-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18.07k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「graphql」 「deskpro」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 headzoo/graphql-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 headzoo/graphql-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 headzoo/graphql-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 18.07k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2018-02-09