Skip to content
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.36.0

### Changed
Expand Down
27 changes: 17 additions & 10 deletions src/Error/Warning.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Comment on lines 59 to 63
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;
Comment on lines 86 to +90
// @phpstan-ignore-next-line necessary until we can use proper unions
} elseif (is_int($enable)) {
self::$enableWarnings |= $enable;
Expand All @@ -99,24 +99,31 @@ public static function enable($enable = true): void

public static function warnOnce(string $errorMessage, int $warningId, ?int $messageLevel = null): 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
{
$messageLevel ??= \E_USER_WARNING;

if (! self::shouldWarn($warningId)) {
return;
}
self::$warned[$warningId] = true;

if (self::$warningHandler !== null) {
(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);
Comment on lines +132 to 136
$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
Loading