diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 4933a6e71..836d5e70d 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -130,9 +130,7 @@ public function __construct(?array $row = null) public function offsetExists(mixed $offset): bool { assert(is_string($offset)); - // 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); + return array_key_exists($offset, static::$prop_types); } public function offsetGet(mixed $offset): mixed { @@ -140,9 +138,7 @@ public function offsetGet(mixed $offset): mixed if(!$this->offsetExists($offset)) { throw new \OutOfBoundsException("Undefined dynamic property: $offset"); } - // property lookup is a workaround for - // https://github.com/webonyx/graphql-php/pull/1531 - return $this->dynamic_props[$offset] ?? $this->$offset ?? null; + return $this->dynamic_props[$offset] ?? null; } public function offsetSet(mixed $offset, mixed $value): void { diff --git a/ext/graphql/main.php b/ext/graphql/main.php index 48de55870..5dd548e84 100644 --- a/ext/graphql/main.php +++ b/ext/graphql/main.php @@ -39,6 +39,37 @@ public static function update_post_metadata(int $post_id, MetadataInput $metadat } } +function shmFieldResolver( + mixed $objectValue, + mixed $args, + mixed $context, + \GraphQL\Type\Definition\ResolveInfo $info +): mixed { + $fieldName = $info->fieldName; + $property = null; + + if (is_array($objectValue)) { + if (isset($objectValue[$fieldName])) { + $property = $objectValue[$fieldName]; + } + } elseif ($objectValue instanceof \ArrayAccess) { + if (isset($objectValue->{$fieldName})) { + $property = $objectValue->{$fieldName}; + } elseif (isset($objectValue[$fieldName])) { + $property = $objectValue[$fieldName]; + } + + } elseif (is_object($objectValue)) { + if (isset($objectValue->{$fieldName})) { + $property = $objectValue->{$fieldName}; + } + } + + return $property instanceof \Closure + ? $property($objectValue, $args, $context, $info) + : $property; +} + class GraphQL extends Extension { public static function get_schema(): Schema @@ -89,6 +120,7 @@ public function onPageRequest(PageRequestEvent $event): void $t1 = ftime(); $server = new StandardServer([ 'schema' => $this->get_schema(), + 'fieldResolver' => "\Shimmie2\shmFieldResolver", ]); $t2 = ftime(); $resp = $server->executeRequest(); @@ -197,7 +229,7 @@ public function onCliGen(CliGenEvent $event): void $schema = $this->get_schema(); $t2 = ftime(); $debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS; - $body = GQL::executeQuery($schema, $query)->toArray($debug); + $body = GQL::executeQuery($schema, $query, fieldResolver: "\Shimmie2\shmFieldResolver")->toArray($debug); $t3 = ftime(); $body['stats'] = get_debug_info_arr(); $body['stats']['graphql_schema_time'] = round($t2 - $t1, 2); diff --git a/ext/graphql/test.php b/ext/graphql/test.php index d0555ff57..93ace1e0f 100644 --- a/ext/graphql/test.php +++ b/ext/graphql/test.php @@ -16,13 +16,23 @@ public function testSchema(): void $this->assertTrue(true); } + /** + * @return array + */ + protected function graphql(string $query): array + { + $schema = GraphQL::get_schema(); + $debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS; + return GQL::executeQuery($schema, $query, fieldResolver: "\Shimmie2\shmFieldResolver")->toArray($debug); + } + 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); + $image = Image::by_id($image_id); - $query = '{ + $result = $this->graphql('{ posts(limit: 3, offset: 0) { id post_id @@ -33,10 +43,7 @@ public function testQuery(): void name } } - }'; - $schema = GraphQL::get_schema(); - $debug = DebugFlag::INCLUDE_DEBUG_MESSAGE | DebugFlag::RETHROW_INTERNAL_EXCEPTIONS; - $result = GQL::executeQuery($schema, $query)->toArray($debug); + }'); $this->assertEquals([ 'data' => [