From fb61586049bc3325eb85fb9f0573719aa190fb46 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Thu, 10 Nov 2022 09:40:32 +0100 Subject: [PATCH] Handle fields omitted through `@skip` or `@include` https://github.com/spawnia/sailor/issues/77 --- .../expected/Operations/SkipNonNullable.php | 48 +++++++++++++++++++ .../SkipNonNullable/SkipNonNullable.php | 47 ++++++++++++++++++ .../SkipNonNullableErrorFreeResult.php | 20 ++++++++ .../SkipNonNullable/SkipNonNullableResult.php | 43 +++++++++++++++++ examples/simple/schema.graphql | 1 + examples/simple/src/clientDirectives.graphql | 4 ++ tests/Integration/SimpleTest.php | 10 ++++ 7 files changed, 173 insertions(+) create mode 100644 examples/simple/expected/Operations/SkipNonNullable.php create mode 100644 examples/simple/expected/Operations/SkipNonNullable/SkipNonNullable.php create mode 100644 examples/simple/expected/Operations/SkipNonNullable/SkipNonNullableErrorFreeResult.php create mode 100644 examples/simple/expected/Operations/SkipNonNullable/SkipNonNullableResult.php diff --git a/examples/simple/expected/Operations/SkipNonNullable.php b/examples/simple/expected/Operations/SkipNonNullable.php new file mode 100644 index 00000000..67d73e3b --- /dev/null +++ b/examples/simple/expected/Operations/SkipNonNullable.php @@ -0,0 +1,48 @@ + + */ +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'); + } +} diff --git a/examples/simple/expected/Operations/SkipNonNullable/SkipNonNullable.php b/examples/simple/expected/Operations/SkipNonNullable/SkipNonNullable.php new file mode 100644 index 00000000..dd46928d --- /dev/null +++ b/examples/simple/expected/Operations/SkipNonNullable/SkipNonNullable.php @@ -0,0 +1,47 @@ +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'); + } +} diff --git a/examples/simple/expected/Operations/SkipNonNullable/SkipNonNullableErrorFreeResult.php b/examples/simple/expected/Operations/SkipNonNullable/SkipNonNullableErrorFreeResult.php new file mode 100644 index 00000000..7aeecc32 --- /dev/null +++ b/examples/simple/expected/Operations/SkipNonNullable/SkipNonNullableErrorFreeResult.php @@ -0,0 +1,20 @@ +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'); + } +} diff --git a/examples/simple/schema.graphql b/examples/simple/schema.graphql index 3bc15a23..3c420a62 100644 --- a/examples/simple/schema.graphql +++ b/examples/simple/schema.graphql @@ -2,6 +2,7 @@ type Query { scalarWithArg(arg: String): ID twoArgs(first: String, second: Int): ID singleObject: SomeObject + nonNullable: String! } type SomeObject { diff --git a/examples/simple/src/clientDirectives.graphql b/examples/simple/src/clientDirectives.graphql index 00d4b21e..3e1218ef 100644 --- a/examples/simple/src/clientDirectives.graphql +++ b/examples/simple/src/clientDirectives.graphql @@ -16,3 +16,7 @@ query ClientDirectiveInlineFragmentQuery($value: Boolean!) { twoArgs } } + +query SkipNonNullable($value: Boolean!) { + nonNullable @skip(if: $value) +} diff --git a/tests/Integration/SimpleTest.php b/tests/Integration/SimpleTest.php index 0a967a13..156f6929 100644 --- a/tests/Integration/SimpleTest.php +++ b/tests/Integration/SimpleTest.php @@ -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 @@ -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', + ], + ]); + } }