From d8703bb8d8c1d0951078ed2b3e3028b3fedb96b6 Mon Sep 17 00:00:00 2001 From: Remco Pander Date: Fri, 21 Jul 2017 11:17:22 +0200 Subject: [PATCH 1/2] Allow different methods of batching --- Controller/GraphController.php | 3 ++- DependencyInjection/Configuration.php | 4 ++++ DependencyInjection/OverblogGraphQLExtension.php | 6 ++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/Controller/GraphController.php b/Controller/GraphController.php index a78fc94ab..8c4a8edfe 100644 --- a/Controller/GraphController.php +++ b/Controller/GraphController.php @@ -60,6 +60,7 @@ private function createResponse(Request $request, $schemaName, $batched) private function processBatchQuery(Request $request, $schemaName = null) { $queries = $this->get('overblog_graphql.request_batch_parser')->parse($request); + $apolloBatching = 'apollo' === $this->getParameter('overblog_graphql.batching_method'); $payloads = []; foreach ($queries as $query) { @@ -71,7 +72,7 @@ private function processBatchQuery(Request $request, $schemaName = null) [], $schemaName ); - $payloads[] = ['id' => $query['id'], 'payload' => $payloadResult->toArray()]; + $payloads[] = $apolloBatching ? $payloadResult->toArray() : ['id' => $query['id'], 'payload' => $payloadResult->toArray()]; } return $payloads; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 067701260..d14188e27 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -38,6 +38,10 @@ public function getConfigTreeBuilder() $rootNode ->children() + ->enumNode('batching_method') + ->values(['relay', 'apollo']) + ->defaultValue('relay') + ->end() ->arrayNode('definitions') ->addDefaultsIfNotSet() ->children() diff --git a/DependencyInjection/OverblogGraphQLExtension.php b/DependencyInjection/OverblogGraphQLExtension.php index 7b6423a45..1d974c27a 100644 --- a/DependencyInjection/OverblogGraphQLExtension.php +++ b/DependencyInjection/OverblogGraphQLExtension.php @@ -34,6 +34,7 @@ public function load(array $configs, ContainerBuilder $container) $config = $this->treatConfigs($configs, $container); + $this->setBatchingMethod($config, $container); $this->setExpressionLanguageDefaultParser($container); $this->setServicesAliases($config, $container); $this->setSchemaBuilderArguments($config, $container); @@ -76,6 +77,11 @@ private function setAutoMappingParameters(array $config, ContainerBuilder $conta $container->setParameter($this->getAlias().'.auto_mapping.directories', $config['definitions']['auto_mapping']['directories']); } + private function setBatchingMethod(array $config, ContainerBuilder $container) + { + $container->setParameter($this->getAlias().'.batching_method', $config['batching_method']); + } + private function setExpressionLanguageDefaultParser(ContainerBuilder $container) { $class = version_compare(Kernel::VERSION, '3.2.0', '>=') ? ArrayAdapter::class : ArrayParserCache::class; From 441efd0dca901bf726b306b8701188c92832b826 Mon Sep 17 00:00:00 2001 From: Remco Pander Date: Fri, 21 Jul 2017 19:07:17 +0200 Subject: [PATCH 2/2] Add some documentation. --- Resources/doc/data-fetching/batching.md | 19 +++++++++++++++++++ Resources/doc/data-fetching/index.md | 1 + Resources/doc/index.md | 4 ++-- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 Resources/doc/data-fetching/batching.md diff --git a/Resources/doc/data-fetching/batching.md b/Resources/doc/data-fetching/batching.md new file mode 100644 index 000000000..57483457c --- /dev/null +++ b/Resources/doc/data-fetching/batching.md @@ -0,0 +1,19 @@ +# Query batching + +The bundle supports batching using [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer) or [Apollo GraphQL](http://dev.apollodata.com/core/network.html#query-batching) directly. +To use batching, you must use "/batch" as a suffix to your graphql endpoint (see routing config). +Then you can switch between implementations in your configuration like so: + +For Relay (default value): +```yaml +overblog_graphql: + batching_method: "relay" +``` + +For Apollo: +```yaml +overblog_graphql: + batching_method: "apollo" +``` + +Done! \ No newline at end of file diff --git a/Resources/doc/data-fetching/index.md b/Resources/doc/data-fetching/index.md index 84479edbe..f55c116bf 100644 --- a/Resources/doc/data-fetching/index.md +++ b/Resources/doc/data-fetching/index.md @@ -1,6 +1,7 @@ Data fetching ============= +* [Batching](batching.md) * [Promise](promise.md) Next step [secure your server](../security/index.md). diff --git a/Resources/doc/index.md b/Resources/doc/index.md index ad9c92e6e..b40e3abe6 100644 --- a/Resources/doc/index.md +++ b/Resources/doc/index.md @@ -3,7 +3,7 @@ OverblogGraphQLBundle This Symfony 2 / 3 bundle provide integration [GraphQL](https://facebook.github.io/graphql/) using [webonyx/graphql-php](https://github.com/webonyx/graphql-php) and [GraphQL Relay](https://facebook.github.io/relay/docs/graphql-relay-specification.html). -It also supports batching using libs like [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer). +It also supports batching using libs like [ReactRelayNetworkLayer](https://github.com/nodkz/react-relay-network-layer) or [Apollo GraphQL](http://dev.apollodata.com/core/network.html#query-batching). Requirements ------------ @@ -54,4 +54,4 @@ overblog_graphql_graphiql: resource: "@OverblogGraphQLBundle/Resources/config/routing/graphiql.yml" ``` -Now you can define your [graphQL schema](definitions/index.md). +Now you can define your [graphQL schema](definitions/index.md). \ No newline at end of file