Skip to content

Add ObjectTypeConfig and ArgConfig #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 21 additions & 0 deletions src/Type/Builder/ArgConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace GraphQL\Type\Builder;

class ArgConfig extends Config
{
use NameDescriptionConfigTrait;
use TypeConfigTrait;

/**
* @param mixed $defaultValue
*
* @return $this
*/
public function defaultValue($defaultValue)
{
$this->addConfig('defaultValue', $defaultValue, false);

return $this;
}
}
29 changes: 29 additions & 0 deletions src/Type/Builder/ArgsConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace GraphQL\Type\Builder;

class ArgsConfig extends Config
{
public function addArgConfig(ArgConfig $argConfig)
{
return $this->addConfig('args', $argConfig->build());
}

public function addArg($name, $type, $defaultValue = null, $description = null)
{
$argConfig = ArgConfig::create()
->name($name)
->type($type)
->defaultValue($defaultValue)
->description($description);

return $this->addArgConfig($argConfig);
}

public function build()
{
$args = parent::build();

return isset($args['args']) ? $args['args'] : [];
}
}
43 changes: 43 additions & 0 deletions src/Type/Builder/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace GraphQL\Type\Builder;

abstract class Config
{
/**
* @var array
*/
private $config;

protected function __construct()
{
$this->config = [];
}

/**
* @return static
*/
public static function create()
{
return new static();
}

protected function addConfig($name, $value, $append = true)
{
if (!$append) {
$this->config[$name] = $value;
} else {
if (!isset($this->config[$name])) {
$this->config[$name] = [];
}
$this->config[$name][] = $value;
}

return $this;
}

public function build()
{
return $this->config;
}
}
61 changes: 61 additions & 0 deletions src/Type/Builder/FieldConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace GraphQL\Type\Builder;

class FieldConfig extends Config
{
use NameDescriptionConfigTrait;
use TypeConfigTrait;

/**
* @param callable|null $resolve
*
* @return $this
*/
public function resolve(callable $resolve = null)
{
return $this->addConfig('resolve', $resolve, false);
}

/**
* @param callable|null $complexity
*
* @return $this
*/
public function complexity(callable $complexity = null)
{
return $this->addConfig('complexity', $complexity, false);
}

/**
* @param string|null $deprecationReason
* @return $this
*/
public function deprecationReason($deprecationReason = null)
{
return $this->addConfig('deprecationReason', $deprecationReason, false);
}

/**
* @param ArgConfig $arg
*
* @return $this
*/
public function addArg(ArgConfig $arg)
{
return $this->addConfig('args', $arg->build());
}

/**
* @param ArgsConfig $args
*
* @return $this
*/
public function addArgs(ArgsConfig $args)
{
foreach ($args->build() as $arg) {
$this->addConfig('args', $arg);
}
return $this;
}
}
15 changes: 15 additions & 0 deletions src/Type/Builder/FieldsConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace GraphQL\Type\Builder;

class FieldsConfig extends Config
{
use FieldsConfigTrait;

public function build()
{
$fields = parent::build();

return isset($fields['fields']) ? $fields['fields'] : [];
}
}
55 changes: 55 additions & 0 deletions src/Type/Builder/FieldsConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace GraphQL\Type\Builder;

/**
* Class FieldsConfigTrait
*
* @method $this addConfig(string $name, mixed $value, boolean $append = true)
*/
trait FieldsConfigTrait
{
public function addFieldConfig(FieldConfig $fieldConfig)
{
return $this->addConfig('fields', $fieldConfig->build());
}

public function addField($name, $type, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null)
{
return $this->pushField($name, $type, $description, $args, $resolve, $complexity);
}

public function addDeprecatedField($name, $type, $deprecationReason, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null)
{
return $this->pushField($name, $type, $description, $args, $resolve, $complexity, $deprecationReason);
}

protected function pushField($name, $type, $description = null, ArgsConfig $args = null, callable $resolve = null, callable $complexity = null, $deprecationReason = null)
{
$fieldConfig = FieldConfig::create()
->name($name)
->type($type);

if (null !== $resolve) {
$fieldConfig->resolve($resolve);
}

if (null !== $description) {
$fieldConfig->description($description);
}

if (null !== $args) {
$fieldConfig->addArgs($args);
}

if (null !== $complexity) {
$fieldConfig->complexity($complexity);
}

if (null !== $deprecationReason) {
$fieldConfig->deprecationReason($deprecationReason);
}

return $this->addFieldConfig($fieldConfig);
}
}
35 changes: 35 additions & 0 deletions src/Type/Builder/NameDescriptionConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace GraphQL\Type\Builder;

/**
* Class NameDescriptionConfigTrait
*
* @method $this addConfig(string $name, mixed $value, boolean $append = true)
*/
trait NameDescriptionConfigTrait
{
/**
* @param string $name
*
* @return $this
*/
public function name($name)
{
$this->addConfig('name', $name, false);

return $this;
}

/**
* @param string|null $description
*
* @return $this
*/
public function description($description)
{
$this->addConfig('description', $description, false);

return $this;
}
}
78 changes: 78 additions & 0 deletions src/Type/Builder/ObjectTypeConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace GraphQL\Type\Builder;

use GraphQL\Type\Definition\InterfaceType;

class ObjectTypeConfig extends Config
{
use NameDescriptionConfigTrait;
use FieldsConfigTrait;

/**
* @param InterfaceType[] $interfaces
*
* @return $this
*/
public function interfaces(array $interfaces)
{
$this->addConfig('interfaces', [], false);
foreach ($interfaces as $interface) {
$this->addInterface($interface);
}

return $this;
}

/**
* @param callable|FieldsConfig $fields
*
* @return $this
*/
public function fields($fields)
{
if ($fields instanceof FieldsConfig) {
$this->addConfig('fields', $fields->build(), false);
} elseif ($fields) {
$this->addConfig('fields', $fields, false);
}

return $this;
}

/**
* @param InterfaceType $interface
*
* @return $this
*/
public function addInterface(InterfaceType $interface)
{
$this->addConfig('interfaces', $interface);

return $this;
}

/**
* @param callable|null $isTypeOf
*
* @return $this
*/
public function isTypeOf(callable $isTypeOf = null)
{
$this->addConfig('isTypeOf', $isTypeOf, false);

return $this;
}

/**
* @param callable|null $resolveField
*
* @return $this
*/
public function resolveField(callable $resolveField = null)
{
$this->addConfig('resolveField', $resolveField, false);

return $this;
}
}
25 changes: 25 additions & 0 deletions src/Type/Builder/TypeConfigTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace GraphQL\Type\Builder;

use GraphQL\Type\Definition\Type;

/**
* Class TypeConfigTrait
*
* @method $this addConfig(string $name, mixed $value, boolean $append = true)
*/
trait TypeConfigTrait
{
/**
* @param Type|callable $type
*
* @return $this
*/
public function type($type)
{
$this->addConfig('type', $type, false);

return $this;
}
}
Loading