Skip to content
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

Add support to extend input, interface and enum types #1203

Merged
merged 5 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 15 additions & 1 deletion src/Schema/AST/ASTBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Nuwave\Lighthouse\Schema\AST;

use GraphQL\Language\AST\EnumTypeExtensionNode;
use GraphQL\Language\AST\InputObjectTypeExtensionNode;
use GraphQL\Language\AST\InterfaceTypeExtensionNode;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ObjectTypeExtensionNode;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
Expand Down Expand Up @@ -170,6 +173,7 @@ protected function applyTypeExtensionManipulators(): void
foreach ($this->documentAST->typeExtensions as $typeName => $typeExtensionsList) {
/** @var \GraphQL\Language\AST\TypeExtensionNode $typeExtension */
foreach ($typeExtensionsList as $typeExtension) {

/** @var \Nuwave\Lighthouse\Support\Contracts\TypeExtensionManipulator $typeExtensionManipulator */
foreach (
$this->directiveFactory->createAssociatedDirectivesOfType($typeExtension, TypeExtensionManipulator::class)
Expand All @@ -180,13 +184,23 @@ protected function applyTypeExtensionManipulators(): void

// After manipulation on the type extension has been done,
// we can merge its fields with the original type
if ($typeExtension instanceof ObjectTypeExtensionNode) {
if ($typeExtension instanceof ObjectTypeExtensionNode ||
$typeExtension instanceof InputObjectTypeExtensionNode ||
$typeExtension instanceof InterfaceTypeExtensionNode
) {
$relatedObjectType = $this->documentAST->types[$typeName];

$relatedObjectType->fields = ASTHelper::mergeUniqueNodeList(
$relatedObjectType->fields,
$typeExtension->fields
);
} elseif ($typeExtension instanceof EnumTypeExtensionNode) {
$relatedObjectType = $this->documentAST->types[$typeName];
faiverson marked this conversation as resolved.
Show resolved Hide resolved

$relatedObjectType->values = ASTHelper::mergeUniqueNodeList(
$relatedObjectType->values,
$typeExtension->values
);
}
}
}
Expand Down
123 changes: 120 additions & 3 deletions tests/Unit/Schema/AST/ASTBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public function testCanMergeTypeExtensionFields(): void
type Query {
foo: String
}

extend type Query {
bar: Int!
}

extend type Query {
baz: Boolean
}
Expand All @@ -43,13 +43,80 @@ public function testCanMergeTypeExtensionFields(): void
);
}

public function testCanMergeInputExtensionFields(): void
{
$this->schema = '
faiverson marked this conversation as resolved.
Show resolved Hide resolved
input Inputs {
foo: String
}

extend input Inputs {
bar: Int!
}

extend input Inputs {
baz: Boolean
}
';
$documentAST = $this->astBuilder->documentAST();
$this->assertCount(
3,
$documentAST->types['Inputs']->fields
);
}

public function testCanMergeInterfaceExtensionFields(): void
{
$this->schema = '
interface Named {
name: String!
}

extend interface Named {
bar: Int!
}

extend interface Named {
baz: Boolean
}
';
$documentAST = $this->astBuilder->documentAST();
$this->assertCount(
3,
$documentAST->types['Named']->fields
);
}

public function testCanMergeEnumExtensionFields(): void
{
$this->schema = '
enum MyEnum {
ONE
TWO
}

extend enum MyEnum {
THREE
}

extend enum MyEnum {
FOUR
}
';
$documentAST = $this->astBuilder->documentAST();
$this->assertCount(
4,
$documentAST->types['MyEnum']->values
);
}

public function testDoesNotAllowDuplicateFieldsOnTypeExtensions(): void
{
$this->schema = '
type Query {
foo: String
}

extend type Query {
foo: Int
}
Expand All @@ -58,4 +125,54 @@ public function testDoesNotAllowDuplicateFieldsOnTypeExtensions(): void
$this->expectException(DefinitionException::class);
$this->astBuilder->documentAST();
}

public function testDoesNotAllowDuplicateFieldsOnInputExtensions(): void
{
$this->schema = '
input Inputs {
foo: String
}

extend input Inputs {
foo: Int
}
';

$this->expectException(DefinitionException::class);
faiverson marked this conversation as resolved.
Show resolved Hide resolved
$this->astBuilder->documentAST();
}

public function testDoesNotAllowDuplicateFieldsOnInterfaceExtensions(): void
{
$this->schema = '
interface Named {
foo: String
}

extend interface Named{
foo: Int
}
';

$this->expectException(DefinitionException::class);
$this->astBuilder->documentAST();
}

public function testDoesNotAllowDuplicateValuesOnEnumExtensions(): void
{
$this->schema = '
enum MyEnum {
ONE
TWO
}

extend enum MyEnum {
TWO
THREE
}
';

$this->expectException(DefinitionException::class);
$this->astBuilder->documentAST();
}
}