Skip to content

Commit

Permalink
Verify that a type name is specified before overriding class's type name
Browse files Browse the repository at this point in the history
  • Loading branch information
sburba committed Aug 1, 2018
1 parent 171631a commit 4d05023
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Folklore/GraphQL/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function schema($schema = null)
$this->typesInstances[$name] = $objectType;
$types[] = $objectType;

$this->addType($type, $name);
$this->addType($type, is_numeric($name) ? null : $name);
}
} else {
foreach ($this->types as $name => $type) {
Expand Down
41 changes: 24 additions & 17 deletions tests/GraphQLTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

use GraphQL\Type\Schema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Error\Error;
use Folklore\GraphQL\Error\ValidationError;
use Folklore\GraphQL\Events\TypeAdded;
use Folklore\GraphQL\Events\SchemaAdded;
use Folklore\GraphQL\Events\TypeAdded;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;

class GraphQLTest extends TestCase
{
Expand Down Expand Up @@ -77,14 +77,21 @@ public function testSchemaWithArray()
'updateExampleCustom' => UpdateExampleMutation::class
],
'types' => [
CustomExampleType::class
CustomExampleType::class,
AnotherCustomExampleType::class
]
]);


$graphql_types = GraphQL::getTypes();
$schema_types = $schema->getTypeMap();

$this->assertGraphQLSchema($schema);
$this->assertGraphQLSchemaHasQuery($schema, 'examplesCustom');
$this->assertGraphQLSchemaHasMutation($schema, 'updateExampleCustom');
$this->assertArrayHasKey('CustomExample', $schema->getTypeMap());
$this->assertArrayHasKey('CustomExample', $schema_types);
$this->assertArrayHasKey('AnotherCustomExample', $schema_types);
$this->assertArrayHasKey('CustomExample', $graphql_types);
$this->assertArrayHasKey('AnotherCustomExample', $graphql_types);
}

/**
Expand Down Expand Up @@ -239,17 +246,17 @@ public function testAddType()
public function testAddTypeWithName()
{
GraphQL::addType(ExampleType::class, 'CustomExample');

$types = GraphQL::getTypes();
$this->assertArrayHasKey('CustomExample', $types);

$type = app($types['CustomExample']);
$this->assertInstanceOf(ExampleType::class, $type);

$type = GraphQL::type('CustomExample');
$this->assertInstanceOf(\GraphQL\Type\Definition\ObjectType::class, $type);
}

/**
* Test get types
*
Expand All @@ -259,11 +266,11 @@ public function testGetTypes()
{
$types = GraphQL::getTypes();
$this->assertArrayHasKey('Example', $types);

$type = app($types['Example']);
$this->assertInstanceOf(\Folklore\GraphQL\Support\Type::class, $type);
}

/**
* Test add schema
*
Expand All @@ -272,9 +279,9 @@ public function testGetTypes()
public function testAddSchema()
{
$this->expectsEvents(SchemaAdded::class);

$this->app['events']->shouldReceive('listen')->with(SchemaAdded::class, Closure::class)->once();

GraphQL::addSchema('custom_add', [
'query' => [
'examplesCustom' => ExamplesQuery::class
Expand All @@ -290,7 +297,7 @@ public function testAddSchema()
$schemas = GraphQL::getSchemas();
$this->assertArrayHasKey('custom_add', $schemas);
}

/**
* Test get schemas
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Objects/AnotherCustomExampleType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Type as GraphQLType;

class AnotherCustomExampleType extends GraphQLType
{

protected $attributes = [
'name' => 'AnotherCustomExample',
'description' => 'An example'
];

public function fields()
{
return [
'test' => [
'type' => Type::string(),
'description' => 'A test field'
]
];
}
}

0 comments on commit 4d05023

Please sign in to comment.