x-graphql/schema-gateway
Composer 安装命令:
composer require x-graphql/schema-gateway
包简介
Merging all sub schemas into one and add relationships for them
README 文档
README
Image source: GraphQL Stitching
Getting started
Install this package via Composer
composer require x-graphql/schema-gateway
Add http-schema package for creating and executing GraphQL schema over HTTP:
composer require x-graphql/http-schema
Usages
<?php require __DIR__ . '/vendor/autoload.php'; use GraphQL\GraphQL; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Utils\SchemaPrinter; use GraphQL\Type\Schema; use XGraphQL\HttpSchema\HttpDelegator; use XGraphQL\HttpSchema\HttpSchemaFactory; use XGraphQL\SchemaGateway\MandatorySelectionSetProviderInterface; use XGraphQL\SchemaGateway\Relation; use XGraphQL\SchemaGateway\RelationArgumentResolverInterface; use XGraphQL\SchemaGateway\RelationOperation; use XGraphQL\SchemaGateway\SchemaGatewayFactory; use XGraphQL\SchemaGateway\SubSchema; $localSchema = new Schema([ 'query' => new ObjectType([ 'name' => 'Query', 'fields' => [ 'person' => [ 'type' => new ObjectType([ 'name' => 'Person', 'fields' => [ 'name' => Type::nonNull(Type::string()), 'fromCountry' => Type::nonNull(Type::string()), ], ]), 'resolve' => fn() => ['name' => 'John Doe', 'fromCountry' => 'VN'] ], ], ]), ]); $localSubSchema = new SubSchema('local', $localSchema); $remoteSchema = HttpSchemaFactory::createFromIntrospectionQuery( new HttpDelegator('https://countries.trevorblades.com/'), ); $remoteSubSchema = new SubSchema('remote', $remoteSchema); $countryRelation = new Relation( 'Person', 'remoteCountry', RelationOperation::QUERY, 'country', new class implements RelationArgumentResolverInterface, MandatorySelectionSetProviderInterface { public function shouldKeep(string $argumentName, Relation $relation): bool { return false; } public function resolve(array $objectValue, array $currentArgs, Relation $relation): array { return ['code' => $objectValue['fromCountry']]; } public function getMandatorySelectionSet(Relation $relation): string { return '{ fromCountry }'; } } ); $schemaGateway = SchemaGatewayFactory::create([$localSubSchema, $remoteSubSchema], [$countryRelation]); $query = <<<'GQL' query { continents { name } person { name remoteCountry { name code } } } GQL; var_dump(SchemaPrinter::doPrint($schemaGateway)); var_dump(GraphQL::executeQuery($schemaGateway, $query)->toArray());
Rules when merging sub schemas:
- Top-level field names need to be unique across all merged schemas (case-sensitive match).
- Types with the exact same name and structure will be merged. But types with the same name but different structure will result in type conflicts.
Inspiration
This library has been inspired by many others related work including:
Thanks to all the great people who created these projects!
Credits
Created by Minh Vuong
x-graphql/schema-gateway 适用场景与选型建议
x-graphql/schema-gateway 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.38k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 03 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 x-graphql/schema-gateway 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 x-graphql/schema-gateway 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 40.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-11