idci/graphql-client-bundle

帮助您处理 GraphQL 查询

v1.2.3 2024-02-27 15:59 UTC

README

这个 Symfony 4 扩展包通过客户端、查询构建器和缓存管理来帮助处理 GraphQL API。

安装

使用 Composer

$ composer require idci/graphql-client-bundle

基本配置

定义新的 GuzzleHttp 客户端

eight_points_guzzle:
    clients:
        my_guzzle_client_one:
            base_url: 'http://one.example.com/' # will target http://one.example.com/graphql/ as entrypoint
        my_guzzle_client_two:
            base_url: 'http://two.example.com/'  # will target http://two.example.com/graphql/ as entrypoint

定义新的 GraphQL 客户端

idci_graphql_client:
    clients:
        my_client_one:
            http_client: 'eight_points_guzzle.client.my_guzzle_client_one'
        my_client_two:
            http_client: 'eight_points_guzzle.client.my_guzzle_client_two'
framework:
    cache:
        my_provider_one: "%env(resolve:MY_CACHE_DSN)%"
        pools:
            cache.my_cache_one:
                adapter: app.cache.my_adapter_one.my_cache_one
                default_lifetime: 600
                public: true
            cache.my_cache_two:
                adapter: app.cache.my_adapter_two.my_cache_two
                default_lifetime: 3600
                public: true

然后您可以通过注册表来调用它,例如

<?php

namespace App\Controller;

use IDCI\Bundle\GraphQLClientBundle\Client\GraphQLApiClientRegistryInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class HomeController extends AbstractController
{
    /**
     * @Route("/")
     */
    public function homeAction(GraphQLApiClientRegistryInterface $graphQlApiClientRegistry)
    {
        $firstClient = $graphQlApiClientRegistry->get('my_client_one');
        $secondClient = $graphQlApiClientRegistry->get('my_client_two');
        // ...
    }
}

简单查询构建器

客户端使用查询构建器,简化了 GraphQL 查询的格式化。

<?php

class GraphQLApiClient
{
    public function buildQuery($action, array $requestedFields): GraphQLQuery;

    public function buildMutation($action, array $requestedFields): GraphQLQuery;
}

然后可以使用 GraphQLQuery 对象以字符串格式检索构建的 GraphQL 查询

<?php

$queryString = $query->getGraphQlQuery();
echo $queryString;

或检索查询的结果

<?php

$results = $query->getResults();

示例

字段

<?php

$query = $graphQlApiClientRegistry->get('my_client')->buildQuery(
    'child',
    [
        'name',
        'age',
        'parents' => [
            'name',
        ],
    ]
)->getGraphQlQuery();

将生成

{
  child {
    name
    age
    parents {
      name
    }
  }
}

查询参数

<?php

$query = $graphQlApiClientRegistry->get('my_client')->buildQuery(
    [
        'child' => [
            'name' => 'searchedName'
        ]
    ],
    [
        'name',
        'age',
        'parents' => [
            'name',
        ],
    ]
)->getGraphQlQuery();

将生成

{
  child(name: "searchedName") {
    name
    age
    parents {
      name
    }
  }
}

子查询参数

<?php

$query = $graphQlApiClientRegistry->get('my_client')->buildQuery(
    'child',
    [
        'name',
        'age',
        'parents' => [
            '_parameters' => [ // reserved argument
                'name' => 'searchedName'
            ],
            'name',
            'cars' => [
                'color'
            ]
        ],
    ]
)->getGraphQlQuery();

将生成

{
  child {
    name
    age
    parents(name: "searchedName") {
      name
      cars {
        color
      }
    }
  }
}

片段

<?php

$query = $graphQlApiClientRegistry->get('my_client')->buildQuery(
    'child',
    [
        'name',
        'age',
        'toys' => [
            '_fragments' => [
                'Robot' => [
                    'name',
                    'sensors',
                ],
                'Car' => [
                    'name',
                    'color',
                ],
            ],
        ],
    ]
)->getGraphQlQuery();

将生成

{
  child {
    name
    age
    toys {
      ... on Robot {
        name
        sensors
      }
      ... on Car {
        name
        color
      }
    }
  }
}

别名

<?php

$query = $graphQlApiClientRegistry->get('my_client')->buildQuery(
    'child',
    [
        'name',
        'age',
        'toys' => [
            '_alias' => 'green_toys',
            '_parameters' => [ // reserved argument
                'color' => 'green'
            ],
            'name',
            'color',
        ],
        'toys' => [
            '_alias' => 'blue_toys',
            '_parameters' => [ // reserved argument
                'color' => 'blue'
            ],
            'name',
            'color',
        ],
    ]
)->getGraphQlQuery();

将生成

{
  child {
    name
    age
    green_toys: toys(color: "green") {
      name
      color
    }
    blue_toys: toys(color: "blue") {
      name
      color
    }
  }
}

突变

<?php

$query = $graphQlApiClientRegistry->get('my_client')->buildMutation(
    [
        'child' => [
            'age' => 6
        ]
    ],
    [
        'name',
        'age',
    ]
)->getGraphQlQuery();

将生成

mutation {
  child(age: 6) {
    name
    age
  }
}

流畅查询构建器

您还可以使用具有流畅接口的查询构建器的替代版本(受 doctrine 查询构建器启发)。

<?php

$qb = $graphQlApiClientRegistry->get('my_client')->createQueryBuilder();

$qb
    ->setType('mutation')
    ->setAction('child')
    ->addArgument('age', 6)
    ->addRequestedFields('name')
    ->addRequestedFields('age')
    ->addRequestedFields('toys', [
        '_fragments' => [
            'Robot' => [
                'name',
                'sensors',
            ],
            'Car' => [
                'name',
                'color',
            ],
        ],
    ])
;

$qb->getQuery()->getResults();

?>

将生成

mutation {
  child(age: 6) {
    name
    age
    toys {
      ... on Robot {
        name
        sensors
      }
      ... on Car {
        name
        color
      }
    }
  }
}

缓存

安装 symfony/cache

$ composer require symfony/cache

在您的 config/packages/cache.yaml 中创建新的缓存适配器提供者(官方文档

framework:
  cache:
    # Redis
    app: cache.adapter.redis
    default_redis_provider: "%env(resolve:REDIS_DSN)%"

    pools:
        cache.my_first_adapter: ~
        cache.my_second_adapter: ~

更新您的配置在 config/packages/idci_graphql_client.yaml

idci_graphql_client:
    cache_enabled: true
    clients:
        my_client_one:
            http_client: 'eight_points_guzzle.client.my_guzzle_client_one'
            cache: 'cache.my_first_adapter'
        my_client_two:
            http_client: 'eight_points_guzzle.client.my_guzzle_client_two'
            cache: 'cache.my_second_adapter'

现在当您的客户端执行查询时,结果将被插入或从您的缓存提供者检索

您还可以为特定环境激活/停用缓存,例如

when@dev:
    idci_graphql_client:
        cache_enabled: false

命令

您可以使用以下命令选择要清除的缓存

$ php bin/console cache:pool:clear cache.my_first_adapter
$ php bin/console cache:pool:clear cache.my_second_adapter