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

Support Array fieldtype in GraphQL #3972

Merged
Merged
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
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,
]);
}
}