Skip to content

fix: allow query to be passed as instance #1178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
$type = $this->getType($name, $fresh);

foreach ($modifiers as $modifier) {
$type = Type::$modifier($type);

Check failure on line 259 in src/GraphQL.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Parameter #1 $type of static method GraphQL\Type\Definition\Type::nonNull() expects (callable(): (GraphQL\Type\Definition\NullableType&GraphQL\Type\Definition\Type))|(GraphQL\Type\Definition\NullableType&GraphQL\Type\Definition\Type), GraphQL\Type\Definition\Type given.
}

return $type;
Expand Down Expand Up @@ -352,7 +352,7 @@
}

/**
* @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 @@ -364,6 +364,8 @@
$field = $this->app->make($field);
/** @var Field $field */
$field = $field->toArray();
} elseif ($field instanceof Field) {
$field = $field->toArray();
}
$name = is_numeric($name) ? $field['name'] : $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);
}
}
Loading