Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
28 changes: 27 additions & 1 deletion src/Utils/ASTDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -536,16 +536,42 @@ private function makeScalarDef(ScalarTypeDefinitionNode $def): CustomScalarType
]);
}

/** @throws InvariantViolation */
/**
* @throws \Exception
* @throws \ReflectionException
* @throws InvariantViolation
*/
private function makeInputObjectDef(InputObjectTypeDefinitionNode $def): InputObjectType
{
$name = $def->name->value;
/** @var array<InputObjectTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];

$oneOfDirective = Directive::oneOfDirective();

// Check for @oneOf directive in the definition node
$isOneOf = Values::getDirectiveValues(
$oneOfDirective,
$def
) !== null;

// Check for @oneOf directive in extension nodes
if (! $isOneOf) {
foreach ($extensionASTNodes as $extensionNode) {
if (Values::getDirectiveValues(
$oneOfDirective,
$extensionNode
) !== null) {
$isOneOf = true;
break;
}
}
}

return new InputObjectType([
'name' => $name,
'description' => $def->description->value ?? null,
'isOneOf' => $isOneOf,
'fields' => fn (): array => $this->makeInputFields([$def, ...$extensionASTNodes]),
'astNode' => $def,
'extensionASTNodes' => $extensionASTNodes,
Expand Down
1 change: 1 addition & 0 deletions src/Utils/SchemaPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ protected static function printInputObject(InputObjectType $type, array $options

return static::printDescription($options, $type)
. "input {$type->name}"
. ($type->isOneOf() ? ' @oneOf' : '')
. static::printBlock($fields);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Utils/BuildSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,39 @@ public function testCorrectlyExtendInputObjectType(): void
self::assertSame($inputSDL, $this->printAllASTNodes($someInput));
}

/** @see it('Correctly extend input object type with @oneOf directive') */
public function testCorrectlyExtendInputObjectTypeWithOneOfDirective(): void
{
$inputSDL = <<<'GRAPHQL'
input SomeInput {
first: String
}

extend input SomeInput @oneOf {
second: Int
}

GRAPHQL;

$schema = BuildSchema::build($inputSDL);

$someInput = $schema->getType('SomeInput');
assert($someInput instanceof InputObjectType);

// Verify that the @oneOf directive from the extension is properly applied
self::assertTrue($someInput->isOneOf());

$expectedSomeInputSDL = <<<'GRAPHQL'
input SomeInput @oneOf {
first: String
second: Int
}
GRAPHQL;

self::assertSame($expectedSomeInputSDL, SchemaPrinter::printType($someInput));
self::assertSame($inputSDL, $this->printAllASTNodes($someInput));
}

/** @see it('Correctly assign AST nodes') */
public function testCorrectlyAssignASTNodes(): void
{
Expand Down
26 changes: 26 additions & 0 deletions tests/Utils/SchemaPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,32 @@ public function testInputType(): void
);
}

/** @see it('Print Input Type with @oneOf directive') */
public function testInputTypeWithOneOfDirective(): void
{
$inputType = new InputObjectType([
'name' => 'InputType',
'isOneOf' => true,
'fields' => [
'int' => Type::int(),
],
]);

$schema = new Schema([
'types' => [$inputType],
]);

self::assertPrintedSchemaEquals(
<<<'GRAPHQL'
input InputType @oneOf {
int: Int
}

GRAPHQL,
$schema
);
}

/** @see it('Custom Scalar') */
public function testCustomScalar(): void
{
Expand Down
Loading