Skip to content
Merged
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
13 changes: 1 addition & 12 deletions Command/GraphDumpSchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->execute($request)
->toArray();

if (isset($result['errors'])) {
foreach ($result['errors'] as $error) {
$output->error($error['message']);
}

return 1;
}

$file = $input->getOption('file');
if (empty($file)) {
$file = $container->getParameter('kernel.root_dir').'/../var/schema.json';
}
$file = $input->hasOption('file') ? $input->getOption('file') : $container->getParameter('kernel.root_dir').'/../var/schema.json';

$schema = json_encode($result['data']);

Expand Down
29 changes: 26 additions & 3 deletions Controller/GraphController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,32 @@ class GraphController extends Controller
{
public function endpointAction(Request $request)
{
$req = $this->get('overblog_graphql.request_parser')->parse($request);
$res = $this->get('overblog_graphql.request_executor')->execute($req, []);
if ($request->query->has('batch')) {
$data = $this->treatBatchQuery($request);
} else {
$data = $this->treatNormalQuery($request);
}

return new JsonResponse($res->toArray(), 200);
return new JsonResponse($data, 200);
}

private function treatBatchQuery(Request $request)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace treat by process?

{
$params = $this->get('overblog_graphql.request_batch_parser')->parse($request);
$data = [];

foreach ($params as $i => $entry) {
$data[$i] = $this->get('overblog_graphql.request_executor')->execute($entry)->toArray();
}

return $data;
}

private function treatNormalQuery(Request $request)
{
$params = $this->get('overblog_graphql.request_parser')->parse($request);
$data = $this->get('overblog_graphql.request_executor')->execute($params)->toArray();

return $data;
}
}
3 changes: 0 additions & 3 deletions Relay/Mutation/InputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public function __construct(array $config)
);

$name = str_replace('Input', '', $config['name']);
if (empty($name)) {
$name = $config['name'];
}

parent::__construct([
'name' => $name.'Input',
Expand Down
3 changes: 0 additions & 3 deletions Relay/Mutation/PayloadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public function __construct(array $config)
);

$name = str_replace('Payload', '', $config['name']);
if (empty($name)) {
$name = $config['name'];
}

parent::__construct([
'name' => $name.'Payload',
Expand Down
70 changes: 70 additions & 0 deletions Request/BatchParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Request;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class BatchParser implements ParserInterface
{
/**
* @param Request $request
* @return array
*/
public function parse(Request $request)
{
// Extracts the GraphQL request parameters
$data = $this->getParsedBody($request);

if (empty($data)) {
throw new BadRequestHttpException('Must provide at least one valid query.');
}

foreach($data as $i => &$entry) {
if (empty($entry[static::PARAM_QUERY]) || !is_string($entry[static::PARAM_QUERY])) {
throw new BadRequestHttpException(sprintf('No valid query found in node "%s"', $i));
}

$entry = $entry + [
static::PARAM_VARIABLES => null,
static::PARAM_OPERATION_NAME => null,
];
}

return $data;
}

/**
* Gets the body from the request.
*
* @param Request $request
*
* @return array
*/
private function getParsedBody(Request $request)
{
$type = explode(';', $request->headers->get('content-type'))[0];

// JSON object
if ($type !== static::CONTENT_TYPE_JSON) {
throw new BadRequestHttpException(sprintf('Only request with content type "%" is accepted.', static::CONTENT_TYPE_JSON));
}

$parsedBody = json_decode($request->getContent(), true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new BadRequestHttpException('POST body sent invalid JSON');
}

return $parsedBody;
}
}
36 changes: 16 additions & 20 deletions Request/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

class Parser
class Parser implements ParserInterface
{
/**
* Parses the HTTP request and extracts the GraphQL request parameters.
*
* @param Request $request
*
* @return array
*/
public function parse(Request $request)
Expand All @@ -46,23 +43,22 @@ private function getParsedBody(Request $request)

switch ($type) {
// Plain string
case 'application/graphql':
$parsedBody = ['query' => $body];
case static::CONTENT_TYPE_GRAPHQL:
$parsedBody = [static::PARAM_QUERY => $body];
break;

// JSON object
case 'application/json':
$json = json_decode($body, true);
case static::CONTENT_TYPE_JSON:
$parsedBody = json_decode($body, true);

if (JSON_ERROR_NONE !== json_last_error()) {
throw new BadRequestHttpException('POST body sent invalid JSON');
}
$parsedBody = $json;
break;

// URL-encoded query-string
case 'application/x-www-form-urlencoded':
case 'multipart/form-data':
case static::CONTENT_TYPE_FORM:
case static::CONTENT_TYPE_FORM_DATA:
$parsedBody = $request->request->all();
break;

Expand All @@ -86,18 +82,18 @@ private function getParams(Request $request, array $data = [])
{
// Add default request parameters
$data = $data + [
'query' => null,
'variables' => null,
'operationName' => null,
static::PARAM_QUERY => null,
static::PARAM_VARIABLES => null,
static::PARAM_OPERATION_NAME => null,
];

// Keep a reference to the query-string
$qs = $request->query;

// Override request using query-string parameters
$query = $qs->has('query') ? $qs->get('query') : $data['query'];
$variables = $qs->has('variables') ? $qs->get('variables') : $data['variables'];
$operationName = $qs->has('operationName') ? $qs->get('operationName') : $data['operationName'];
$query = $qs->has(static::PARAM_QUERY) ? $qs->get(static::PARAM_QUERY) : $data[static::PARAM_QUERY];
$variables = $qs->has(static::PARAM_VARIABLES) ? $qs->get(static::PARAM_VARIABLES) : $data[static::PARAM_VARIABLES];
$operationName = $qs->has(static::PARAM_OPERATION_NAME) ? $qs->get(static::PARAM_OPERATION_NAME) : $data[static::PARAM_OPERATION_NAME];

// `query` parameter is mandatory.
if (empty($query)) {
Expand All @@ -115,9 +111,9 @@ private function getParams(Request $request, array $data = [])
}

return [
'query' => $query,
'variables' => $variables,
'operationName' => $operationName,
static::PARAM_QUERY => $query,
static::PARAM_VARIABLES => $variables,
static::PARAM_OPERATION_NAME => $operationName,
];
}
}
38 changes: 38 additions & 0 deletions Request/ParserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the OverblogGraphQLBundle package.
*
* (c) Overblog <http://github.com/overblog/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Overblog\GraphQLBundle\Request;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;

interface ParserInterface
{
const CONTENT_TYPE_GRAPHQL ='application/graphql';
const CONTENT_TYPE_JSON = 'application/json';
const CONTENT_TYPE_FORM = 'application/x-www-form-urlencoded';
const CONTENT_TYPE_FORM_DATA = 'multipart/form-data';

const PARAM_QUERY = 'query';
const PARAM_VARIABLES = 'variables';
const PARAM_OPERATION_NAME = 'operationName';

/**
* Parses the HTTP request and extracts the GraphQL request parameters.
*
* @param Request $request
*
* @throw BadRequestHttpException
*
* @return array
*/
public function parse(Request $request);
}
4 changes: 4 additions & 0 deletions Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ services:
overblog_graphql.request_parser:
class: Overblog\GraphQLBundle\Request\Parser

overblog_graphql.request_batch_parser:
class: Overblog\GraphQLBundle\Request\BatchParser

overblog_graphql.schema:
class: GraphQL\Schema
public: false
Expand Down Expand Up @@ -112,6 +115,7 @@ services:
public: false
arguments:
- "@overblog_graphql.cache_expression_language_parser"
- []
calls:
- ["setContainer", ["@service_container"]]

Expand Down
Loading