Skip to content

Commit

Permalink
fix: Change default value for BUTLER_GRAPHQL_NAMESPACE.
Browse files Browse the repository at this point in the history
The value of the `BUTLER_GRAPHQL_NAMESPACE` environment variable is used when resolving graphql queries, mutations and types.

The default value for this namespace is prefixed by `\\`. When this library resolves a query from the container it resolves object with the `\` prefix included. The problem arises when you try to bind or resolve something in the container using `ExampleQuery::class` as this does not use `\` as a prefix.

Example:
The namespace in our config is `\App\Http\Graphql` and we are trying to resolve the `Example` query. This library will resolve `\App\Http\Graphql\Example` in the container.
We need to inject a specific instance of a class to the constructor of the query so we declare the following:
```
$app->when(Example::class)->needs(Dep::class)->give(...)
```
`Example::class` will expand to `App\Http\Graphql\Example`. Because this doesn't match `\App\Http\Graphql\Example`, the binding won't work.

This commit changes the default namespace in the config to use the standard format in php.
  • Loading branch information
emil-nasso committed Jan 30, 2020
1 parent 785558c commit 44348b1
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed
- **BREAKING**: Removes leading slashes in the default namespace configuration to better support dependency injection with the laravel container. [#24](https://github.com/glesys/butler-graphql/pull/24)

## [2.2.0] - 2019-09-12

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ php artisan vendor:publish
### Change the Schema Location and Namespaces

- `BUTLER_GRAPHQL_SCHEMA` – Defaults to `app_path('Http/Graphql/schema.graphql')`.
- `BUTLER_GRAPHQL_NAMESPACE` – Defaults to `'\\App\\Http\\Graphql\\'`.
- `BUTLER_GRAPHQL_NAMESPACE` – Defaults to `'App\\Http\\Graphql\\'`.

### Debugging

Expand Down
2 changes: 1 addition & 1 deletion config/butler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
'include_debug_message' => env('BUTLER_GRAPHQL_INCLUDE_DEBUG_MESSAGE', false),
'include_trace' => env('BUTLER_GRAPHQL_INCLUDE_TRACE', false),

'namespace' => env('BUTLER_GRAPHQL_NAMESPACE', '\\App\\Http\\Graphql\\'),
'namespace' => env('BUTLER_GRAPHQL_NAMESPACE', 'App\\Http\\Graphql\\'),

'schema' => env('BUTLER_GRAPHQL_SCHEMA', base_path('app/Http/Graphql/schema.graphql')),

Expand Down
2 changes: 1 addition & 1 deletion tests/ServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_include_trace_config()
public function test_namespace_config()
{
$namespace = $this->app->config->get('butler.graphql.namespace');
$this->assertSame($namespace, '\\App\\Http\\Graphql\\');
$this->assertSame($namespace, 'App\\Http\\Graphql\\');
}

public function test_schema_config()
Expand Down

0 comments on commit 44348b1

Please sign in to comment.