Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Middlewares/SecurityFieldMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ public function process(
$injectSource = $queryFieldDescriptor->isInjectSource();

$queryFieldDescriptor = $queryFieldDescriptor->withResolver(function (object|null $source, ...$args) use ($originalResolver, $securityAnnotations, $resolver, $failWith, $parameters, $queryFieldDescriptor, $injectSource) {
$variables = $this->getVariables($args, $parameters, $originalResolver->executionSource($source), $injectSource);
$variables = $this->getVariables(
$args,
$parameters,
$injectSource ? $source : $originalResolver->executionSource($source),
$injectSource,
);

foreach ($securityAnnotations as $annotation) {
try {
Expand Down
7 changes: 6 additions & 1 deletion src/Middlewares/SecurityInputFieldMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public function process(InputFieldDescriptor $inputFieldDescriptor, InputFieldHa
$injectSource = $inputFieldDescriptor->isInjectSource();

$inputFieldDescriptor = $inputFieldDescriptor->withResolver(function (object|null $source, ...$args) use ($originalResolver, $securityAnnotations, $resolver, $parameters, $inputFieldDescriptor, $injectSource) {
$variables = $this->getVariables($args, $parameters, $originalResolver->executionSource($source), $injectSource);
$variables = $this->getVariables(
$args,
$parameters,
$injectSource ? $source : $originalResolver->executionSource($source),
$injectSource,
);

foreach ($securityAnnotations as $annotation) {
try {
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/Integration/Types/ExtendedContactType.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ public function extendedSecretName(Contact $contact): string|null
{
return $contact->getName();
}

/**
* Regression: in #[Security] on an #[ExtendType] field, `this` must be
* the source object, not the resolver instance.
*/
#[Field]
#[Security("user && this.getName() == 'Joe'", failWith: null)]
public function sourceAwareSecretName(Contact $contact): string|null
{
return $contact->getName();
}
}
3 changes: 3 additions & 0 deletions tests/Integration/EndToEndTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@ public function testEndToEndSecurityOnExtendTypeField(): void
contacts {
name
extendedSecretName
sourceAwareSecretName
}
}
';
Expand All @@ -1368,6 +1369,7 @@ public function testEndToEndSecurityOnExtendTypeField(): void
$data = $this->getSuccessResult($result);
$this->assertSame('Joe', $data['contacts'][0]['name']);
$this->assertNull($data['contacts'][0]['extendedSecretName']);
$this->assertNull($data['contacts'][0]['sourceAwareSecretName']);

// Logged-in user with bar=42 → the Security expression passes and the field returns the name.
$container = $this->createContainer([
Expand All @@ -1394,6 +1396,7 @@ public function getUser(): object|null
$result = GraphQL::executeQuery($schema, $queryString);
$data = $this->getSuccessResult($result);
$this->assertSame('Joe', $data['contacts'][0]['extendedSecretName']);
$this->assertSame('Joe', $data['contacts'][0]['sourceAwareSecretName']);
}

public function testEndToEndUnions(): void
Expand Down
Loading