Skip to content

Commit

Permalink
Allow custom model name in @canfind (#2515)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Feb 19, 2024
1 parent eed725d commit 5514697
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v6.33.3

### Fixed

- Allow custom model name in `@canFind` https://github.com/nuwave/lighthouse/pull/2515

## v6.33.2

### Fixed
Expand Down
12 changes: 9 additions & 3 deletions docs/6/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,11 @@ Deprecated. Use the [@can\* family of directives](#can-family-of-directives) ins

## @can\* family of directives

All @can\* directives have common arguments. These arguments specify how gates are checked and what to do if the user is not authorized.
All `@can*` directives have common arguments. These arguments specify how gates are checked and what to do if the user is not authorized.
Each directive has its own set of arguments that specify what to check against.

```graphql
"""
"""
The ability to check permissions for.
"""
ability: String!
Expand Down Expand Up @@ -728,6 +728,12 @@ directive @canFind(
"""
find: String!
"""
Specify the class name of the model to use.
This is only needed when the default model detection does not work.
"""
model: String
"""
Should the query fail when the models of `find` were not found?
"""
Expand All @@ -740,7 +746,7 @@ directive @canFind(
) repeatable on FIELD_DEFINITION
```
### canModel
### canRoot
```graphql
"""
Expand Down
12 changes: 9 additions & 3 deletions docs/master/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,11 +650,11 @@ Deprecated. Use the [@can\* family of directives](#can-family-of-directives) ins

## @can\* family of directives

All @can\* directives have common arguments. These arguments specify how gates are checked and what to do if the user is not authorized.
All `@can*` directives have common arguments. These arguments specify how gates are checked and what to do if the user is not authorized.
Each directive has its own set of arguments that specify what to check against.

```graphql
"""
"""
The ability to check permissions for.
"""
ability: String!
Expand Down Expand Up @@ -728,6 +728,12 @@ directive @canFind(
"""
find: String!
"""
Specify the class name of the model to use.
This is only needed when the default model detection does not work.
"""
model: String
"""
Should the query fail when the models of `find` were not found?
"""
Expand All @@ -740,7 +746,7 @@ directive @canFind(
) repeatable on FIELD_DEFINITION
```
### canModel
### canRoot
```graphql
"""
Expand Down
6 changes: 6 additions & 0 deletions src/Auth/CanFindDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public static function definition(): string
"""
find: String!
"""
Specify the class name of the model to use.
This is only needed when the default model detection does not work.
"""
model: String
"""
Should the query fail when the models of `find` were not found?
"""
Expand Down
38 changes: 38 additions & 0 deletions tests/Integration/Auth/CanFindDirectiveDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ public function testQueriesForSpecificModel(): void
]);
}

public function testCustomModelName(): void
{
$admin = new User();
$admin->name = UserPolicy::ADMIN;
$this->be($admin);

$user = factory(User::class)->create();
assert($user instanceof User);

$this->schema = /** @lang GraphQL */ '
type Query {
account(id: ID! @whereKey): Account
@canFind(ability: "view", find: "id", model: "User")
@first(model: "User")
}
type Account {
name: String!
}
';

$this->graphQL(/** @lang GraphQL */ '
query ($id: ID!) {
account(id: $id) {
name
}
}
', [
'id' => $user->getKey(),
])->assertJson([
'data' => [
'account' => [
'name' => $user->name,
],
],
]);
}

public function testFailsToFindSpecificModel(): void
{
$user = new User();
Expand Down

0 comments on commit 5514697

Please sign in to comment.