garlic/graphql-wrapper

GraphQL包装器包用于通过消息总线(默认为RabbitMQ)在微服务之间进行通信

1.1 2019-04-19 11:27 UTC

README

此包允许微服务使用GraphQL查询构建器相互通信

安装

为此包工作,只需要几样东西。

将garlic/bus包添加到您的composer.json

composer require garlic/graphql-wrapper

GraphQL方式从服务(多个服务)获取结果

重要:如果您想使用GraphQL包装器,必须在您的查询中请求的所有服务上安装garlicservices/graphql-bundle。要在应用程序上安装包,只需在控制台中输入以下命令

composer require garlic/grpahql-bundle

轻松使用GraphQl查询

查询远程微服务的简单示例

$graphQLService = $this->get(GraphQLService::class);

$addressQuery = $graphQLService->createQuery('serviceName.QueryName');
$addressQuery
    ->select('id', 'city', 'zipcode')
    ->where('country = Ukraine');

$result = $graphQLService->fetch();

查询内部相关对象

查询相关对象的示例

$graphQLService = $this->get(GraphQLService::class);

$apartmentQuery = $graphQLService->createQuery('serviceName.QueryName');
$apartmentQuery
    ->select('id', 'buildYear', 'address.id', 'address.city', 'address.country')
    ->where('size = 5');
    
$result = $graphQLService->fetch();    

在内部相关对象上搜索

在包含对象上搜索数据的示例

$graphQLService = $this->get(GraphQLService::class);

$apartmentQuery = $graphQLService->createQuery('serviceName.QueryName');
$apartmentQuery
    ->select('id', 'buildYear', 'address.id', 'address.city', 'address.country')
    ->where('size = 5', 'address.country = Ukraine');
    
$result = $graphQLService->fetch();

查询外部相关对象(stitchOne)

使用stitchOne()方法相互查询的示例(拼接结果将作为对象包含)

$graphQLService = $this->get(GraphQLService::class);

$addressQuery = $graphQLService->createQuery('firstServiceName.QueryName');
$addressQuery
    ->select('id', 'city', 'country')
    ->where('country = Ukraine')
;

$apartmentQuery = $graphQLService->createQuery('secondServiceName.QueryName');
$apartmentQuery
    ->select('id', 'size', 'addressId')
    ->where('size = 5')
    ->stitchOne($addressQuery, 'address', 'addressId', 'id')
;

$result = $graphQLService->fetch();

查询外部相关对象列表(stitchMany)

使用stitchMany()方法相互拼接查询的示例(拼接结果将作为对象列表包含)

$graphQLService = $this->get(GraphQLService::class);

$addressQuery = $graphQLService->createQuery('firstServiceName.QueryName');
$addressQuery
    ->select('id', 'city', 'country')
    ->where('country = Ukraine')
;

$apartmentQuery = $graphQLService->createQuery('secondServiceName.QueryName');
$apartmentQuery
    ->select('id', 'size', 'addressId')
    ->where('size = 5')
    ->stitchMany($addressQuery, 'address', 'addressId', 'id')
;

$result = $graphQLService->fetch();

使用内部包含对象进行查询拼接

使用内部包含对象字段的查询拼接示例

$graphQLService = $this->get(GraphQLService::class);

$addressQuery = $graphQLService->createQuery('firstServiceName.QueryName');
$addressQuery
    ->select('id', 'city', 'country')
    ->where('country = Ukraine')
;

$apartmentQuery = $graphQLService->createQuery('secondServiceName.QueryName');
$apartmentQuery
    ->select('id', 'size', 'address.id', 'address.city', 'address.country')
    ->where('size = 5')
    ->stitchOne($addressQuery, 'fullAddress', 'address.id', 'id')
;

$result = $graphQLService->fetch();

传递请求头

您只需使用创建的查询上的addHeader方法即可传递任何您想要的头信息

$graphQLService = $this->get(GraphQLService::class);

$apartmentQuery = $graphQLService->createQuery('secondServiceName.QueryName');
$apartmentQuery
    ->select(...)
    ->where(...)
    ->addHeader('Authorization', 'abc');

GraphQL突变

突变是通过发送某种类型的查询来更改服务数据的方式。以下介绍了这些查询以及如何创建它们。

使用GraphQL突变创建新数据

创建远程微服务上新数据行的示例。方法"set"将新字段数据放入查询中,而方法"select"包含查询完成后将返回的字段。

$graphQLService = $this->get(GraphQLService::class);

$apartmentMutation = $graphQLService->createNewMutation('ServiceName.CreateMutationName');
$apartmentMutation
    ->set('size = 3', 'buildYear = 2018')
    ->select('id');
    
$result = $graphQLService->fetch();    

使用GraphQL突变更新数据

$graphQLService = $this->get(GraphQLService::class);

$apartmentMutation = $graphQLService->createUpdateMutation('ServiceName.UpdateMutationName');
$apartmentMutation
    ->set('size = 3', 'buildYear = 2018')
    ->where('size = 5')
    ->select('id');
    
$result = $graphQLService->fetch();    

使用GraphQL突变删除数据

$graphQLService = $this->get(GraphQLService::class);

$apartmentMutation = $graphQLService->createDeleteMutation('ServiceName.DeleteMutationName');
$apartmentMutation
    ->where('size = 5')
    ->select('id');
    
$result = $graphQLService->fetch();    

异步批量请求并行处理

$graphQLService = $this->get(GraphQLService::class);

$addressMutation = $graphQLService->createNewMutation('template.AddressCreate');
$addressMutation
    ->select('id', 'country', 'city')
    ->set('country = Ukraine', 'city = Boyarka', 'street = Kyivska', 'zipcode = 20214', 'house = 1');

$apartmentQuery = $graphQLService->createQuery('template.AddressFind');
$apartmentQuery
    ->select('id')
    ->where(['id' => 123])
;

$result = $graphQLService->fetchAsync(); 

突变中的查询拼接

查询拼接与查询模式的工作方式相同。试试看,太棒了!

创建带有后续查询拼接的创建突变示例。

$graphQLService = $this->get(GraphQLService::class);

$addressMutation = $graphQLService->createNewMutation('FirstServiceName.CreateMutationName');
$addressMutation
    ->set('city = Kyiv', 'country = Ukraine')
    ->select('id');
    
$apartmentQuery = $graphQLService->createQuery('SecondServiceName.QueryName');
$apartmentQuery
    ->select('id', 'size', 'address.id', 'address.city', 'address.country')
    ->where('size = 5')
    ->stitchOne($addressMutation, 'newAddress', 'address.country', 'country')
;    
    
$result = $graphQLService->fetch();    

您可以使用拼接与查询和突变,反之亦然。甚至可以将多个突变拼接在一起。

享受吧