Skip to content

Commit

Permalink
InterfaceType creation
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Oct 2, 2016
1 parent af155a0 commit dfcca94
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
47 changes: 47 additions & 0 deletions src/InterfaceTypeCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Chillu\GraphQL;

use GraphQL\Type\Definition\InterfaceType as BaseInterfaceType;

class InterfaceTypeCreator extends TypeCreator {

protected function getTypeResolver()
{
if(!method_exists($this, 'resolveType'))
{
return null;
}

$resolver = array($this, 'resolveType');
return function() use ($resolver)
{
$args = func_get_args();
return call_user_func_array($resolver, $args);
};
}

/**
* Get the attributes from the container.
*
* @return array
*/
public function getAttributes()
{
$attributes = parent::getAttributes();

$resolver = $this->getTypeResolver();
if(isset($resolver))
{
$attributes['resolveType'] = $resolver;
}

return $attributes;
}

public function toType()
{
return new BaseInterfaceType($this->toArray());
}

}
18 changes: 17 additions & 1 deletion src/TypeCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public function fields()
return [];
}

/**
* @return array
*/
public function interfaces()
{
return [];
}

/**
* Returns field structure with field resolvers added.
*
Expand Down Expand Up @@ -106,14 +114,22 @@ public function toArray()

public function getAttributes()
{
return array_merge(
$interfaces = $this->interfaces();

$attributes = array_merge(
$this->attributes(),
[
'fields' => function () {
return $this->getFields();
},
]
);

if(sizeof($interfaces)) {
$attributes['interfaces'] = $interfaces;
}

return $attributes;
}

/**
Expand Down

0 comments on commit dfcca94

Please sign in to comment.