1. Go to this page and download the library: Download x-graphql/field-guard 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-guard example snippets
use GraphQL\Type\Definition\ResolveInfo;
use XGraphQL\FieldGuard\RuleInterface;
$isAdminRule = new class implements RuleInterface {
public function allows(mixed $value, array $args, mixed $context, ResolveInfo $info) : bool{
return $context->isAdmin();
}
public function shouldRemember(mixed $value,array $args,mixed $context,ResolveInfo $info) : bool{
return true;
}
};
$permissions = [
'Query' => [
'getUser' => true, /// all user can get user.
'getBook' => false, /// deny all user to get book.
],
'Mutation' => [
'createUser' => $isAdminRule, /// only admin user can create user.
]
];
use XGraphQL\FieldMiddleware\FieldMiddleware;
use XGraphQL\FieldGuard\FieldGuardMiddleware;
$schema = ...
$guardMiddleware = new FieldGuardMiddleware($permissions);
FieldMiddleware::apply($schema, [$guardMiddleware]);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.