diff --git a/src/Fieldtypes/Group.php b/src/Fieldtypes/Group.php index 33a997e8a7..e86e1e7130 100644 --- a/src/Fieldtypes/Group.php +++ b/src/Fieldtypes/Group.php @@ -2,8 +2,12 @@ namespace Statamic\Fieldtypes; +use Statamic\Facades\GraphQL; use Statamic\Fields\Fields; use Statamic\Fields\Fieldtype; +use Statamic\Fields\Values; +use Statamic\GraphQL\Types\GroupType; +use Statamic\Support\Str; class Group extends Fieldtype { @@ -108,7 +112,7 @@ private function performAugmentation($value, $shallow) { $method = $shallow ? 'shallowAugment' : 'augment'; - return $this->fields()->addValues($value ?? [])->{$method}()->values()->all(); + return new Values($this->fields()->addValues($value ?? [])->{$method}()->values()->all()); } public function preProcessValidatable($value) @@ -122,4 +126,25 @@ public function preProcessValidatable($value) ->all(), ); } + + public function toGqlType() + { + return GraphQL::type($this->gqlItemTypeName()); + } + + public function addGqlTypes() + { + GraphQL::addType(new GroupType($this, $this->gqlItemTypeName())); + + $this->fields()->all()->each(function ($field) { + $field->fieldtype()->addGqlTypes(); + }); + } + + private function gqlItemTypeName() + { + return 'Group_'.collect($this->field->handlePath())->map(function ($part) { + return Str::studly($part); + })->join('_'); + } } diff --git a/src/GraphQL/Types/GroupType.php b/src/GraphQL/Types/GroupType.php new file mode 100644 index 0000000000..8f391f922f --- /dev/null +++ b/src/GraphQL/Types/GroupType.php @@ -0,0 +1,33 @@ +fieldtype = $fieldtype; + $this->attributes['name'] = $name; + } + + public function fields(): array + { + $fields = $this->fieldtype->fields()->toGql(); + + return $fields + ->map(function ($field) use ($fields) { + $field['resolve'] = function ($row, $args, $context, $info) use ($fields) { + return ($resolver = $fields[$info->fieldName]['resolve'] ?? null) + ? $resolver($row, $args, $context, $info) + : $row[$info->fieldName]; + }; + + return $field; + }) + ->all(); + } +}