Skip to content

Commit

Permalink
Add configuration option parse_source_location
Browse files Browse the repository at this point in the history
  • Loading branch information
valerio-pizzichini authored Jan 16, 2024
1 parent fe444a8 commit 3cf820e
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Added

- Add configuration option `parse_source_location` https://github.com/nuwave/lighthouse/pull/2499

## v6.29.1

### Fixed
Expand Down
11 changes: 9 additions & 2 deletions src/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function parse(string $query): DocumentNode
$cacheConfig = $this->configRepository->get('lighthouse.query_cache');

if (! $cacheConfig['enable']) {
return Parser::parse($query);
return $this->parseQuery($query);
}

$cacheFactory = Container::getInstance()->make(CacheFactory::class);
Expand All @@ -256,7 +256,7 @@ public function parse(string $query): DocumentNode
return $store->remember(
"lighthouse:query:{$sha256}",
$cacheConfig['ttl'],
static fn (): DocumentNode => Parser::parse($query),
fn (): DocumentNode => $this->parseQuery($query),
);
}

Expand Down Expand Up @@ -353,4 +353,11 @@ protected function cleanUpAfterExecution(): void
FieldValue::clear();
$this->errorPool->clear();
}

protected function parseQuery(string $query): DocumentNode
{
return Parser::parse($query, [
'noLocation' => ! $this->configRepository->get('lighthouse.parse_source_location'),
]);
}
}
2 changes: 1 addition & 1 deletion src/WhereConditions/WhereConditionsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function handleHasCondition(
$builder->getModel(),
);
}
: null,
: null,
$operator,
$amount,
)
Expand Down
13 changes: 13 additions & 0 deletions src/lighthouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@
'ttl' => env('LIGHTHOUSE_QUERY_CACHE_TTL', 24 * 60 * 60),
],

/*
|--------------------------------------------------------------------------
| Parse source location
|--------------------------------------------------------------------------
|
| Should the source location be included in the AST nodes resulting from query parsing?
| Setting this to `false` improves performance, but omits the key `locations` from errors,
| see https://spec.graphql.org/October2021/#sec-Errors.Error-result-format.
|
*/

'parse_source_location' => true,

/*
|--------------------------------------------------------------------------
| Namespaces
Expand Down
31 changes: 23 additions & 8 deletions tests/Integration/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public function testRejectsInvalidQuery(): void
);
}

public function testReturnsFullGraphQLError(): void
/** @dataProvider parseSourceLocations */
public function testReturnsFullGraphQLError(bool $parseSourceLocations): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.parse_source_location', $parseSourceLocations);

$message = 'some error';
$this->mockResolver(static fn (): Error => new Error($message));

Expand All @@ -59,7 +63,7 @@ public function testReturnsFullGraphQLError(): void
}
';

$this
$response = $this
->graphQL(/** @lang GraphQL */ <<<'GRAPHQL'
{
foo
Expand All @@ -74,15 +78,26 @@ public function testReturnsFullGraphQLError(): void
[
'message' => $message,
'path' => ['foo'],
'locations' => [
[
'line' => 2,
'column' => 5,
],
],
],
],
]);

$locationsJsonPath = 'errors.0.locations';
$parseSourceLocations
? $response->json($locationsJsonPath) === [
[
'line' => 2,
'column' => 5,
],
]
: $response->assertJsonMissingPath($locationsJsonPath);
}

/** @return iterable<array{bool}> */
public static function parseSourceLocations(): iterable
{
yield [true];
yield [false];
}

public function testIgnoresInvalidJSONVariables(): void
Expand Down
39 changes: 39 additions & 0 deletions tests/Integration/Validation/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,45 @@ public function testFullValidationError(): void
]);
}

public function testFullValidationErrorWithoutLocationParse(): void
{
$config = $this->app->make(ConfigRepository::class);
$config->set('lighthouse.parse_source_location', false);

$this->schema = /** @lang GraphQL */ '
type Query {
foo(
bar: String @rules(apply: ["required"])
): Int
}
';

$this
->graphQL(/** @lang GraphQL */ '
{
foo
}
')
->assertExactJson([
'errors' => [
[
'message' => 'Validation failed for the field [foo].',
'extensions' => [
ValidationException::KEY => [
'bar' => [
'The bar field is required.',
],
],
],
'path' => ['foo'],
],
],
'data' => [
'foo' => null,
],
]);
}

public function testRunsOnNonRootFields(): void
{
$this->schema = /** @lang GraphQL */ '
Expand Down

0 comments on commit 3cf820e

Please sign in to comment.