diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 113b8ef4..34374055 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -130,7 +130,9 @@ class Image implements \ArrayAccess public function offsetExists(mixed $offset): bool { 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 { @@ -139,7 +141,9 @@ class Image implements \ArrayAccess $known = implode(", ", array_keys(static::$prop_types)); 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 { @@ -464,15 +468,6 @@ class Image implements \ArrayAccess 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. */ diff --git a/ext/graphql/test.php b/ext/graphql/test.php index 98146745..d0555ff5 100644 --- a/ext/graphql/test.php +++ b/ext/graphql/test.php @@ -4,6 +4,9 @@ declare(strict_types=1); namespace Shimmie2; +use GraphQL\Error\DebugFlag; +use GraphQL\GraphQL as GQL; + class GraphQLTest extends ShimmiePHPUnitTestCase { public function testSchema(): void @@ -12,4 +15,47 @@ class GraphQLTest extends ShimmiePHPUnitTestCase $schema->assertValid(); $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)); + } } diff --git a/ext/image/main.php b/ext/image/main.php index 21700213..6cc7b4d8 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -203,7 +203,7 @@ class ImageIO extends Extension public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void { - $fname = $event->image->get_filename(); + $fname = $event->image->filename; $base_fname = basename($fname, '.' . $event->image->get_ext()); $event->replace('$id', (string)$event->image->id);