From a875a2b8b85d501ee5275f0cf66e94dc6c1a3f59 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Mon, 2 Jan 2017 17:59:14 +0100 Subject: [PATCH 1/3] Added args builder config Also Documented it :memo: --- Config/TypeWithOutputFieldsDefinition.php | 7 +- Resources/doc/definitions/builders/args.md | 89 ++++++++++++++++++- .../DependencyInjection/Builder/PagerArgs.php | 4 +- .../OverblogGraphQLTypesExtensionTest.php | 17 ++++ 4 files changed, 114 insertions(+), 3 deletions(-) diff --git a/Config/TypeWithOutputFieldsDefinition.php b/Config/TypeWithOutputFieldsDefinition.php index 818f4fc47..a26e257a4 100644 --- a/Config/TypeWithOutputFieldsDefinition.php +++ b/Config/TypeWithOutputFieldsDefinition.php @@ -142,8 +142,13 @@ protected function outputFieldsSelection($name) $argsBuilderName = $field['argsBuilder']['builder']; } + $builderConfig = []; + if (isset($field['argsBuilder']['config']) && is_array($field['argsBuilder']['config'])) { + $builderConfig = $field['argsBuilder']['config']; + } + if ($argsBuilderName) { - $args = $this->getBuilder($argsBuilderName, static::BUILDER_ARGS_TYPE)->toMappingDefinition([]); + $args = $this->getBuilder($argsBuilderName, static::BUILDER_ARGS_TYPE)->toMappingDefinition($builderConfig); $field['args'] = isset($field['args']) && is_array($field['args']) ? array_merge($args, $field['args']) : $args; } diff --git a/Resources/doc/definitions/builders/args.md b/Resources/doc/definitions/builders/args.md index 1d7b49238..a3c5dbcff 100644 --- a/Resources/doc/definitions/builders/args.md +++ b/Resources/doc/definitions/builders/args.md @@ -1,4 +1,91 @@ Args builder ============ -TODO +Builder is a way to don't repeat args definition. + +Define your custom args builder + +```yaml +#app/config/config.yml +overblog_graphql: + #... + definitions: + #... + builders: + args: + - + alias: "Pager" + class: "MyBundle\\GraphQL\\Args\\Pager" +``` + +Builder class must implements `Overblog\GraphQLBundle\Definition\Builder\MappingInterface` + +```php + [ + 'type' => 'Int!', + 'defaultValue' => $defaultLimit, + ], + 'offset' => [ + 'type' => 'Int!', + 'defaultValue' => 0, + ], + ]; + } +} +``` + +usage: + +```yaml +foo: + type: "object" + config: + fields: + categories: + type: "[String!]!" + argsBuilder: "Pager" + + categories2: + type: "[String!]!" + argsBuilder: + builder: "Pager" + config: + defaultLimit: 50 +``` + +this is equivalent to: + +```yaml +foo: + categories: + type: "[String!]!" + args: + limit: + type: "Int!" + defaultValue: 20 + offset: + type: "Int!" + defaultValue: 0 + categories2: + type: "[String!]!" + args: + limit: + type: "Int!" + defaultValue: 50 + offset: + type: "Int!" + defaultValue: 0 +``` diff --git a/Tests/DependencyInjection/Builder/PagerArgs.php b/Tests/DependencyInjection/Builder/PagerArgs.php index bfb33be9c..021fded40 100644 --- a/Tests/DependencyInjection/Builder/PagerArgs.php +++ b/Tests/DependencyInjection/Builder/PagerArgs.php @@ -17,10 +17,12 @@ class PagerArgs implements MappingInterface { public function toMappingDefinition(array $config) { + $defaultLimit = isset($config['defaultLimit']) ? (int) $config['defaultLimit'] : 20; + return [ 'limit' => [ 'type' => 'Int!', - 'defaultValue' => 20, + 'defaultValue' => $defaultLimit, ], 'offset' => [ 'type' => 'Int!', diff --git a/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php b/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php index 801c04f73..6ceb671bf 100644 --- a/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php +++ b/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php @@ -165,6 +165,10 @@ public function testCustomBuilders() 'type' => '[String!]!', 'argsBuilder' => ['builder' => 'Pager'], ], + 'categories2' => [ + 'type' => '[String!]!', + 'argsBuilder' => ['builder' => 'Pager', 'config' => ['defaultLimit' => 50]], + ], ], ], ], @@ -218,6 +222,19 @@ public function testCustomBuilders() ], ], ], + 'categories2' => [ + 'type' => '[String!]!', + 'args' => [ + 'limit' => [ + 'type' => 'Int!', + 'defaultValue' => 50, + ], + 'offset' => [ + 'type' => 'Int!', + 'defaultValue' => 0, + ], + ], + ], ], 'interfaces' => [], ], From 2b2b3d5d227ab77c2149901be4cf23d08f42081f Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Mon, 2 Jan 2017 18:02:14 +0100 Subject: [PATCH 2/3] :memo: Added better Field builder documentation --- Resources/doc/definitions/builders/field.md | 45 ++++++++++++++++--- .../doc/definitions/relay/node/global-id.md | 9 ++-- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/Resources/doc/definitions/builders/field.md b/Resources/doc/definitions/builders/field.md index fc3505f79..60bdc2663 100644 --- a/Resources/doc/definitions/builders/field.md +++ b/Resources/doc/definitions/builders/field.md @@ -7,7 +7,7 @@ Define your custom field builder ```yaml #app/config/config.yml overblog_graphql: - #... + #... definitions: #... builders: @@ -20,6 +20,8 @@ overblog_graphql: Builder class must implements `Overblog\GraphQLBundle\Definition\Builder\MappingInterface` ```php + 'The raw ID of an object', - 'type' => 'Int!', - 'resolve' => '@=value.id', + 'type' => $type, + 'resolve' => '@=value.'.$name, ]; } } @@ -40,12 +45,11 @@ class RawIdField implements MappingInterface usage: ```yaml -#Resources/graphql/schema.yml User: type: object config: fields: - # equivalent to rawId: { description: "The user raw id", type: 'Int!', resolve: "@=value.id" } + # equivalent to => rawId: { description: "The user raw id", type: 'Int!', resolve: "@=value.id" } rawId: builder: "RawId" description: "The user raw id" @@ -54,6 +58,33 @@ Post: type: object config: fields: - # equivalent to rawId: { description: "The raw ID of an object", type: 'Int!', resolve: "@=value.id" } - rawId: "RawId" + # equivalent to => rawId: { description: "The raw ID of an object", type: 'String', resolve: "@=value.photoID" } + rawId: + builder: "RawId" + #config your builder + builderConfig: + name: photoID + type: String +``` + +this is equivalent to: + +```yaml +User: + type: object + config: + fields: + rawId: + description: "The user raw id" + type: 'Int!' + resolve: "@=value.id" + +Post: + type: object + config: + fields: + rawId: + description: "The raw ID of an object" + type: 'String' + resolve: "@=value.photoID" ``` diff --git a/Resources/doc/definitions/relay/node/global-id.md b/Resources/doc/definitions/relay/node/global-id.md index 23a02a05a..066bcf23e 100644 --- a/Resources/doc/definitions/relay/node/global-id.md +++ b/Resources/doc/definitions/relay/node/global-id.md @@ -26,8 +26,6 @@ User: fields: id: builder: "Relay::GlobalId" - builderConfig: - typeName: User name: type: String interfaces: [NodeInterface] @@ -38,8 +36,11 @@ Photo: fields: id: builder: "Relay::GlobalId" + # here the entry to custom your field builder builderConfig: - typeName: Photo + # Change the type name + typeName: Image + # custom id fetcher function idFetcher: '@=value["photoId"]' width: type: Int @@ -51,8 +52,6 @@ Post: fields: id: builder: "Relay::GlobalId" - builderConfig: - typeName: Post text: type: String interfaces: [NodeInterface] From c4dbecc727f2b1949740d7f752bf2fe1a6229f43 Mon Sep 17 00:00:00 2001 From: Jeremiah VALERIE Date: Thu, 9 Feb 2017 15:41:12 +0100 Subject: [PATCH 3/3] Allow short config builder definition syntax --- DependencyInjection/Configuration.php | 51 +++++++++++++------ Resources/doc/definitions/builders/args.md | 2 + Resources/doc/definitions/builders/field.md | 2 + .../OverblogGraphQLTypesExtensionTest.php | 5 +- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 6af8f9d1f..9309843b3 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -110,22 +110,8 @@ public function getConfigTreeBuilder() ->arrayNode('builders') ->children() - ->arrayNode('field') - ->prototype('array') - ->children() - ->scalarNode('alias')->isRequired()->end() - ->scalarNode('class')->isRequired()->end() - ->end() - ->end() - ->end() - ->arrayNode('args') - ->prototype('array') - ->children() - ->scalarNode('alias')->isRequired()->end() - ->scalarNode('class')->isRequired()->end() - ->end() - ->end() - ->end() + ->append($this->addBuilderSection('field')) + ->append($this->addBuilderSection('args')) ->end() ->end() @@ -176,6 +162,39 @@ public function getConfigTreeBuilder() return $treeBuilder; } + private function addBuilderSection($name) + { + $builder = new TreeBuilder(); + $node = $builder->root($name); + $node->beforeNormalization() + ->ifTrue(function ($v) { + return is_array($v) && !empty($v); + }) + ->then(function ($v) { + foreach ($v as $key => &$config) { + if (is_string($config)) { + $config = [ + 'alias' => $key, + 'class' => $config, + ]; + } + } + + return $v; + }) + ->end(); + + $node->prototype('array') + ->children() + ->scalarNode('alias')->isRequired()->end() + ->scalarNode('class')->isRequired()->end() + ->end() + ->end() + ; + + return $node; + } + private function addSecurityQuerySection($name, $disabledValue) { $builder = new TreeBuilder(); diff --git a/Resources/doc/definitions/builders/args.md b/Resources/doc/definitions/builders/args.md index a3c5dbcff..a7e1894bb 100644 --- a/Resources/doc/definitions/builders/args.md +++ b/Resources/doc/definitions/builders/args.md @@ -16,6 +16,8 @@ overblog_graphql: - alias: "Pager" class: "MyBundle\\GraphQL\\Args\\Pager" +# using short syntax +# Pager: "MyBundle\\GraphQL\\Args\\Pager" ``` Builder class must implements `Overblog\GraphQLBundle\Definition\Builder\MappingInterface` diff --git a/Resources/doc/definitions/builders/field.md b/Resources/doc/definitions/builders/field.md index 60bdc2663..3170636ae 100644 --- a/Resources/doc/definitions/builders/field.md +++ b/Resources/doc/definitions/builders/field.md @@ -15,6 +15,8 @@ overblog_graphql: - alias: "RawId" class: "MyBundle\\GraphQL\\Field\\RawIdField" +# using short syntax +# RawId: "MyBundle\\GraphQL\\Field\\RawIdField" ``` Builder class must implements `Overblog\GraphQLBundle\Definition\Builder\MappingInterface` diff --git a/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php b/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php index 6ceb671bf..68356f408 100644 --- a/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php +++ b/Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php @@ -127,10 +127,7 @@ public function testCustomBuilders() 'definitions' => [ 'builders' => [ 'field' => [ - [ - 'alias' => 'RawId', - 'class' => 'Overblog\\GraphQLBundle\\Tests\\DependencyInjection\\Builder\\RawIdField', - ], + 'RawId' => 'Overblog\\GraphQLBundle\\Tests\\DependencyInjection\\Builder\\RawIdField', ], 'args' => [ [