Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect Warning settings before calling custom handler #1393

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

### Changed

- Respect settings in `GraphQL\Error\Warning` before calling custom `$warningHandler`

## v15.8.1

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1771,7 +1771,7 @@ const ALL = 63;
*
* @api
*/
static function setWarningHandler(?callable $warningHandler = null): void
static function setWarningHandler(?callable $warningHandler): void
```

```php
Expand Down
35 changes: 20 additions & 15 deletions src/Error/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ final class Warning
*
* @api
*/
public static function setWarningHandler(callable $warningHandler = null): void
public static function setWarningHandler(?callable $warningHandler): void
{
self::$warningHandler = $warningHandler;
}
Expand All @@ -59,7 +59,7 @@ public static function setWarningHandler(callable $warningHandler = null): void
public static function suppress($suppress = true): void
{
if ($suppress === true) {
self::$enableWarnings = 0;
self::$enableWarnings = self::NONE;
} elseif ($suppress === false) {
self::$enableWarnings = self::ALL;
// @phpstan-ignore-next-line necessary until we can use proper unions
Expand Down Expand Up @@ -87,7 +87,7 @@ public static function enable($enable = true): void
if ($enable === true) {
self::$enableWarnings = self::ALL;
} elseif ($enable === false) {
self::$enableWarnings = 0;
self::$enableWarnings = self::NONE;
// @phpstan-ignore-next-line necessary until we can use proper unions
} elseif (\is_int($enable)) {
self::$enableWarnings |= $enable;
Expand All @@ -97,26 +97,31 @@ public static function enable($enable = true): void
}
}

public static function warnOnce(string $errorMessage, int $warningId, int $messageLevel = null): void
public static function warnOnce(string $errorMessage, int $warningId, int $messageLevel = \E_USER_WARNING): void
{
$messageLevel ??= \E_USER_WARNING;

if (self::$warningHandler !== null) {
(self::$warningHandler)($errorMessage, $warningId, $messageLevel);
} elseif ((self::$enableWarnings & $warningId) > 0 && ! isset(self::$warned[$warningId])) {
self::$warned[$warningId] = true;
\trigger_error($errorMessage, $messageLevel);
if (isset(self::$warned[$warningId])) {
return;
}

self::warn($errorMessage, $warningId, $messageLevel);
}

public static function warn(string $errorMessage, int $warningId, int $messageLevel = null): void
public static function warn(string $errorMessage, int $warningId, int $messageLevel = \E_USER_WARNING): void
{
$messageLevel ??= \E_USER_WARNING;
if (! self::shouldWarn($warningId)) {
return;
}
self::$warned[$warningId] = true;

if (self::$warningHandler !== null) {
if (isset(self::$warningHandler)) {
(self::$warningHandler)($errorMessage, $warningId, $messageLevel);
} elseif ((self::$enableWarnings & $warningId) > 0) {
} else {
\trigger_error($errorMessage, $messageLevel);
}
}

private static function shouldWarn(int $warningId): bool
{
return (self::$enableWarnings & $warningId) > 0;
}
}
14 changes: 8 additions & 6 deletions tests/Executor/ExecutorLazySchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,18 @@ public function testWarnsAboutSlowIsTypeOfForLazySchema(): void
$result = Executor::execute($schema, Parser::parse($query));
self::assertEquals($expected, $result);

$warnings = [];
Warning::setWarningHandler(function ($warning) use (&$warnings): void {
$warnings[] = $warning;
});
Warning::enable(Warning::WARNING_FULL_SCHEMA_SCAN);
$result = Executor::execute($schema, Parser::parse($query));
self::markTestIncomplete('No longer works with PHPUnit 10, reintroduce with https://github.com/webonyx/graphql-php/pull/1393');
self::assertCount(1, $result->errors);
$error = $result->errors[0] ?? null;
self::assertInstanceOf(Error::class, $error);
self::assertSame(
self::assertEquals($expected, $result);

self::assertSame([
'GraphQL Interface Type `Pet` returned `null` from its `resolveType` function for value: instance of GraphQL\Tests\Executor\TestClasses\Dog. Switching to slow resolution method using `isTypeOf` of all possible implementations. It requires full schema scan and degrades query performance significantly. Make sure your `resolveType` function always returns a valid implementation or throws.',
$error->getMessage()
);
], $warnings);
}

public function testHintsOnConflictingTypeInstancesInDefinitions(): void
Expand Down