From 2d61dfe9f475690d1a1a8081ea41e058043f07bb Mon Sep 17 00:00:00 2001 From: Mathieu TUDISCO Date: Fri, 13 Nov 2020 14:51:58 +0100 Subject: [PATCH 1/4] refacto: extract getSchema method. --- src/Concerns/HandlesGraphqlRequests.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Concerns/HandlesGraphqlRequests.php b/src/Concerns/HandlesGraphqlRequests.php index 3a177c7..7c898dd 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->getSchema(), [$this, 'decorateTypeConfig']); $result = null; GraphQL::useExperimentalExecutor(); @@ -56,6 +56,11 @@ public function __invoke(Request $request) return $this->decorateResponse($result->toArray($this->debugFlags())); } + + public function getSchema() + { + return file_get_contents($this->schemaPath()) + } public function errorFormatter(GraphqlError $graphqlError) { From ffb0acf81f1d61b578adf073877a88ea5dc97502 Mon Sep 17 00:00:00 2001 From: Mathieu TUDISCO Date: Fri, 13 Nov 2020 15:23:42 +0100 Subject: [PATCH 2/4] fix typo. --- src/Concerns/HandlesGraphqlRequests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/HandlesGraphqlRequests.php b/src/Concerns/HandlesGraphqlRequests.php index 7c898dd..22334aa 100644 --- a/src/Concerns/HandlesGraphqlRequests.php +++ b/src/Concerns/HandlesGraphqlRequests.php @@ -59,7 +59,7 @@ public function __invoke(Request $request) public function getSchema() { - return file_get_contents($this->schemaPath()) + return file_get_contents($this->schemaPath()); } public function errorFormatter(GraphqlError $graphqlError) From bcd9361f8054acc44ee034ae52bad5e643f8ceb3 Mon Sep 17 00:00:00 2001 From: Christoffer Andersson Date: Fri, 13 Nov 2020 20:31:15 +0100 Subject: [PATCH 3/4] Rename getSchema => schema, update CHANGELOG and add test --- CHANGELOG.md | 2 ++ src/Concerns/HandlesGraphqlRequests.php | 12 ++++++------ tests/HandlesGraphqlRequestsTest.php | 17 +++++++++++++++++ .../stubs/GraphqlControllerWithCustomSchema.php | 15 +++++++++++++++ tests/stubs/Queries/Foo.php | 11 +++++++++++ 5 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 tests/stubs/GraphqlControllerWithCustomSchema.php create mode 100644 tests/stubs/Queries/Foo.php 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/src/Concerns/HandlesGraphqlRequests.php b/src/Concerns/HandlesGraphqlRequests.php index 22334aa..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($this->getSchema(), [$this, 'decorateTypeConfig']); + $schema = BuildSchema::build($this->schema(), [$this, 'decorateTypeConfig']); $result = null; GraphQL::useExperimentalExecutor(); @@ -56,11 +56,6 @@ public function __invoke(Request $request) return $this->decorateResponse($result->toArray($this->debugFlags())); } - - public function getSchema() - { - return file_get_contents($this->schemaPath()); - } public function errorFormatter(GraphqlError $graphqlError) { @@ -100,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 @@ + Date: Fri, 13 Nov 2020 20:42:15 +0100 Subject: [PATCH 4/4] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) 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`.