x-graphql / field-middleware
Adding custom logics before and after when resolving fields
Installs: 11 651
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: >=8.2
- webonyx/graphql-php: ^15.9
Requires (Dev)
- phpunit/phpunit: ^11.0
- symplify/easy-coding-standard: ^12.1
README
Adding custom logics before and after when resolving fields
Getting Started
Install this package via Composer
composer require x-graphql/field-middleware
Usages
Create your first middleware:
use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Schema; use XGraphQL\FieldMiddleware\MiddlewareInterface; class MyMiddleware implements MiddlewareInterface { public function resolve(mixed $value, array $arguments, mixed $context, ResolveInfo $info, callable $next) : mixed { $firstName = $next($value, $arguments, $context, $info); return $firstName . ' Doe'; } }
Then let apply this middleware to schema:
use GraphQL\GraphQL; use GraphQL\Type\Definition\ObjectType; use GraphQL\Type\Definition\Type; use GraphQL\Type\Schema; use XGraphQL\FieldMiddleware\FieldMiddleware; $schema = new Schema([ 'query' => new ObjectType([ 'name' => 'Query', 'fields' => [ 'name' => Type::string() ], ]), ]); FieldMiddleware::apply($schema, [new MyMiddleware()]); $result = GraphQL::executeQuery($schema, '{ name }', ['name' => 'John']); var_dump($result->toArray());
Credits
Created by Minh Vuong