Skip to content
Draft
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Added

- If an object implements `\ArrayAccess`, check both array value and property https://github.com/webonyx/graphql-php/pull/1960

Comment on lines 10 to +15
## v15.37.1

### Fixed
Expand Down
8 changes: 7 additions & 1 deletion src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,16 @@ public static function suggestionList(string $input, array $options): array
*/
public static function extractKey($objectLikeValue, string $key)
{
if (is_array($objectLikeValue) || $objectLikeValue instanceof \ArrayAccess) {
if (is_array($objectLikeValue)) {
return $objectLikeValue[$key] ?? null;
}

if ($objectLikeValue instanceof \ArrayAccess) {
return $objectLikeValue[$key]
?? $objectLikeValue->{$key} // @phpstan-ignore-line Variable property access on ArrayAccess is fine here, we do the same for arbitrary objects
?? null;
}
Comment on lines +275 to +279

if (is_object($objectLikeValue)) {
return $objectLikeValue->{$key} ?? null;
}
Expand Down
76 changes: 5 additions & 71 deletions tests/Executor/ExecutorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,7 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo
'name' => 'ArrayAccess',
'fields' => [
'set' => Type::int(),
'setProperty' => Type::int(),
'unsetNull' => Type::int(),
'unsetThrow' => Type::int(),
],
Expand Down Expand Up @@ -1224,6 +1225,8 @@ public function testDefaultResolverGrabsValuesOffOfCommonPhpDataStructures(): vo
'arrayAccess' => [
'type' => $ArrayAccess,
'resolve' => static fn (): \ArrayAccess => new class implements \ArrayAccess {
public ?int $setProperty = 1;

/** @param mixed $offset */
#[\ReturnTypeWillChange]
public function offsetExists($offset): bool
Expand Down Expand Up @@ -1317,6 +1320,7 @@ public function __get(string $name): ?int
}
arrayAccess {
set
setProperty
unsetNull
unsetThrow
}
Expand Down Expand Up @@ -1344,6 +1348,7 @@ public function __get(string $name): ?int
],
'arrayAccess' => [
'set' => 1,
'setProperty' => 1,
'unsetNull' => null,
'unsetThrow' => null,
],
Expand All @@ -1362,75 +1367,4 @@ public function __get(string $name): ?int
$result->toArray()
);
}

public function testDefaultResolverDoesNotAccessPropertiesOfArrayAccess(): void
{
$schema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'arrayAccess' => [
'type' => new ObjectType([
'name' => 'ArrayAccess',
'fields' => [
'property' => Type::int(),
],
]),
// Eloquent models implement \ArrayAccess to expose their attributes.
// Their properties hold internal state that must stay hidden.
// https://github.com/webonyx/graphql-php/pull/1531
'resolve' => static fn (): \ArrayAccess => new class implements \ArrayAccess {
public ?int $property = 1;

/** @param mixed $offset */
#[\ReturnTypeWillChange]
public function offsetExists($offset): bool
{
return false;
}

/** @param mixed $offset */
#[\ReturnTypeWillChange]
public function offsetGet($offset): ?int
{
return null;
}

/**
* @param mixed $offset
* @param mixed $value
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value): void {}

/** @param mixed $offset */
#[\ReturnTypeWillChange]
public function offsetUnset($offset): void {}
},
],
],
]),
]);

$query = Parser::parse('
{
arrayAccess {
property
}
}
');

$result = Executor::execute($schema, $query);

self::assertSame(
[
'data' => [
'arrayAccess' => [
'property' => null,
],
],
],
$result->toArray()
);
}
}
Loading