Skip to content

Commit

Permalink
[graphql] custom field resolver, fixes #1089
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Feb 25, 2024
1 parent 5ad5b92 commit 3a393c2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
8 changes: 2 additions & 6 deletions core/imageboard/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,15 @@ 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
{
assert(is_string($offset));
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
{
Expand Down
34 changes: 33 additions & 1 deletion ext/graphql/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 13 additions & 6 deletions ext/graphql/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ public function testSchema(): void
$this->assertTrue(true);
}

/**
* @return array<string, mixed>
*/
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
Expand All @@ -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' => [
Expand Down

0 comments on commit 3a393c2

Please sign in to comment.