Skip to content

Commit

Permalink
Handle fields omitted through @skip or @include
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored and CRC-Mismatch committed May 30, 2023
1 parent 3c167f4 commit 17d6d74
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 0 deletions.
48 changes: 48 additions & 0 deletions examples/simple/expected/Operations/SkipNonNullable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Spawnia\Sailor\Simple\Operations;

/**
* @extends \Spawnia\Sailor\Operation<\Spawnia\Sailor\Simple\Operations\SkipNonNullable\SkipNonNullableResult>
*/
class SkipNonNullable extends \Spawnia\Sailor\Operation
{
/**
* @param bool $value
*/
public static function execute($value): SkipNonNullable\SkipNonNullableResult
{
return self::executeOperation(
$value,
);
}

protected static function converters(): array
{
static $converters;

return $converters ??= [
['value', new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\BooleanConverter)],
];
}

public static function document(): string
{
return /* @lang GraphQL */ 'query SkipNonNullable($value: Boolean!) {
__typename
nonNullable @skip(if: $value)
}';
}

public static function endpoint(): string
{
return 'simple';
}

public static function config(): string
{
return \Safe\realpath(__DIR__ . '/../../sailor.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Spawnia\Sailor\Simple\Operations\SkipNonNullable;

/**
* @property string $nonNullable
* @property string $__typename
*/
class SkipNonNullable extends \Spawnia\Sailor\ObjectLike
{
/**
* @param string $nonNullable
*/
public static function make($nonNullable): self
{
$instance = new self;

if ($nonNullable !== self::UNDEFINED) {
$instance->nonNullable = $nonNullable;
}
$instance->__typename = 'Query';

return $instance;
}

protected function converters(): array
{
static $converters;

return $converters ??= [
'nonNullable' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
'__typename' => new \Spawnia\Sailor\Convert\NonNullConverter(new \Spawnia\Sailor\Convert\StringConverter),
];
}

public static function endpoint(): string
{
return 'simple';
}

public static function config(): string
{
return \Safe\realpath(__DIR__ . '/../../../sailor.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Spawnia\Sailor\Simple\Operations\SkipNonNullable;

class SkipNonNullableErrorFreeResult extends \Spawnia\Sailor\ErrorFreeResult
{
public SkipNonNullable $data;

public static function endpoint(): string
{
return 'simple';
}

public static function config(): string
{
return \Safe\realpath(__DIR__ . '/../../../sailor.php');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Spawnia\Sailor\Simple\Operations\SkipNonNullable;

class SkipNonNullableResult extends \Spawnia\Sailor\Result
{
public ?SkipNonNullable $data = null;

protected function setData(\stdClass $data): void
{
$this->data = SkipNonNullable::fromStdClass($data);
}

/**
* Useful for instantiation of successful mocked results.
*
* @return static
*/
public static function fromData(SkipNonNullable $data): self
{
$instance = new static;
$instance->data = $data;

return $instance;
}

public function errorFree(): SkipNonNullableErrorFreeResult
{
return SkipNonNullableErrorFreeResult::fromResult($this);
}

public static function endpoint(): string
{
return 'simple';
}

public static function config(): string
{
return \Safe\realpath(__DIR__ . '/../../../sailor.php');
}
}
1 change: 1 addition & 0 deletions examples/simple/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type Query {
scalarWithArg(arg: String): ID
twoArgs(first: String, second: Int): ID
singleObject: SomeObject
nonNullable: String!
}

type SomeObject {
Expand Down
4 changes: 4 additions & 0 deletions examples/simple/src/clientDirectives.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@ query ClientDirectiveInlineFragmentQuery($value: Boolean!) {
twoArgs
}
}

query SkipNonNullable($value: Boolean!) {
nonNullable @skip(if: $value)
}
10 changes: 10 additions & 0 deletions tests/Integration/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Spawnia\Sailor\Simple\Operations\MyObjectNestedQuery\MyObjectNestedQueryResult;
use Spawnia\Sailor\Simple\Operations\MyScalarQuery;
use Spawnia\Sailor\Simple\Operations\MyScalarQuery\MyScalarQueryResult;
use Spawnia\Sailor\Simple\Operations\SkipNonNullable\SkipNonNullable;
use Spawnia\Sailor\Tests\TestCase;

final class SimpleTest extends TestCase
Expand Down Expand Up @@ -206,4 +207,13 @@ public function testNestedObjectNull(): void
self::assertNotNull($object);
self::assertNull($object->nested);
}

public function testSkipNonNullable(): void
{
SkipNonNullable::fromStdClass((object) [
'data' => (object) [
'__typename' => 'Query',
],
]);
}
}

0 comments on commit 17d6d74

Please sign in to comment.