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
7 changes: 6 additions & 1 deletion Config/TypeWithOutputFieldsDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
51 changes: 35 additions & 16 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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();
Expand Down
91 changes: 90 additions & 1 deletion Resources/doc/definitions/builders/args.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
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:
-
Copy link

Choose a reason for hiding this comment

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

is this normal?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it means that we need a list of alias/class values

alias: "Pager"
class: "MyBundle\\GraphQL\\Args\\Pager"
# using short syntax
# Pager: "MyBundle\\GraphQL\\Args\\Pager"
```

Builder class must implements `Overblog\GraphQLBundle\Definition\Builder\MappingInterface`

```php
<?php

namespace MyBundle\GraphQL\Args;

use Overblog\GraphQLBundle\Definition\Builder\MappingInterface;

class Pager implements MappingInterface
{
public function toMappingDefinition(array $config)
{
$defaultLimit = isset($config['defaultLimit']) ? (int)$config['defaultLimit'] : 20;

return [
'limit' => [
'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
```
47 changes: 40 additions & 7 deletions Resources/doc/definitions/builders/field.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ Define your custom field builder
```yaml
#app/config/config.yml
overblog_graphql:
#...
#...
definitions:
#...
builders:
field:
-
alias: "RawId"
class: "MyBundle\\GraphQL\\Field\\RawIdField"
# using short syntax
# RawId: "MyBundle\\GraphQL\\Field\\RawIdField"
```

Builder class must implements `Overblog\GraphQLBundle\Definition\Builder\MappingInterface`

```php
<?php

namespace MyBundle\GraphQL\Field;

use Overblog\GraphQLBundle\Definition\Builder\MappingInterface;
Expand All @@ -28,10 +32,13 @@ class RawIdField implements MappingInterface
{
public function toMappingDefinition(array $config)
{
$name = isset($config['name']) ? $config['name'] : 'id';
$type = isset($config['type']) ? $config['type'] : 'Int!';

return [
'description' => 'The raw ID of an object',
'type' => 'Int!',
'resolve' => '@=value.id',
'type' => $type,
'resolve' => '@=value.'.$name,
];
}
}
Expand All @@ -40,12 +47,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"
Expand All @@ -54,6 +60,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"
```
9 changes: 4 additions & 5 deletions Resources/doc/definitions/relay/node/global-id.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ User:
fields:
id:
builder: "Relay::GlobalId"
builderConfig:
typeName: User
name:
type: String
interfaces: [NodeInterface]
Expand All @@ -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
Expand All @@ -51,8 +52,6 @@ Post:
fields:
id:
builder: "Relay::GlobalId"
builderConfig:
typeName: Post
text:
type: String
interfaces: [NodeInterface]
Expand Down
4 changes: 3 additions & 1 deletion Tests/DependencyInjection/Builder/PagerArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!',
Expand Down
22 changes: 18 additions & 4 deletions Tests/DependencyInjection/OverblogGraphQLTypesExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[
Expand Down Expand Up @@ -165,6 +162,10 @@ public function testCustomBuilders()
'type' => '[String!]!',
'argsBuilder' => ['builder' => 'Pager'],
],
'categories2' => [
'type' => '[String!]!',
'argsBuilder' => ['builder' => 'Pager', 'config' => ['defaultLimit' => 50]],
],
],
],
],
Expand Down Expand Up @@ -218,6 +219,19 @@ public function testCustomBuilders()
],
],
],
'categories2' => [
'type' => '[String!]!',
'args' => [
'limit' => [
'type' => 'Int!',
'defaultValue' => 50,
],
'offset' => [
'type' => 'Int!',
'defaultValue' => 0,
],
],
],
],
'interfaces' => [],
],
Expand Down