Skip to content

Commit 24ffd60

Browse files
committed
New method "Executor::promiseToExecute()" which always returns promise (even for SyncPromiseAdapter)
1 parent 0e2ac57 commit 24ffd60

File tree

1 file changed

+56
-25
lines changed

1 file changed

+56
-25
lines changed

src/Executor/Executor.php

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,49 @@ public static function execute(
112112
PromiseAdapter $promiseAdapter = null
113113
)
114114
{
115-
if (null !== $variableValues) {
116-
Utils::invariant(
117-
is_array($variableValues) || $variableValues instanceof \ArrayAccess,
118-
"Variable values are expected to be array or instance of ArrayAccess, got " . Utils::getVariableType($variableValues)
119-
);
120-
}
121-
if (null !== $operationName) {
122-
Utils::invariant(
123-
is_string($operationName),
124-
"Operation name is supposed to be string, got " . Utils::getVariableType($operationName)
125-
);
115+
$promiseAdapter = $promiseAdapter ?: self::getPromiseAdapter();
116+
117+
$result = self::promiseToExecute(
118+
$promiseAdapter,
119+
$schema,
120+
$ast,
121+
$rootValue,
122+
$contextValue,
123+
$variableValues,
124+
$operationName,
125+
$fieldResolver
126+
);
127+
128+
// Wait for promised results when using sync promises
129+
if ($promiseAdapter instanceof SyncPromiseAdapter) {
130+
$result = $promiseAdapter->wait($result);
126131
}
127132

128-
$promiseAdapter = $promiseAdapter ?: self::getPromiseAdapter();
133+
return $result;
134+
}
129135

136+
/**
137+
* @param PromiseAdapter $promiseAdapter
138+
* @param Schema $schema
139+
* @param DocumentNode $ast
140+
* @param null $rootValue
141+
* @param null $contextValue
142+
* @param null $variableValues
143+
* @param null $operationName
144+
* @param callable|null $fieldResolver
145+
* @return Promise
146+
*/
147+
public static function promiseToExecute(
148+
PromiseAdapter $promiseAdapter,
149+
Schema $schema,
150+
DocumentNode $ast,
151+
$rootValue = null,
152+
$contextValue = null,
153+
$variableValues = null,
154+
$operationName = null,
155+
callable $fieldResolver = null
156+
)
157+
{
130158
try {
131159
$exeContext = self::buildExecutionContext(
132160
$schema,
@@ -139,21 +167,11 @@ public static function execute(
139167
$promiseAdapter
140168
);
141169
} catch (Error $e) {
142-
if ($promiseAdapter instanceof SyncPromiseAdapter) {
143-
return new ExecutionResult(null, [$e]);
144-
} else {
145-
return $promiseAdapter->createFulfilled(new ExecutionResult(null, [$e]));
146-
}
147-
}
148-
149-
$executor = new self($exeContext, $promiseAdapter);
150-
$result = $executor->executeQuery();
151-
152-
if ($result instanceof Promise && $promiseAdapter instanceof SyncPromiseAdapter) {
153-
$result = $promiseAdapter->wait($result);
170+
return $promiseAdapter->createFulfilled(new ExecutionResult(null, [$e]));
154171
}
155172

156-
return $result;
173+
$executor = new self($exeContext);
174+
return $executor->executeQuery();
157175
}
158176

159177
/**
@@ -183,6 +201,19 @@ private static function buildExecutionContext(
183201
PromiseAdapter $promiseAdapter = null
184202
)
185203
{
204+
if (null !== $rawVariableValues) {
205+
Utils::invariant(
206+
is_array($rawVariableValues) || $rawVariableValues instanceof \ArrayAccess,
207+
"Variable values are expected to be array or instance of ArrayAccess, got " . Utils::getVariableType($rawVariableValues)
208+
);
209+
}
210+
if (null !== $operationName) {
211+
Utils::invariant(
212+
is_string($operationName),
213+
"Operation name is supposed to be string, got " . Utils::getVariableType($operationName)
214+
);
215+
}
216+
186217
$errors = [];
187218
$fragments = [];
188219
$operation = null;

0 commit comments

Comments
 (0)