Skip to content

Commit

Permalink
Wip oneOf directive
Browse files Browse the repository at this point in the history
  • Loading branch information
Václav Pelíšek committed Nov 6, 2023
1 parent c5c0e4d commit a93005e
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
34 changes: 17 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/Typesystem/Exception/OneOfDirectiveNotSatisfied.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\Typesystem\Exception;

final class OneOfDirectiveNotSatisfied extends \Graphpinator\Typesystem\Exception\TypeError
{
public const MESSAGE = 'Exactly one field must be specified and be not null.';

public function isOutputable() : bool
{
return true;
}
}
10 changes: 10 additions & 0 deletions src/Typesystem/Exception/OneOfInputInvalidFields.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\Typesystem\Exception;

final class OneOfInputInvalidFields extends \Graphpinator\Typesystem\Exception\TypeError
{
public const MESSAGE = 'OneOf input type must have only nullable fields without default values.';
}
44 changes: 44 additions & 0 deletions src/Typesystem/Spec/OneOfDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types = 1);

namespace Graphpinator\Typesystem\Spec;

use \Graphpinator\Value\ArgumentValueSet;
use \Graphpinator\Value\InputValue;

#[\Graphpinator\Typesystem\Attribute\Description('Built-in oneOf directive')]
final class OneOfDirective extends \Graphpinator\Typesystem\Directive implements \Graphpinator\Typesystem\Location\InputObjectLocation
{
protected const NAME = 'oneOf';

public function validateInputUsage(\Graphpinator\Typesystem\InputType $inputType, ArgumentValueSet $arguments) : bool
{
foreach ($inputType->getArguments() as $argument) {

Check failure on line 17 in src/Typesystem/Spec/OneOfDirective.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Graphpinator\Typesystem\Spec\OneOfDirective::validateInputUsage() should return bool but return statement is missing.
if ($argument->getType() instanceof \Graphpinator\Typesystem\NotNullType ||
$argument->getDefaultValue() instanceof \Graphpinator\Value\ArgumentValue) {
throw new \Graphpinator\Typesystem\Exception\OneOfInputInvalidFields();
}
}
}

public function resolveInputObject(ArgumentValueSet $arguments, InputValue $inputValue) : void
{
$currentCount = 0;

foreach ($inputValue as $innerValue) {
\assert($innerValue instanceof \Graphpinator\Value\ArgumentValue);

if ($currentCount >= 1 || $innerValue->getValue() instanceof \Graphpinator\Value\NullValue) {
throw new \Graphpinator\Typesystem\Exception\OneOfDirectiveNotSatisfied();
}

++$currentCount;
}
}

protected function getFieldDefinition() : \Graphpinator\Typesystem\Argument\ArgumentSet
{
return new \Graphpinator\Typesystem\Argument\ArgumentSet([]);
}
}

0 comments on commit a93005e

Please sign in to comment.