Skip to content

Commit

Permalink
BUGFIX: Support multiple variable formats
Browse files Browse the repository at this point in the history
Currently the only format the HTTP endpoint support for
the variables is a JSON-serialized string:

```
{
	"query": "<some-query>",
	"variables": "{\"id\": \"123\"}"
    // ...
}
```

With this change, unserialized JSON objects are supported, too:

```
```
{
	"query": "<some-query>",
	"variables": {
       "id": "123"
    }
    // ...
}
```

Note: I couldn't find anything about this in the GraphQL spec.
The GraphiQL IDE sends the variables serialized, but other endpoints
seem to support both formats.
  • Loading branch information
bwaidelich committed Sep 1, 2016
1 parent e843eb5 commit 2d6996e
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Classes/Controller/StandardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,23 @@ public function indexAction($endpoint)
/**
* @param string $endpoint The GraphQL endpoint, to allow for providing multiple APIs (this value is set from the routing usually)
* @param string $query The GraphQL query string (see GraphQL::execute())
* @param string $variables JSON-encoded list of variables (if any, see GraphQL::execute())
* @param array $variables list of variables (if any, see GraphQL::execute()). Note: The variables can be JSON-serialized to a string (like GraphiQL does) or a "real" array
* @param string $operationName The operation to execute (if multiple, see GraphQL::execute())
* @return string
* @Flow\SkipCsrfProtection
*/
public function queryAction($endpoint, $query, $variables = null, $operationName = null)
{
$this->verifySettings($endpoint);
$decodedVariables = json_decode($variables, true);
if ($variables !== null && is_string($this->request->getArgument('variables'))) {
$variables = json_decode($this->request->getArgument('variables'), true);
}

$querySchema = $this->typeResolver->get($this->settings['endpoints'][$endpoint]['querySchema']);
$mutationSchema = isset($this->settings['endpoints'][$endpoint]['mutationSchema']) ? $this->typeResolver->get($this->settings['endpoints'][$endpoint]['mutationSchema']) : null;
$subscriptionSchema = isset($this->settings['endpoints'][$endpoint]['subscriptionSchema']) ? $this->typeResolver->get($this->settings['endpoints'][$endpoint]['subscriptionSchema']) : null;
$schema = new Schema($querySchema, $mutationSchema, $subscriptionSchema);
$result = GraphQL::executeAndReturnResult($schema, $query, null, $decodedVariables, $operationName);
$result = GraphQL::executeAndReturnResult($schema, $query, null, $variables, $operationName);
$this->view->assign('result', $result);
}

Expand Down

0 comments on commit 2d6996e

Please sign in to comment.