Skip to content

Commit

Permalink
Mutation support
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Sep 24, 2016
1 parent 560052e commit 593a12e
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
53 changes: 51 additions & 2 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@
class Manager
{
/**
* @var array {@link Chillu\GraphQL\TypeCreator}
* @var array Map of named {@link Type}
*/
protected $types = [];

/**
* @var array Map of {@link Chillu\GraphQL\QueryCreator}
* @var array Map of named arrays
*/
protected $queries = [];

/**
* @var array Map of named arrays
*/
protected $mutations = [];

/**
* @var callable
*/
Expand All @@ -34,6 +39,8 @@ class Manager
public static function createFromConfig($config)
{
$manager = Injector::inst()->create(Manager::class);

// Types
if ($config && array_key_exists('types', $config)) {
foreach ($config['types'] as $name => $typeCreatorClass) {
$typeCreator = Injector::inst()->create($typeCreatorClass, $manager);
Expand All @@ -49,6 +56,7 @@ public static function createFromConfig($config)
}
}

// Queries
if ($config && array_key_exists('queries', $config)) {
foreach ($config['queries'] as $name => $queryCreatorClass) {
$queryCreator = Injector::inst()->create($queryCreatorClass, $manager);
Expand All @@ -64,6 +72,22 @@ public static function createFromConfig($config)
}
}

// Mutations
if ($config && array_key_exists('mutations', $config)) {
foreach ($config['mutations'] as $name => $mutationCreatorClass) {
$mutationCreator = Injector::inst()->create($mutationCreatorClass, $manager);
if (!($mutationCreator instanceof MutationCreator)) {
throw new InvalidArgumentException(sprintf(
'The mutation named "%s" needs to be a class extending ' . MutationCreator::class,
$name
));
}

$mutation = $mutationCreator->toArray();
$manager->addMutation($mutation, $name);
}
}

return $manager;
}

Expand All @@ -77,8 +101,14 @@ public function schema()
'fields' => $this->queries,
]);

$mutationType = new ObjectType([
'name' => 'Mutation',
'fields' => $this->mutations,
]);

return new Schema([
'query' => $queryType,
'mutation' => $mutationType,
]);
}

Expand Down Expand Up @@ -163,6 +193,25 @@ public function getQuery($name)
return $this->queries[$name];
}

/**
* @param array $mutation
* @param string $name Identifier for this mutation (unique in schema)
*/
public function addMutation($mutation, $name)
{
$this->mutations[$name] = $mutation;
}

/**
* @param string $name
*
* @return array
*/
public function getMutation($name)
{
return $this->mutations[$name];
}

/**
* More verbose error display defaults.
*
Expand Down
13 changes: 13 additions & 0 deletions src/MutationCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Chillu\GraphQL;

/**
* Represents a GraphQL mutation.
*
* @todo Validation support
*/
class MutationCreator extends FieldCreator
{

}
15 changes: 15 additions & 0 deletions tests/Fake/MutationCreatorFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Chillu\GraphQL\Tests\Fake;

use Chillu\GraphQL\MutationCreator;

class MutationCreatorFake extends MutationCreator
{
public function type()
{
return function() {
return $this->manager->getType('mytype');
};
}
}
36 changes: 36 additions & 0 deletions tests/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Chillu\GraphQL\Manager;
use Chillu\GraphQL\Tests\Fake\TypeCreatorFake;
use Chillu\GraphQL\Tests\Fake\QueryCreatorFake;
use Chillu\GraphQL\Tests\Fake\MutationCreatorFake;
use GraphQL\Type\Definition\Type;
use SilverStripe\Dev\SapphireTest;
use GraphQL\Error;
Expand All @@ -19,22 +20,39 @@ public function testCreateFromConfig()
'types' => [
'mytype' => TypeCreatorFake::class,
],
'queries' => [
'myquery' => QueryCreatorFake::class,
],
'mutations' => [
'mymutation' => MutationCreatorFake::class,
],
];
$manager = Manager::createFromConfig($config);
$this->assertInstanceOf(
Type::class,
$manager->getType('mytype')
);
$this->assertInternalType(
'array',
$manager->getQuery('myquery')
);
$this->assertInternalType(
'array',
$manager->getMutation('mymutation')
);
}

public function testSchema()
{
$manager = new Manager();
$manager->addType($this->getType($manager), 'mytype');
$manager->addQuery($this->getQuery($manager), 'myquery');
$manager->addMutation($this->getMutation($manager), 'mymutation');

$schema = $manager->schema();
$this->assertInstanceOf(Schema::class, $schema);
$this->assertNotNull($schema->getType('TypeCreatorFake'));
$this->assertNotNull($schema->getMutationType()->getField('mymutation'));
$this->assertNotNull($schema->getQueryType()->getField('myquery'));
}

Expand Down Expand Up @@ -73,6 +91,19 @@ public function testAddQuery()
);
}

public function testAddMutation()
{
$manager = new Manager();
$mutation = $this->getMutation($manager);
$type = $this->getType($manager);
$manager->addMutation($mutation, 'mymutation');
$manager->addType($type, 'mytype');
$this->assertEquals(
$mutation,
$manager->getMutation('mymutation')
);
}

public function testQueryWithError()
{
$mock = $this->getMockBuilder(Manager::class)
Expand Down Expand Up @@ -102,4 +133,9 @@ protected function getQuery(Manager $manager)
{
return (new QueryCreatorFake($manager))->toArray();
}

protected function getMutation(Manager $manager)
{
return (new MutationCreatorFake($manager))->toArray();
}
}

0 comments on commit 593a12e

Please sign in to comment.