diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb0db0..a3adc75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- 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) + ## [3.4.0] - 2020-10-06 diff --git a/README.md b/README.md index 9182bd0..dbdd9b8 100644 --- a/README.md +++ b/README.md @@ -298,6 +298,8 @@ php artisan vendor:publish - `BUTLER_GRAPHQL_SCHEMA` – Defaults to `app_path('Http/Graphql/schema.graphql')`. - `BUTLER_GRAPHQL_NAMESPACE` – Defaults to `'App\\Http\\Graphql\\'`. +*NOTE:* If you don't want to load the schema from file, you can override the `schema()` method of `HandlesGraphqlRequests` to return the content of the schema anyway you'd like. + ### Debugging - `BUTLER_GRAPHQL_INCLUDE_DEBUG_MESSAGE` – Set to `true` to include the real error message in error responses. Defaults to `false`. diff --git a/src/Concerns/HandlesGraphqlRequests.php b/src/Concerns/HandlesGraphqlRequests.php index 3a177c7..e2c586a 100644 --- a/src/Concerns/HandlesGraphqlRequests.php +++ b/src/Concerns/HandlesGraphqlRequests.php @@ -31,7 +31,7 @@ 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']); $result = null; GraphQL::useExperimentalExecutor(); @@ -95,6 +95,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 @@ +