1. Go to this page and download the library: Download x-graphql/field-middleware library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
x-graphql / field-middleware example snippets
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';
}
}
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());
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.