|
| 1 | +# phpnomad/webonyx-integration |
| 2 | + |
| 3 | +[](https://packagist.org/packages/phpnomad/webonyx-integration) |
| 4 | +[](https://packagist.org/packages/phpnomad/webonyx-integration) |
| 5 | +[](https://packagist.org/packages/phpnomad/webonyx-integration) |
| 6 | +[](https://packagist.org/packages/phpnomad/webonyx-integration) |
| 7 | + |
| 8 | +Integrates [`webonyx/graphql-php`](https://github.com/webonyx/graphql-php) with PHPNomad's GraphQL layer. Provides the concrete `GraphQLStrategy` that `phpnomad/graphql` declares as an abstraction, so applications can serve GraphQL queries through the webonyx engine without binding their resolvers to it. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +composer require phpnomad/webonyx-integration |
| 14 | +``` |
| 15 | + |
| 16 | +## What This Provides |
| 17 | + |
| 18 | +- `WebonyxGraphQLStrategy` implements `GraphQLStrategy` from `phpnomad/graphql`. It stitches together SDL fragments and resolver maps contributed by registered `TypeDefinition`s, builds one webonyx `Schema`, and executes queries through `GraphQL::executeQuery()`. |
| 19 | +- Resolver classes are resolved out of a `phpnomad/di` `InstanceProvider`, so a `FieldResolver` can depend on datastores, services, or any other bound class through normal constructor injection. The schema is built lazily on the first `execute()` call and rebuilt when a new type definition is registered. |
| 20 | + |
| 21 | +## Requirements |
| 22 | + |
| 23 | +- PHP 8.2+ |
| 24 | +- `phpnomad/graphql` |
| 25 | +- `phpnomad/di` |
| 26 | +- `webonyx/graphql-php` ^15.0 |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +Bind `WebonyxGraphQLStrategy` to the `GraphQLStrategy` interface in your container, then register a `TypeDefinition` for each slice of your schema. |
| 31 | + |
| 32 | +```php |
| 33 | +<?php |
| 34 | + |
| 35 | +use PHPNomad\GraphQL\Interfaces\FieldResolver; |
| 36 | +use PHPNomad\GraphQL\Interfaces\GraphQLStrategy; |
| 37 | +use PHPNomad\GraphQL\Interfaces\ResolverContext; |
| 38 | +use PHPNomad\GraphQL\Interfaces\TypeDefinition; |
| 39 | +use PHPNomad\GraphQL\Webonyx\Strategies\WebonyxGraphQLStrategy; |
| 40 | + |
| 41 | +$container->bind(GraphQLStrategy::class, WebonyxGraphQLStrategy::class); |
| 42 | + |
| 43 | +class PostSchema implements TypeDefinition |
| 44 | +{ |
| 45 | + public function getSdl(): string |
| 46 | + { |
| 47 | + return <<<SDL |
| 48 | + type Post { id: ID! title: String! } |
| 49 | + extend type Query { posts: [Post!]! } |
| 50 | + SDL; |
| 51 | + } |
| 52 | + |
| 53 | + public function getResolvers(): array |
| 54 | + { |
| 55 | + return [ |
| 56 | + 'Query' => ['posts' => PostsQueryResolver::class], |
| 57 | + ]; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +class PostsQueryResolver implements FieldResolver |
| 62 | +{ |
| 63 | + public function __construct(private readonly PostRepository $posts) |
| 64 | + { |
| 65 | + } |
| 66 | + |
| 67 | + public function resolve(mixed $rootValue, array $args, ResolverContext $context): mixed |
| 68 | + { |
| 69 | + return $this->posts->all(); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +$strategy = $container->get(GraphQLStrategy::class); |
| 74 | +$strategy->registerTypeDefinition(fn() => new PostSchema()); |
| 75 | + |
| 76 | +/** @var ResolverContext $context */ |
| 77 | +$result = $strategy->execute('{ posts { id title } }', [], $context); |
| 78 | +``` |
| 79 | + |
| 80 | +Because resolvers are referenced by class name and instantiated through the container, their constructor dependencies wire through the same DI bindings as the rest of the application. |
| 81 | + |
| 82 | +## Documentation |
| 83 | + |
| 84 | +Framework docs live at [phpnomad.com](https://phpnomad.com). For the underlying engine, see the [`webonyx/graphql-php` repository](https://github.com/webonyx/graphql-php) and its schema and resolver reference. |
| 85 | + |
| 86 | +## License |
| 87 | + |
| 88 | +MIT. See [LICENSE](LICENSE). |
0 commit comments