[graphql] fix graphql field access, fixes #1089

This commit is contained in:
Shish 2024-02-24 22:05:12 +00:00 committed by Shish
parent 2f4d8572b7
commit 2be141327a
3 changed files with 53 additions and 12 deletions

View file

@ -130,7 +130,9 @@ class Image implements \ArrayAccess
public function offsetExists(mixed $offset): bool public function offsetExists(mixed $offset): bool
{ {
assert(is_string($offset)); assert(is_string($offset));
return array_key_exists($offset, static::$prop_types); // property_exists is a workaround for
// https://github.com/webonyx/graphql-php/pull/1531
return array_key_exists($offset, static::$prop_types) || property_exists($this, $offset);
} }
public function offsetGet(mixed $offset): mixed public function offsetGet(mixed $offset): mixed
{ {
@ -139,7 +141,9 @@ class Image implements \ArrayAccess
$known = implode(", ", array_keys(static::$prop_types)); $known = implode(", ", array_keys(static::$prop_types));
throw new \OutOfBoundsException("Undefined dynamic property: $offset (Known: $known)"); throw new \OutOfBoundsException("Undefined dynamic property: $offset (Known: $known)");
} }
return $this->dynamic_props[$offset] ?? null; // property lookup is a workaround for
// https://github.com/webonyx/graphql-php/pull/1531
return $this->dynamic_props[$offset] ?? $this->$offset ?? null;
} }
public function offsetSet(mixed $offset, mixed $value): void public function offsetSet(mixed $offset, mixed $value): void
{ {
@ -464,15 +468,6 @@ class Image implements \ArrayAccess
return warehouse_path(self::THUMBNAIL_DIR, $this->hash); return warehouse_path(self::THUMBNAIL_DIR, $this->hash);
} }
/**
* Get the original filename.
*/
#[Field(name: "filename")]
public function get_filename(): string
{
return $this->filename;
}
/** /**
* Get the image's extension. * Get the image's extension.
*/ */

View file

@ -4,6 +4,9 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
use GraphQL\Error\DebugFlag;
use GraphQL\GraphQL as GQL;
class GraphQLTest extends ShimmiePHPUnitTestCase class GraphQLTest extends ShimmiePHPUnitTestCase
{ {
public function testSchema(): void public function testSchema(): void
@ -12,4 +15,47 @@ class GraphQLTest extends ShimmiePHPUnitTestCase
$schema->assertValid(); $schema->assertValid();
$this->assertTrue(true); $this->assertTrue(true);
} }
public function testQuery(): void
{
$this->log_in_as_user();
$image_id = $this->post_image("tests/pbx_screenshot.jpg", "test");
$image = Image::by_id_ex($image_id);
$query = '{
posts(limit: 3, offset: 0) {
id
post_id
tags
width
owner {
id
name
}
}
}';
$schema = GraphQL::get_schema();
$debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS;
$result = GQL::executeQuery($schema, $query)->toArray($debug);
$this->assertEquals([
'data' => [
'posts' => [
[
'id' => "post:$image_id",
'post_id' => $image_id,
'tags' => [
'test',
],
'width' => 640,
'owner' => [
'id' => 'user:'.$image->get_owner()->id,
'name' => self::$user_name,
],
],
]
,
],
], $result, var_export($result, true));
}
} }

View file

@ -203,7 +203,7 @@ class ImageIO extends Extension
public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void
{ {
$fname = $event->image->get_filename(); $fname = $event->image->filename;
$base_fname = basename($fname, '.' . $event->image->get_ext()); $base_fname = basename($fname, '.' . $event->image->get_ext());
$event->replace('$id', (string)$event->image->id); $event->replace('$id', (string)$event->image->id);