diff --git a/README.md b/README.md index 4da4672..77d313a 100644 --- a/README.md +++ b/README.md @@ -3,3 +3,137 @@ Schema Gateway ![unit tests](https://github.com/x-graphql/schema-gateway/actions/workflows/unit_tests.yml/badge.svg) [![codecov](https://codecov.io/gh/x-graphql/schema-gateway/graph/badge.svg?token=ODMTWZfOrL)](https://codecov.io/gh/x-graphql/schema-gateway) + + +![Copyright GraphQL Stitching](https://the-guild.dev/graphql/stitching/_next/static/media/distributed-graph.237d74a1.png) + +**Image source: [GraphQL Stitching](https://the-guild.dev/graphql/stitching)** + + +Getting started +--------------- + +Install this package via [Composer](https://getcomposer.org) + +```shell +composer require x-graphql/schema-gateway +``` + +Add `http-schema` package for creating and executing GraphQL schema over HTTP: + +```shell +composer require x-graphql/http-schema +``` + +Usages +------ + +```php + 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: + ++ [Hasura](https://hasura.io) ++ [GraphQL Stitching](https://the-guild.dev/graphql/stitching/) + +Thanks to all the great people who created these projects! + +Credits +------- + +Created by [Minh Vuong](https://github.com/vuongxuongminh)