diff --git a/CHANGELOG.md b/CHANGELOG.md index bed87e4..fdff093 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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/graphql-php@14.3.0. See https://github.com/webonyx/graphql-php/blob/v14.3.0/UPGRADE.md for breaking changes. diff --git a/src/Concerns/HandlesGraphqlRequests.php b/src/Concerns/HandlesGraphqlRequests.php index dff9516..27617ce 100644 --- a/src/Concerns/HandlesGraphqlRequests.php +++ b/src/Concerns/HandlesGraphqlRequests.php @@ -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; @@ -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'); diff --git a/tests/HandlesGraphqlRequestsTest.php b/tests/HandlesGraphqlRequestsTest.php index 22c5460..494bef7 100644 --- a/tests/HandlesGraphqlRequestsTest.php +++ b/tests/HandlesGraphqlRequestsTest.php @@ -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 + ); + } } diff --git a/tests/stubs/GraphqlControllerWithCustomSchema.php b/tests/stubs/GraphqlControllerWithCustomSchema.php new file mode 100644 index 0000000..247a0bd --- /dev/null +++ b/tests/stubs/GraphqlControllerWithCustomSchema.php @@ -0,0 +1,15 @@ +