Skip to content

Commit 000a558

Browse files
committed
add support for multiple operations and operationName in request data
1 parent 510c37d commit 000a558

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Execution/Executor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public function execute(Schema $schema, array $document, $rootValue = null, $con
5353
$typeResolver
5454
);
5555

56+
// return early errors if execution context failed.
57+
if(is_array($executionContext)){
58+
return [
59+
"errors" => Errors::prettyPrintErrors($executionContext),
60+
"data" => []
61+
];
62+
}
63+
5664
$data = $this->executeOperation($executionContext, $executionContext->getOperation(), $rootValue);
5765
return $this->buildResponse($executionContext, $data);
5866
}

src/Servers/Server.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function listen()
6565
{
6666
// obtain query and variables
6767
$variables = $this->getVariables();
68+
$operationName = $this->getOperationName();
6869
$query = $this->getQuery();
6970

7071
if ($query === null) {
@@ -103,7 +104,7 @@ public function listen()
103104

104105

105106
// execute query
106-
$result = $this->executor->execute($this->schema, $this->parser->getParsedDocument(), null, null, $variables);
107+
$result = $this->executor->execute($this->schema, $this->parser->getParsedDocument(), null, null, $variables, $operationName);
107108
$this->returnData($result);
108109

109110
} catch (Error $error) {
@@ -151,6 +152,25 @@ private function getQuery(): ?string
151152
}
152153
}
153154

155+
/**
156+
* Returns the operation name string from the raw post data.
157+
*
158+
* @return string|null
159+
*/
160+
private function getOperationName(): ?string
161+
{
162+
// check if query is sent as raw http body in request as "application/json" or via post fields as "multipart/form-data"
163+
$headers = apache_request_headers();
164+
if (array_key_exists("Content-Type", $headers) and $headers["Content-Type"] === "application/json") {
165+
// raw json string in http body
166+
$phpInput = json_decode(file_get_contents("php://input"), true);
167+
return $phpInput["operationName"] ?? null;
168+
} else {
169+
// query sent via post field
170+
return $_POST["operationName"] ?? null;
171+
}
172+
}
173+
154174
/**
155175
* Returns the variables, sent by raw post data.
156176
*

0 commit comments

Comments
 (0)