Skip to content

Commit

Permalink
change name to input argument x injectArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Torres committed Nov 12, 2019
1 parent a3b9eb2 commit 3ba59d4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions docs/master/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -531,11 +531,11 @@ type Mutation {
}
```

You can pass the input variables as arguments to the policy setting `input` argument
You can pass the input variables as arguments to the policy setting `injectArgs` argument
```graphql
type Mutation {
createPost(input: PostInput): Post
@can(ability: "create", input: "true")
@can(ability: "create", injectArgs: "true")
}
```
Now you will have access to `PostInput` values in the policy
Expand Down
9 changes: 5 additions & 4 deletions src/Schema/Directives/CanDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public static function definition(): string
Additional arguments that are passed to `Gate::check`.
"""
args: [String!]
"""
"""
Send input data as arguments to the policy.
Set false by default
"""
input: Boolean!
injectArgs: Boolean!
) on FIELD_DEFINITION
SDL;
}
Expand Down Expand Up @@ -158,8 +158,9 @@ protected function authorize($user, $model): void
protected function getAdditionalArguments(): array
{
$directiveArgs = (array) $this->directiveArgValue('args');
$inputArgs = $this->directiveArgValue('input') === true ? [$this->args] : [];

$inputArgs = $this->directiveArgValue('injectArgs') === true
? [$this->args]
: [];
return array_merge($directiveArgs, $inputArgs);
}
}
36 changes: 16 additions & 20 deletions tests/Unit/Schema/Directives/CanDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ public function testSendInputArgumentToPolicy(): void
$this->be(new User);
$this->schema = '
type Query {
users(key1: String, key2: String, key3: String): [User]!
@can(ability:"severalArgs", input: true)
user(foo: String): User!
@can(ability:"severalArgs", injectArgs: true)
@field(resolver: "'.$this->qualifyTestResolver('resolveUser').'")
}
Expand All @@ -171,24 +171,20 @@ public function testSendInputArgumentToPolicy(): void
}
';

$variables = [
'key1' => 'foo',
'key2' => 'foo2',
'key3' => 'foo3',
];

$this->postGraphQL(
[
'operationName' => 'Users',
'query' => 'query Users($key1: String, $key2: String, $key3: String) {
users(key1: $key1, key2: $key2, key3: $key3) {
name
}
}
',
'variables' => $variables,
]
)->assertOk();

$this->graphQL('
{
user(foo: "bar"){
name
}
}
')->assertJson([
'data' => [
'user' => [
'name' => 'foo',
],
],
]);
}

public function resolveUser(): User
Expand Down
2 changes: 1 addition & 1 deletion tests/Utils/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ public function dependingOnArg($viewer, bool $pass): bool

public function severalArgs($user, array $input): bool
{
return $input['key1'] === 'foo' && $input['key2'] === 'foo2' && $input['key2'] === 'foo3';
return $input['foo'] === 'bar';
}
}

0 comments on commit 3ba59d4

Please sign in to comment.