Skip to content

Commit efe5089

Browse files
mathieutuwecc
authored andcommitted
feat: extract method to fetch schema
1 parent 9f5c65c commit efe5089

File tree

5 files changed

+51
-1
lines changed

5 files changed

+51
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111
- Support PHP 8
12+
- 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)
1213

1314
### Changed
1415
- **BREAKING**: Upgrade to webonyx/[email protected]. See https://github.com/webonyx/graphql-php/blob/v14.3.0/UPGRADE.md for breaking changes.

src/Concerns/HandlesGraphqlRequests.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ trait HandlesGraphqlRequests
3131
public function __invoke(Request $request)
3232
{
3333
$loader = app(DataLoader::class);
34-
$schema = BuildSchema::build(file_get_contents($this->schemaPath()), [$this, 'decorateTypeConfig']);
34+
35+
$schema = BuildSchema::build($this->schema(), [$this, 'decorateTypeConfig']);
3536

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

99+
public function schema()
100+
{
101+
return file_get_contents($this->schemaPath());
102+
}
103+
98104
public function schemaPath()
99105
{
100106
return config('butler.graphql.schema');

tests/HandlesGraphqlRequestsTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,4 +607,21 @@ public function test_nested_collections()
607607
$data
608608
);
609609
}
610+
611+
public function test_custom_schema()
612+
{
613+
$controller = $this->app->make(GraphqlControllerWithCustomSchema::class);
614+
$data = $controller(Request::create('/', 'POST', [
615+
'query' => 'query { foo }'
616+
]));
617+
618+
$this->assertSame(
619+
[
620+
'data' => [
621+
'foo' => 'bar',
622+
],
623+
],
624+
$data
625+
);
626+
}
610627
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Butler\Graphql\Tests;
4+
5+
use Butler\Graphql\Concerns\HandlesGraphqlRequests;
6+
7+
class GraphqlControllerWithCustomSchema
8+
{
9+
use HandlesGraphqlRequests;
10+
11+
public function schema()
12+
{
13+
return 'type Query { foo: String! }';
14+
}
15+
}

tests/stubs/Queries/Foo.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Butler\Graphql\Tests\Queries;
4+
5+
class Foo
6+
{
7+
public function __invoke($root, $args, $context)
8+
{
9+
return 'bar';
10+
}
11+
}

0 commit comments

Comments
 (0)