Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ CHANGELOG

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

## Added
- Allow field to be passed as instance [\#1178 / alancolant](https://github.com/rebing/graphql-laravel/pull/1178)

2025-11-05, 9.12.0
------------------

Expand Down
8 changes: 1 addition & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ parameters:
count: 1
path: src/GraphQL.php

-
rawMessage: 'Cannot access offset ''name'' on array<string, mixed>|Rebing\GraphQL\Support\Field.'
identifier: offsetAccess.nonOffsetAccessible
count: 2
path: src/GraphQL.php

-
rawMessage: Instanceof between Error and Error will always evaluate to true.
identifier: instanceof.alwaysTrue
Expand All @@ -25,7 +19,7 @@ parameters:
path: src/GraphQL.php

-
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.'
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.'
identifier: argument.type
count: 1
path: src/GraphQL.php
Expand Down
6 changes: 4 additions & 2 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ protected function buildObjectTypeFromClass($type, array $opts = []): Type
}

/**
* @param array<int|string,class-string|array<string,mixed>> $fields
* @param array<int|string,class-string|array<string,mixed>|Field> $fields
* @param array<string,string> $opts
*/
protected function buildObjectTypeFromFields(array $fields, array $opts = []): ObjectType
Expand All @@ -363,7 +363,9 @@ protected function buildObjectTypeFromFields(array $fields, array $opts = []): O
foreach ($fields as $name => $field) {
if (\is_string($field)) {
$field = $this->app->make($field);
/** @var Field $field */
}

if ($field instanceof Field) {
$field = $field->toArray();
}
$name = is_numeric($name) ? $field['name'] : $name;
Expand Down
37 changes: 37 additions & 0 deletions tests/Support/Objects/ExampleProseInstanceQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types = 1);
namespace Rebing\GraphQL\Tests\Support\Objects;

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;

class ExampleProseInstanceQuery extends Query
{
public function __construct(public string $prose)
{
}

protected $attributes = [
'name' => 'exampleProse',
];

public function type(): Type
{
return Type::string();
}

public function args(): array
{
return [];
}

/**
* @param mixed $root
* @param array<string,mixed> $args
*/
public function resolve($root, $args): string
{
return $this->prose;
}
}
12 changes: 12 additions & 0 deletions tests/Support/Objects/queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
}
',

'exampleProse' => '
query QueryExamplesProse {
exampleProse
}
',

'exampleProseRenamed' => '
query QueryExamplesProseRenamed {
loremIpsum
}
',

'examplesWithConfigAlias' => '
query examplesConfigAlias($index: Int) {
examplesConfigAlias(index: $index) {
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/GraphQLQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use GraphQL\Utils\SchemaPrinter;
use Rebing\GraphQL\Support\Facades\GraphQL;
use Rebing\GraphQL\Tests\Support\Objects\ExampleProseInstanceQuery;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesQuery;
use Rebing\GraphQL\Tests\TestCase;

Expand Down Expand Up @@ -229,4 +230,25 @@ public function testPrintSchema(): void

self::assertStringContainsString($queryFragment, $gql);
}

public function testQueryCanBeSetAsInstance(): void
{
$schema = GraphQL::buildSchemaFromConfig([
'query' => [
new ExampleProseInstanceQuery('A simple prose'),
'loremIpsum' => new ExampleProseInstanceQuery('Lorem ipsum dolor sit amet'),
],
]);
GraphQL::addSchema('default', $schema);

$result = GraphQL::queryAndReturnResult($this->queries['exampleProse']);
$expectedDataResult = ['exampleProse' => 'A simple prose'];
self::assertSame($expectedDataResult, $result->data);
self::assertCount(0, $result->errors);

$result = GraphQL::queryAndReturnResult($this->queries['exampleProseRenamed']);
$expectedDataResult = ['loremIpsum' => 'Lorem ipsum dolor sit amet'];
self::assertSame($expectedDataResult, $result->data);
self::assertCount(0, $result->errors);
}
}