Skip to content

Repository files navigation

phpnomad/webonyx-integration

Latest VersionTotal DownloadsPHP VersionLicense

Integrates 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.

Installation

composer require phpnomad/webonyx-integration

What This Provides

  • WebonyxGraphQLStrategy implements GraphQLStrategy from phpnomad/graphql. It stitches together SDL fragments and resolver maps contributed by registered TypeDefinitions, builds one webonyx Schema, and executes queries through GraphQL::executeQuery().
  • Resolver classes are resolved out of a phpnomad/diInstanceProvider, 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.

Requirements

  • PHP 8.2+
  • phpnomad/graphql
  • phpnomad/di
  • webonyx/graphql-php ^15.0

Usage

Bind WebonyxGraphQLStrategy to the GraphQLStrategy interface in your container, then register a TypeDefinition for each slice of your schema.

<?phpusePHPNomad\GraphQL\Interfaces\FieldResolver;
usePHPNomad\GraphQL\Interfaces\GraphQLStrategy;
usePHPNomad\GraphQL\Interfaces\ResolverContext;
usePHPNomad\GraphQL\Interfaces\TypeDefinition;
usePHPNomad\GraphQL\Webonyx\Strategies\WebonyxGraphQLStrategy;
$container->bind(GraphQLStrategy::class, WebonyxGraphQLStrategy::class);
class PostSchema implements TypeDefinition
{
publicfunctiongetSdl(): string
{
return<<<SDL type Post { id: ID! title: String! } extend type Query { posts: [Post!]! } SDL;
}
publicfunctiongetResolvers(): array
{
return [
'Query' => ['posts' => PostsQueryResolver::class],
];
}
}
class PostsQueryResolver implements FieldResolver
{
publicfunction__construct(privatereadonlyPostRepository$posts)
{
}
publicfunctionresolve(mixed$rootValue, array$args, ResolverContext$context): mixed
{
return$this->posts->all();
}
}
$strategy = $container->get(GraphQLStrategy::class);
$strategy->registerTypeDefinition(fn() => newPostSchema());
/** @var ResolverContext $context */$result = $strategy->execute('{ posts { id title } }', [], $context);

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.

Documentation

Framework docs live at phpnomad.com. For the underlying engine, see the webonyx/graphql-php repository and its schema and resolver reference.

License

MIT. See LICENSE.

About

Webonyx GraphQL engine integration for PHPNomad's GraphQL abstraction

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages