diff --git a/src/GraphQL.php b/src/GraphQL.php index 3bf211e3..604b7ca9 100644 --- a/src/GraphQL.php +++ b/src/GraphQL.php @@ -352,7 +352,7 @@ protected function buildObjectTypeFromClass($type, array $opts = []): Type } /** - * @param array> $fields + * @param array|Field> $fields * @param array $opts */ protected function buildObjectTypeFromFields(array $fields, array $opts = []): ObjectType @@ -364,6 +364,8 @@ protected function buildObjectTypeFromFields(array $fields, array $opts = []): O $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; diff --git a/tests/Support/Objects/ExampleProseInstanceQuery.php b/tests/Support/Objects/ExampleProseInstanceQuery.php new file mode 100644 index 00000000..55cf4159 --- /dev/null +++ b/tests/Support/Objects/ExampleProseInstanceQuery.php @@ -0,0 +1,37 @@ + 'exampleProse', + ]; + + public function type(): Type + { + return Type::string(); + } + + public function args(): array + { + return []; + } + + /** + * @param mixed $root + * @param array $args + */ + public function resolve($root, $args): string + { + return $this->prose; + } +} diff --git a/tests/Support/Objects/queries.php b/tests/Support/Objects/queries.php index 43c2f3c0..a0337d98 100644 --- a/tests/Support/Objects/queries.php +++ b/tests/Support/Objects/queries.php @@ -19,6 +19,18 @@ } ', + 'exampleProse' => ' + query QueryExamplesProse { + exampleProse + } + ', + + 'exampleProseRenamed' => ' + query QueryExamplesProseRenamed { + loremIpsum + } + ', + 'examplesWithConfigAlias' => ' query examplesConfigAlias($index: Int) { examplesConfigAlias(index: $index) { diff --git a/tests/Unit/GraphQLQueryTest.php b/tests/Unit/GraphQLQueryTest.php index 40efc975..3efa654b 100644 --- a/tests/Unit/GraphQLQueryTest.php +++ b/tests/Unit/GraphQLQueryTest.php @@ -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; @@ -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); + } }