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 @@ -4,6 +4,9 @@ CHANGELOG
[Next release](https://github.com/rebing/graphql-laravel/compare/6.2.0...master)
--------------

### Added
- Support for class based schemas [\#706 / jasonvarga](https://github.com/rebing/graphql-laravel/pull/706)

2021-03-12, 6.2.0
-----------------
### Fixed
Expand Down
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,39 @@ in addition to the global middleware. For example:
],
```

#### Schema classes

You may alternatively define the configuration of a schema in a class that implements `ConfigConvertible`.

In your config, you can reference the name of the class, rather than an array.

```php
'schemas' => [
'default' => DefaultSchema::class
]
```

```php
namespace App\GraphQL\Schemas;

use Rebing\GraphQL\Support\Contracts\ConfigConvertible;

class DefaultSchema implements ConfigConvertible
{
public function toConfig(): array
{
return [
'query' => [
ExampleQuery::class,
],
'mutation' => [
ExampleMutation::class,
]
]
}
}
```

### Creating a query

First you need to create a type. The Eloquent Model is only required, if specifying relations.
Expand Down
14 changes: 12 additions & 2 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rebing\GraphQL\Error\ValidationError;
use Rebing\GraphQL\Exception\SchemaNotFound;
use Rebing\GraphQL\Exception\TypeNotFound;
use Rebing\GraphQL\Support\Contracts\ConfigConvertible;
use Rebing\GraphQL\Support\Contracts\TypeConvertible;
use Rebing\GraphQL\Support\PaginationType;

Expand All @@ -31,7 +32,7 @@ class GraphQL
/** @var Container */
protected $app;

/** @var array<array|Schema> */
/** @var array<array|string|Schema> */
protected $schemas = [];

/**
Expand Down Expand Up @@ -507,6 +508,15 @@ protected function getSchemaConfiguration($schema)
throw new SchemaNotFound('Type '.$schemaName.' not found.');
}

return is_array($schema) ? $schema : $this->schemas[$schemaName];
$schema = is_array($schema) ? $schema : $this->schemas[$schemaName];

if (! is_string($schema)) {
return $schema;
}

/** @var ConfigConvertible $instance */
$instance = app()->make($schema);

return $instance->toConfig();
}
}
13 changes: 13 additions & 0 deletions src/Support/Contracts/ConfigConvertible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Support\Contracts;

interface ConfigConvertible
{
/**
* @return array<array>
*/
public function toConfig(): array;
}
22 changes: 22 additions & 0 deletions tests/Support/Objects/ExampleSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Rebing\GraphQL\Tests\Support\Objects;

use Rebing\GraphQL\Support\Contracts\ConfigConvertible;

class ExampleSchema implements ConfigConvertible
{
public function toConfig(): array
{
return [
'query' => [
'examplesCustom' => ExamplesQuery::class,
],
'mutation' => [
'updateExampleCustom' => UpdateExampleMutation::class,
],
];
}
}
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Rebing\GraphQL\Tests\Support\Objects\ExampleFilterInputType;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesAuthorizeMessageQuery;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesAuthorizeQuery;
use Rebing\GraphQL\Tests\Support\Objects\ExampleSchema;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesConfigAliasQuery;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesFilteredQuery;
use Rebing\GraphQL\Tests\Support\Objects\ExamplesMiddlewareQuery;
Expand Down Expand Up @@ -73,6 +74,9 @@ protected function getEnvironmentSetUp($app)
],
]);

$app['config']->set('graphql.schemas.class_based', ExampleSchema::class);
$app['config']->set('graphql.schemas.invalid_class_based', 'ThisClassDoesntExist');

$app['config']->set('graphql.types', [
'Example' => ExampleType::class,
'ExampleConfigAlias' => ExampleType2::class,
Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/GraphQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Support\Traits\Macroable;
use Rebing\GraphQL\Error\ValidationError;
use Rebing\GraphQL\Exception\SchemaNotFound;
Expand Down Expand Up @@ -69,6 +70,19 @@ public function testSchemaWithName(): void
$this->assertArrayHasKey('Example', $schema->getTypeMap());
}

/**
* Test schema with name referencing a class.
*/
public function testSchemaWithNameReferencingClass(): void
{
$schema = GraphQL::schema('class_based');

$this->assertGraphQLSchema($schema);
$this->assertGraphQLSchemaHasQuery($schema, 'examplesCustom');
$this->assertGraphQLSchemaHasMutation($schema, 'updateExampleCustom');
$this->assertArrayHasKey('Example', $schema->getTypeMap());
}

/**
* Test schema custom.
*/
Expand Down Expand Up @@ -101,6 +115,16 @@ public function testSchemaWithWrongName(): void
GraphQL::schema('wrong');
}

/**
* Test schema with invalid class name.
*/
public function testSchemaWithInvalidClassName(): void
{
$this->expectException(BindingResolutionException::class);
$this->expectExceptionMessage('Target class [ThisClassDoesntExist] does not exist.');
GraphQL::schema('invalid_class_based');
}

/**
* Test type.
*/
Expand Down