Skip to content

Commit 12d1e42

Browse files
authored
Merge pull request #1178 from alancolant/resolve-query-instance
fix: allow query to be passed as instance
2 parents 9c40724 + 5763b83 commit 12d1e42

File tree

6 files changed

+79
-9
lines changed

6 files changed

+79
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ CHANGELOG
33

44
[Next release](https://github.com/rebing/graphql-laravel/compare/9.12.0...master)
55

6+
## Added
7+
- Allow field to be passed as instance [\#1178 / alancolant](https://github.com/rebing/graphql-laravel/pull/1178)
8+
69
2025-11-05, 9.12.0
710
------------------
811

phpstan-baseline.neon

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ parameters:
66
count: 1
77
path: src/GraphQL.php
88

9-
-
10-
rawMessage: 'Cannot access offset ''name'' on array<string, mixed>|Rebing\GraphQL\Support\Field.'
11-
identifier: offsetAccess.nonOffsetAccessible
12-
count: 2
13-
path: src/GraphQL.php
14-
159
-
1610
rawMessage: Instanceof between Error and Error will always evaluate to true.
1711
identifier: instanceof.alwaysTrue
@@ -25,7 +19,7 @@ parameters:
2519
path: src/GraphQL.php
2620

2721
-
28-
rawMessage: 'Parameter #1 $config of class GraphQL\Type\Definition\ObjectType constructor expects array{name?: string|null, description?: string|null, resolveField?: (callable(mixed, array<string, mixed>, mixed, GraphQL\Type\Definition\ResolveInfo): mixed)|null, argsMapper?: (callable(array<string, mixed>, GraphQL\Type\Definition\FieldDefinition, GraphQL\Language\AST\FieldNode, mixed): mixed)|null, fields: (callable(): iterable)|iterable, interfaces?: (callable(): iterable<callable(): GraphQL\Type\Definition\InterfaceType|GraphQL\Type\Definition\InterfaceType>)|iterable<(callable(): GraphQL\Type\Definition\InterfaceType)|GraphQL\Type\Definition\InterfaceType>, isTypeOf?: (callable(mixed, mixed, GraphQL\Type\Definition\ResolveInfo): (bool|GraphQL\Deferred|null))|null, astNode?: GraphQL\Language\AST\ObjectTypeDefinitionNode|null, ...}, non-empty-array<string, array<non-empty-array<string, mixed>|(ArrayAccess&Rebing\GraphQL\Support\Field)>|string> given.'
22+
rawMessage: 'Parameter #1 $config of class GraphQL\Type\Definition\ObjectType constructor expects array{name?: string|null, description?: string|null, resolveField?: (callable(mixed, array<string, mixed>, mixed, GraphQL\Type\Definition\ResolveInfo): mixed)|null, argsMapper?: (callable(array<string, mixed>, GraphQL\Type\Definition\FieldDefinition, GraphQL\Language\AST\FieldNode, mixed): mixed)|null, fields: (callable(): iterable)|iterable, interfaces?: (callable(): iterable<callable(): GraphQL\Type\Definition\InterfaceType|GraphQL\Type\Definition\InterfaceType>)|iterable<(callable(): GraphQL\Type\Definition\InterfaceType)|GraphQL\Type\Definition\InterfaceType>, isTypeOf?: (callable(mixed, mixed, GraphQL\Type\Definition\ResolveInfo): (bool|GraphQL\Deferred|null))|null, astNode?: GraphQL\Language\AST\ObjectTypeDefinitionNode|null, ...}, non-empty-array<string, array|string> given.'
2923
identifier: argument.type
3024
count: 1
3125
path: src/GraphQL.php

src/GraphQL.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ protected function buildObjectTypeFromClass($type, array $opts = []): Type
353353
}
354354

355355
/**
356-
* @param array<int|string,class-string|array<string,mixed>> $fields
356+
* @param array<int|string,class-string|array<string,mixed>|Field> $fields
357357
* @param array<string,string> $opts
358358
*/
359359
protected function buildObjectTypeFromFields(array $fields, array $opts = []): ObjectType
@@ -363,7 +363,9 @@ protected function buildObjectTypeFromFields(array $fields, array $opts = []): O
363363
foreach ($fields as $name => $field) {
364364
if (\is_string($field)) {
365365
$field = $this->app->make($field);
366-
/** @var Field $field */
366+
}
367+
368+
if ($field instanceof Field) {
367369
$field = $field->toArray();
368370
}
369371
$name = is_numeric($name) ? $field['name'] : $name;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace Rebing\GraphQL\Tests\Support\Objects;
5+
6+
use GraphQL\Type\Definition\Type;
7+
use Rebing\GraphQL\Support\Query;
8+
9+
class ExampleProseInstanceQuery extends Query
10+
{
11+
public function __construct(public string $prose)
12+
{
13+
}
14+
15+
protected $attributes = [
16+
'name' => 'exampleProse',
17+
];
18+
19+
public function type(): Type
20+
{
21+
return Type::string();
22+
}
23+
24+
public function args(): array
25+
{
26+
return [];
27+
}
28+
29+
/**
30+
* @param mixed $root
31+
* @param array<string,mixed> $args
32+
*/
33+
public function resolve($root, $args): string
34+
{
35+
return $this->prose;
36+
}
37+
}

tests/Support/Objects/queries.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
}
2020
',
2121

22+
'exampleProse' => '
23+
query QueryExamplesProse {
24+
exampleProse
25+
}
26+
',
27+
28+
'exampleProseRenamed' => '
29+
query QueryExamplesProseRenamed {
30+
loremIpsum
31+
}
32+
',
33+
2234
'examplesWithConfigAlias' => '
2335
query examplesConfigAlias($index: Int) {
2436
examplesConfigAlias(index: $index) {

tests/Unit/GraphQLQueryTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
use GraphQL\Utils\SchemaPrinter;
77
use Rebing\GraphQL\Support\Facades\GraphQL;
8+
use Rebing\GraphQL\Tests\Support\Objects\ExampleProseInstanceQuery;
89
use Rebing\GraphQL\Tests\Support\Objects\ExamplesQuery;
910
use Rebing\GraphQL\Tests\TestCase;
1011

@@ -229,4 +230,25 @@ public function testPrintSchema(): void
229230

230231
self::assertStringContainsString($queryFragment, $gql);
231232
}
233+
234+
public function testQueryCanBeSetAsInstance(): void
235+
{
236+
$schema = GraphQL::buildSchemaFromConfig([
237+
'query' => [
238+
new ExampleProseInstanceQuery('A simple prose'),
239+
'loremIpsum' => new ExampleProseInstanceQuery('Lorem ipsum dolor sit amet'),
240+
],
241+
]);
242+
GraphQL::addSchema('default', $schema);
243+
244+
$result = GraphQL::queryAndReturnResult($this->queries['exampleProse']);
245+
$expectedDataResult = ['exampleProse' => 'A simple prose'];
246+
self::assertSame($expectedDataResult, $result->data);
247+
self::assertCount(0, $result->errors);
248+
249+
$result = GraphQL::queryAndReturnResult($this->queries['exampleProseRenamed']);
250+
$expectedDataResult = ['loremIpsum' => 'Lorem ipsum dolor sit amet'];
251+
self::assertSame($expectedDataResult, $result->data);
252+
self::assertCount(0, $result->errors);
253+
}
232254
}

0 commit comments

Comments
 (0)