定制 garlic/graphql-wrapper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

garlic/graphql-wrapper

Composer 安装命令:

composer require garlic/graphql-wrapper

包简介

GraphQL wrapper bundle uses for communication among microservices through message bus (by default is RabbitMQ)

README 文档

README

This bundle allow microservices communicate to each other using GraphQL query builder

Installation

Just a couple things are necessary for this bundle works.

Add garlic/bus bundle to your composer.json

composer require garlic/graphql-wrapper

GraphQL way to get result from service (several services)

Important: If you want to use GraphQL wrapper you have to install garlicservices/graphql-bundle on all the services you requiested in your queries. To install bundle on application just type in console the command showed below

composer require garlic/grpahql-bundle

Easy way to use GraphQl query

Simple example of querying data from remote microservice

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

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

$result = $graphQLService->fetch();

Querying internal related objects

Example of querying data from related objects

$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();    

Searching on internal related objects

Example of searching data on included objects

$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();

Querying external related objects (stitchOne)

Example of query stitching to one another by using stitchOne() method (stitched result will be included as an object)

$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();

Querying external related list of objects (stitchMany)

Example of stitching queries to one another by using stitchMany() method (stitched result will be included as list of objects)

$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();

Querying stitching by using internally included objects

Example of stitching queries with fields from internally included objects

$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();

Passing request headers

You could pass any headers you want just by using addHeader method on created query:

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

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

GraphQL mutations

Mutation is the way to change service data by sending some kinds of query. What this queries are and how they could created read below.

Create new data with GraphQL mutation

Example of creating new data row on remote microservice. Method "set" put new fields data in a query and method "select" contains fields that will be returned after query done.

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

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

Update data with GraphQL mutation

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

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

Delete data with GraphQL mutation

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

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

Making async batch request with parallel processing

$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(); 

Query stitching in Mutation

Query stitching works the same way as in query mode. Just try, it's amazing!

Example of Create Mutation with next stitching to the query.

$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();    

You can use stitching with query and mutation and vise-versa. Even several mutation can be stitched to one another.

Enjoy

garlic/graphql-wrapper 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 7
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-12-27