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

Handle fields omitted through @skip or @include #79

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
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 @@ -207,4 +208,13 @@ public function testNestedObjectNull(): void
self::assertNotNull($object);
self::assertNull($object->nested);
}

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