Skip to content
Closed
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
10 changes: 0 additions & 10 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v15.29.0

### Changed

- Optimize `Deferred` execution https://github.com/webonyx/graphql-php/pull/1805

### Deprecated

- Deprecate `GraphQL\Deferred::create()` in favor of constructor https://github.com/webonyx/graphql-php/pull/1805

## v15.28.0

### Changed
Expand Down
80 changes: 0 additions & 80 deletions benchmarks/DeferredBench.php

This file was deleted.

7 changes: 3 additions & 4 deletions benchmarks/HugeSchemaBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
*
* @OutputTimeUnit("milliseconds", precision=3)
*
* @Warmup(2)
* @Warmup(1)
*
* @Revs(10)
* @Revs(5)
*
* @Iterations(3)
* @Iterations(1)
*/
class HugeSchemaBench
{
Expand Down Expand Up @@ -50,7 +50,6 @@ public function benchSchema(): void
->getTypeMap();
}

/** @Revs(1000) */
public function benchSchemaLazy(): void
{
$this->createLazySchema();
Expand Down
6 changes: 3 additions & 3 deletions benchmarks/StarWarsBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
*
* @OutputTimeUnit("milliseconds", precision=3)
*
* @Warmup(5)
* @Warmup(2)
*
* @Revs(100)
* @Revs(10)
*
* @Iterations(10)
* @Iterations(2)
*/
class StarWarsBench
{
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
},
"scripts": {
"baseline": "phpstan --generate-baseline",
"bench": "php -d zend.assertions=-1 vendor/bin/phpbench run --report=default",
"bench": "phpbench run",
"check": [
"@fix",
"@stan",
Expand All @@ -78,6 +78,6 @@
"php-cs-fixer": "make php-cs-fixer",
"rector": "rector process",
"stan": "phpstan --verbose",
"test": "php -d zend.exception_ignore_args=0 -d zend.assertions=1 -d assert.active=1 -d assert.exception=1 vendor/bin/phpunit"
"test": "php -d zend.exception_ignore_args=Off -d zend.assertions=On -d assert.active=On -d assert.exception=On vendor/bin/phpunit"
}
}
19 changes: 0 additions & 19 deletions docs/class-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1778,25 +1778,6 @@ function createRejected(Throwable $reason): GraphQL\Executor\Promise\Promise
function all(iterable $promisesOrValues): GraphQL\Executor\Promise\Promise
```

## GraphQL\Deferred

User-facing promise class for deferred field resolution.

@phpstan-type Executor callable(): mixed

### GraphQL\Deferred Methods

```php
/**
* Create a new Deferred promise and enqueue its execution.
*
* @api
*
* @param Executor $executor
*/
function __construct(callable $executor)
```

## GraphQL\Validator\DocumentValidator

Implements the "Validation" section of the spec.
Expand Down
1 change: 0 additions & 1 deletion generate-class-reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
GraphQL\Executor\ScopedContext::class => [],
GraphQL\Executor\ExecutionResult::class => [],
GraphQL\Executor\Promise\PromiseAdapter::class => [],
GraphQL\Deferred::class => [],
GraphQL\Validator\DocumentValidator::class => [],
GraphQL\Error\Error::class => ['constants' => true],
GraphQL\Error\Warning::class => ['constants' => true],
Expand Down
8 changes: 1 addition & 7 deletions phpbench.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,5 @@
"runner.path": "benchmarks",
"runner.file_pattern": "*Bench.php",
"runner.retry_threshold": 5,
"runner.time_unit": "milliseconds",
"runner.progress": "plain",
"report.generators": {
"default": {
"extends": "aggregate"
}
}
"runner.time_unit": "milliseconds"
}
48 changes: 7 additions & 41 deletions src/Deferred.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,21 @@
namespace GraphQL;

use GraphQL\Executor\Promise\Adapter\SyncPromise;
use GraphQL\Executor\Promise\Adapter\SyncPromiseQueue;

/**
* User-facing promise class for deferred field resolution.
*
* @phpstan-type Executor callable(): mixed
* @phpstan-import-type Executor from SyncPromise
*/
class Deferred extends SyncPromise
{
/**
* Executor for deferred promises.
*
* @var (callable(): mixed)|null
*/
protected $executor;

/**
* Create a new Deferred promise and enqueue its execution.
*
* @api
*
* @param Executor $executor
*/
public function __construct(callable $executor)
/** @param Executor $executor */
public static function create(callable $executor): self
{
$this->executor = $executor;

SyncPromiseQueue::enqueue(function (): void {
$executor = $this->executor;
assert($executor !== null, 'Always set in constructor, this callback runs only once.');
$this->executor = null;

try {
$this->resolve($executor());
} catch (\Throwable $e) {
$this->reject($e);
}
});
return new self($executor);
}

/**
* Alias for __construct.
*
* @param Executor $executor
*
* @deprecated TODO remove in next major version, use new Deferred() instead
*/
public static function create(callable $executor): self
/** @param Executor $executor */
public function __construct(callable $executor)
{
return new self($executor);
parent::__construct($executor);
}
}
7 changes: 4 additions & 3 deletions src/Executor/Promise/Adapter/AmpPromiseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function then(Promise $promise, ?callable $onFulfilled = null, ?callable
}
};

$ampPromise = $promise->adoptedPromise;
assert($ampPromise instanceof AmpPromise);
$ampPromise->onResolve($onResolve);
$adoptedPromise = $promise->adoptedPromise;
assert($adoptedPromise instanceof AmpPromise);

$adoptedPromise->onResolve($onResolve);

return new Promise($deferred->promise(), $this);
}
Expand Down
22 changes: 11 additions & 11 deletions src/Executor/Promise/Adapter/ReactPromiseAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,34 @@ public function convertThenable($thenable): Promise
/** @throws InvariantViolation */
public function then(Promise $promise, ?callable $onFulfilled = null, ?callable $onRejected = null): Promise
{
$reactPromise = $promise->adoptedPromise;
assert($reactPromise instanceof ReactPromiseInterface);
$adoptedPromise = $promise->adoptedPromise;
assert($adoptedPromise instanceof ReactPromiseInterface);

return new Promise($reactPromise->then($onFulfilled, $onRejected), $this);
return new Promise($adoptedPromise->then($onFulfilled, $onRejected), $this);
}

/** @throws InvariantViolation */
public function create(callable $resolver): Promise
{
$reactPromise = new ReactPromise($resolver);
$promise = new ReactPromise($resolver);

return new Promise($reactPromise, $this);
return new Promise($promise, $this);
}

/** @throws InvariantViolation */
public function createFulfilled($value = null): Promise
{
$reactPromise = resolve($value);
$promise = resolve($value);

return new Promise($reactPromise, $this);
return new Promise($promise, $this);
}

/** @throws InvariantViolation */
public function createRejected(\Throwable $reason): Promise
{
$reactPromise = reject($reason);
$promise = reject($reason);

return new Promise($reactPromise, $this);
return new Promise($promise, $this);
}

/** @throws InvariantViolation */
Expand All @@ -70,11 +70,11 @@ public function all(iterable $promisesOrValues): Promise
$promisesOrValuesArray = is_array($promisesOrValues)
? $promisesOrValues
: iterator_to_array($promisesOrValues);
$reactPromise = all($promisesOrValuesArray)->then(static fn (array $values): array => array_map(
$promise = all($promisesOrValuesArray)->then(static fn ($values): array => array_map(
static fn ($key) => $values[$key],
array_keys($promisesOrValuesArray),
));

return new Promise($reactPromise, $this);
return new Promise($promise, $this);
}
}
Loading