Skip to content

Commit

Permalink
Support Array fieldtype in GraphQL (#3972)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurperton authored Jul 14, 2021
1 parent f0ac7c6 commit 63c6f26
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Fieldtypes/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Statamic\Fieldtypes;

use Statamic\Facades\GraphQL;
use Statamic\Fields\Fieldtype;
use Statamic\GraphQL\Types\ArrayType;

class Arr extends Fieldtype
{
Expand Down Expand Up @@ -66,4 +68,9 @@ protected function blankKeyed()
})
->all();
}

public function toGqlType()
{
return GraphQL::type(ArrayType::NAME);
}
}
2 changes: 2 additions & 0 deletions src/GraphQL/TypeRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -33,6 +34,7 @@ public function register()
return;
}

GraphQL::addType(ArrayType::class);
GraphQL::addType(JsonArgument::class);
GraphQL::addType(DateRangeType::class);
GraphQL::addType(SiteType::class);
Expand Down
33 changes: 33 additions & 0 deletions src/GraphQL/Types/ArrayType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Statamic\GraphQL\Types;

use GraphQL\Language\AST\Node;
use GraphQL\Type\Definition\ScalarType;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Contracts\TypeConvertible;

class ArrayType extends ScalarType implements TypeConvertible
{
const NAME = 'Array';

public function serialize($value)
{
return $value;
}

public function parseValue($value)
{
return $value;
}

public function parseLiteral(Node $valueNode, ?array $variables = null)
{
return $valueNode->value;
}

public function toType(): Type
{
return new static();
}
}
45 changes: 45 additions & 0 deletions tests/Feature/GraphQL/Fieldtypes/ArrFieldtypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Tests\Feature\GraphQL\Fieldtypes;

/** @group graphql */
class ArrFieldtypeTest extends FieldtypeTestCase
{
/** @test */
public function it_gets_arrays()
{
$keyedConfig = ['type' => '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,
]);
}
}

0 comments on commit 63c6f26

Please sign in to comment.