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

[5.x] Support Sections in form GraphQL queries #11466

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
2 changes: 2 additions & 0 deletions src/GraphQL/TypeRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Statamic\GraphQL\Types\NavType;
use Statamic\GraphQL\Types\PageInterface;
use Statamic\GraphQL\Types\RoleType;
use Statamic\GraphQL\Types\SectionType;
use Statamic\GraphQL\Types\SiteType;
use Statamic\GraphQL\Types\TableRowType;
use Statamic\GraphQL\Types\TaxonomyType;
Expand Down Expand Up @@ -62,6 +63,7 @@ public function register()
GraphQL::addType(AssetInterface::class);
GraphQL::addType(GlobalSetInterface::class);
GraphQL::addType(FieldType::class);
GraphQL::addType(SectionType::class);

PageInterface::addTypes();
EntryInterface::addTypes();
Expand Down
6 changes: 6 additions & 0 deletions src/GraphQL/Types/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public function fields(): array
return $form->blueprint()->fields()->validator()->rules();
},
],
'sections' => [
'type' => GraphQL::listOf(GraphQL::type(SectionType::NAME)),
'resolve' => function ($form, $args, $context, $info) {
return $form->blueprint()->tabs()->first()->sections()->all();
},
],
])->map(function (array $arr) {
$arr['resolve'] = $arr['resolve'] ?? $this->resolver();

Expand Down
38 changes: 38 additions & 0 deletions src/GraphQL/Types/SectionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Statamic\GraphQL\Types;

use Statamic\Facades\GraphQL;

class SectionType extends \Rebing\GraphQL\Support\Type
{
const NAME = 'Section';

protected $attributes = [
'name' => self::NAME,
];

public function fields(): array
{
return [
'display' => [
'type' => GraphQL::string(),
'resolve' => function ($section) {
return $section->display();
},
],
'instructions' => [
'type' => GraphQL::string(),
'resolve' => function ($section) {
return $section->instructions();
},
],
'fields' => [
'type' => GraphQL::listOf(GraphQL::type(FieldType::NAME)),
'resolve' => function ($section) {
return $section->fields()->all();
},
],
];
}
}
92 changes: 92 additions & 0 deletions tests/Feature/GraphQL/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,96 @@ public function it_queries_the_validation_rules()
],
]]);
}

#[Test]
public function it_queries_the_sections()
{
Form::make('contact')->title('Contact Us')->save();

$blueprint = Blueprint::makeFromFields([
'name' => [
'type' => 'text',
'display' => 'Your Name',
'instructions' => 'Enter your name',
'placeholder' => 'Type here...',
'invalid' => 'This isnt in the fieldtypes config fields so it shouldnt be output',
'width' => 50,
],
'subject' => ['type' => 'select', 'options' => ['disco' => 'Disco', 'house' => 'House']],
'message' => ['type' => 'textarea', 'width' => 33],
]);

// Set section display and instructions. You wouldn't really do this for a form blueprint,
// but this is just to test the section type which doesn't get tested anywhere else.
$contents = $blueprint->contents();
$contents['tabs']['main']['sections'][0]['display'] = 'My Section';
$contents['tabs']['main']['sections'][0]['instructions'] = 'The section instructions';
$blueprint->setContents($contents);

BlueprintRepository::shouldReceive('find')->with('forms.contact')->andReturn($blueprint);

$query = <<<'GQL'
{
form(handle: "contact") {
sections {
display
instructions
fields {
handle
type
display
instructions
width
config
}
}
}
}
GQL;

$this
->withoutExceptionHandling()
->post('/graphql', ['query' => $query])
->assertGqlOk()
->assertExactJson(['data' => [
'form' => [
'sections' => [
[
'display' => 'My Section',
'instructions' => 'The section instructions',
'fields' => [
[
'handle' => 'name',
'type' => 'text',
'display' => 'Your Name',
'instructions' => 'Enter your name',
'width' => 50,
'config' => [
'placeholder' => 'Type here...',
],
],
[
'handle' => 'subject',
'type' => 'select',
'display' => 'Subject',
'instructions' => null,
'width' => 100,
'config' => [
'options' => ['disco' => 'Disco', 'house' => 'House'],
],
],
[
'handle' => 'message',
'type' => 'textarea',
'display' => 'Message',
'instructions' => null,
'width' => 33,
'config' => [],
],
],
],
],
],
]]);
}
}
Loading