Skip to content
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

feat: extract method to fetch schema #40

Merged
merged 1 commit into from
Dec 10, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Support PHP 8
- In addition to `schemaPath()` it's now possible to override the `schema()` method. This can be useful if your GraphQL schema needs to be fetched from some other source than a file on disk. [#33](https://github.com/glesys/butler-graphql/pull/33)

### Changed
- **BREAKING**: Upgrade to webonyx/[email protected]. See https://github.com/webonyx/graphql-php/blob/v14.3.0/UPGRADE.md for breaking changes.
Expand Down
8 changes: 7 additions & 1 deletion src/Concerns/HandlesGraphqlRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ trait HandlesGraphqlRequests
public function __invoke(Request $request)
{
$loader = app(DataLoader::class);
$schema = BuildSchema::build(file_get_contents($this->schemaPath()), [$this, 'decorateTypeConfig']);

$schema = BuildSchema::build($this->schema(), [$this, 'decorateTypeConfig']);

/** @var \GraphQL\Executor\ExecutionResult */
$result = null;
Expand Down Expand Up @@ -95,6 +96,11 @@ public function reportException(Exception $exception)
app(ExceptionHandler::class)->report($exception);
}

public function schema()
{
return file_get_contents($this->schemaPath());
}

public function schemaPath()
{
return config('butler.graphql.schema');
Expand Down
17 changes: 17 additions & 0 deletions tests/HandlesGraphqlRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,4 +607,21 @@ public function test_nested_collections()
$data
);
}

public function test_custom_schema()
{
$controller = $this->app->make(GraphqlControllerWithCustomSchema::class);
$data = $controller(Request::create('/', 'POST', [
'query' => 'query { foo }'
]));

$this->assertSame(
[
'data' => [
'foo' => 'bar',
],
],
$data
);
}
}
15 changes: 15 additions & 0 deletions tests/stubs/GraphqlControllerWithCustomSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Butler\Graphql\Tests;

use Butler\Graphql\Concerns\HandlesGraphqlRequests;

class GraphqlControllerWithCustomSchema
{
use HandlesGraphqlRequests;

public function schema()
{
return 'type Query { foo: String! }';
}
}
11 changes: 11 additions & 0 deletions tests/stubs/Queries/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Butler\Graphql\Tests\Queries;

class Foo
{
public function __invoke($root, $args, $context)
{
return 'bar';
}
}