From 7a9dc222dd193d310274fef369b7c997462ad721 Mon Sep 17 00:00:00 2001 From: Arthur Perton Date: Thu, 8 Jul 2021 11:47:50 +0200 Subject: [PATCH 1/2] Support Array fieldtype in GraphQL --- src/Fieldtypes/Arr.php | 7 +++++++ src/GraphQL/TypeRegistrar.php | 2 ++ src/GraphQL/Types/ArrayType.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/GraphQL/Types/ArrayType.php diff --git a/src/Fieldtypes/Arr.php b/src/Fieldtypes/Arr.php index 67fe47f682..38d34a4f8b 100644 --- a/src/Fieldtypes/Arr.php +++ b/src/Fieldtypes/Arr.php @@ -2,7 +2,9 @@ namespace Statamic\Fieldtypes; +use Statamic\Facades\GraphQL; use Statamic\Fields\Fieldtype; +use Statamic\GraphQL\Types\ArrayType; class Arr extends Fieldtype { @@ -66,4 +68,9 @@ protected function blankKeyed() }) ->all(); } + + public function toGqlType() + { + return GraphQL::type(ArrayType::NAME); + } } diff --git a/src/GraphQL/TypeRegistrar.php b/src/GraphQL/TypeRegistrar.php index 5245887837..e3d3f7b18f 100644 --- a/src/GraphQL/TypeRegistrar.php +++ b/src/GraphQL/TypeRegistrar.php @@ -3,6 +3,7 @@ namespace Statamic\GraphQL; use Statamic\Facades\GraphQL; +use Statamic\GraphQL\Types\ArrayType; use Statamic\GraphQL\Types\AssetContainerType; use Statamic\GraphQL\Types\AssetInterface; use Statamic\GraphQL\Types\CollectionStructureType; @@ -33,6 +34,7 @@ public function register() return; } + GraphQL::addType(ArrayType::class); GraphQL::addType(JsonArgument::class); GraphQL::addType(DateRangeType::class); GraphQL::addType(SiteType::class); diff --git a/src/GraphQL/Types/ArrayType.php b/src/GraphQL/Types/ArrayType.php new file mode 100644 index 0000000000..cdf8a13d3a --- /dev/null +++ b/src/GraphQL/Types/ArrayType.php @@ -0,0 +1,33 @@ +value; + } + + public function toType(): Type + { + return new static(); + } +} From f3c11f5d5a3769d478d9cb831e9bc9a019231677 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 14 Jul 2021 15:37:13 -0400 Subject: [PATCH 2/2] test --- .../GraphQL/Fieldtypes/ArrFieldtypeTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/Feature/GraphQL/Fieldtypes/ArrFieldtypeTest.php diff --git a/tests/Feature/GraphQL/Fieldtypes/ArrFieldtypeTest.php b/tests/Feature/GraphQL/Fieldtypes/ArrFieldtypeTest.php new file mode 100644 index 0000000000..1d880b83b8 --- /dev/null +++ b/tests/Feature/GraphQL/Fieldtypes/ArrFieldtypeTest.php @@ -0,0 +1,45 @@ + 'array', 'keys' => ['foo' => 'Foo', 'bar' => 'Bar']]; + $dynamicConfig = ['type' => 'array']; + + $this->createEntryWithFields([ + 'keyed' => [ + 'value' => ['foo' => 'bar', 'baz' => 'qux'], + 'field' => $keyedConfig, + ], + 'keyed_incomplete' => [ + 'value' => ['foo' => 'bar'], + 'field' => $keyedConfig, + ], + 'keyed_undefined' => [ + 'value' => null, + 'field' => $keyedConfig, + ], + 'dynamic' => [ + 'value' => ['alfa' => 'bravo', 'charlie' => 'delta'], + 'field' => $dynamicConfig, + ], + 'dynamic_undefined' => [ + 'value' => null, + 'field' => $dynamicConfig, + ], + ]); + + $this->assertGqlEntryHas('keyed, keyed_incomplete, keyed_undefined, dynamic, dynamic_undefined', [ + 'keyed' => ['foo' => 'bar', 'baz' => 'qux'], + 'keyed_incomplete' => ['foo' => 'bar'], + 'keyed_undefined' => null, + 'dynamic' => ['alfa' => 'bravo', 'charlie' => 'delta'], + 'dynamic_undefined' => null, + ]); + } +}