forked from statamic/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomMutationTest.php
More file actions
142 lines (121 loc) · 3.81 KB
/
Copy pathCustomMutationTest.php
File metadata and controls
142 lines (121 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace Tests\Feature\GraphQL;
use GraphQL\Type\Definition\Type;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use Rebing\GraphQL\Support\Mutation;
use Statamic\Facades\GraphQL;
use Tests\TestCase;
#[Group('graphql')]
class CustomMutationTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
app()->instance('mutation-count', 0);
}
#[Test]
public function custom_mutation_does_not_yet_exist()
{
$this
->post('/graphql', ['query' => 'mutation { createItem(name: "test") }'])
->assertJson(['errors' => [[
'message' => 'Schema is not configured for mutations.',
]]]);
}
#[Test]
#[DefineEnvironment('addCustomMutationsThroughConfig')]
public function a_custom_mutation_can_be_added_to_the_default_schema_through_config()
{
$this
->post('/graphql', ['query' => 'mutation { createItem(name: "test") }'])
->assertGqlOk()
->assertExactJson(['data' => ['createItem' => 'Item created: test']]);
}
#[Test]
#[DefineEnvironment('addCustomMutationsThroughConfig')]
public function multiple_custom_mutations_can_be_added()
{
$this
->post('/graphql', ['query' => 'mutation { createItem(name: "first") }'])
->assertGqlOk()
->assertExactJson(['data' => ['createItem' => 'Item created: first']]);
$this
->post('/graphql', ['query' => 'mutation { updateItem(id: 1, name: "updated") }'])
->assertGqlOk()
->assertExactJson(['data' => ['updateItem' => 'Item 1 updated: updated']]);
}
#[Test]
#[DefineEnvironment('addCustomMutationsThroughConfig')]
public function mutations_are_not_cached()
{
$this
->post('/graphql', ['query' => 'mutation { createItem(name: "test") }'])
->assertGqlOk()
->assertExactJson(['data' => ['createItem' => 'Item created: test']]);
$this
->post('/graphql', ['query' => 'mutation { createItem(name: "test") }'])
->assertGqlOk()
->assertExactJson(['data' => ['createItem' => 'Item created: test']]);
$this->assertEquals(2, app('mutation-count'));
}
protected function addCustomMutationsThroughConfig($app)
{
$app['config']->set('statamic.graphql.mutations', [
CreateItemMutation::class,
UpdateItemMutation::class,
]);
}
}
class CreateItemMutation extends Mutation
{
protected $attributes = [
'name' => 'createItem',
];
public function type(): Type
{
return GraphQL::string();
}
public function args(): array
{
return [
'name' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The name of the item to create',
],
];
}
public function resolve($root, $args)
{
app()->instance('mutation-count', app('mutation-count') + 1);
return 'Item created: '.$args['name'];
}
}
class UpdateItemMutation extends Mutation
{
protected $attributes = [
'name' => 'updateItem',
];
public function type(): Type
{
return GraphQL::string();
}
public function args(): array
{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The ID of the item to update',
],
'name' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The new name of the item',
],
];
}
public function resolve($root, $args)
{
return "Item {$args['id']} updated: {$args['name']}";
}
}