Skip to content

Commit

Permalink
docs: add README
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongxuongminh committed Mar 31, 2024
1 parent f36b4aa commit 0c3390c
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,62 @@
Field Guard
===========

[Middleware](https://github.com/x-graphql/field-middleware) for adding security layer to GraphQL schema

![unit tests](https://github.com/x-graphql/field-guard/actions/workflows/unit_tests.yml/badge.svg)
[![codecov](https://codecov.io/gh/x-graphql/field-guard/graph/badge.svg?token=a76EAc7BUy)](https://codecov.io/gh/x-graphql/field-guard)

Getting Started
---------------

Install this package via [Composer](https://getcomposer.org)

```shell
composer require x-graphql/field-guard
```

Usages
------

Create permissions array mapping object type name, and it fields with rule, rule can be
boolean or instance of `XGraphQL\FieldGuard\RuleInterface`:

```php
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.
]
];
```

Then create middleware with `$permissions` above and apply to schema:

```php
use XGraphQL\FieldMiddleware\FieldMiddleware;
use XGraphQL\FieldGuard\FieldGuardMiddleware;

$schema = ...
$guardMiddleware = new FieldGuardMiddleware($schema, $permissions);

FieldMiddleware::apply($schema, [$guardMiddleware]);
```

Credits
-------
Expand Down

0 comments on commit 0c3390c

Please sign in to comment.