forked from thecodingmachine/graphqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutationInterfaceFreezeTest.php
More file actions
141 lines (124 loc) · 3.94 KB
/
MutationInterfaceFreezeTest.php
File metadata and controls
141 lines (124 loc) · 3.94 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
<?php
declare(strict_types=1);
namespace TheCodingMachine\GraphQLite\Integration;
use GraphQL\Error\DebugFlag;
use GraphQL\GraphQL;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Psr16Cache;
use TheCodingMachine\GraphQLite\Containers\BasicAutoWiringContainer;
use TheCodingMachine\GraphQLite\Containers\EmptyContainer;
use TheCodingMachine\GraphQLite\Schema;
use TheCodingMachine\GraphQLite\SchemaFactory;
/**
* Regression test for issue #308: MutableInterfaceType not frozen when used
* exclusively as a mutation return type.
*
* @see https://github.com/thecodingmachine/graphqlite/issues/308
*/
class MutationInterfaceFreezeTest extends TestCase
{
private Schema $schema;
public function setUp(): void
{
$container = new BasicAutoWiringContainer(new EmptyContainer());
$schemaFactory = new SchemaFactory(new Psr16Cache(new ArrayAdapter()), $container);
$schemaFactory->addNamespace('TheCodingMachine\\GraphQLite\\Fixtures\\MutationInterfaceFreeze');
$this->schema = $schemaFactory->createSchema();
}
/**
* Schema validation triggers getTypeMap() which traverses all types.
* Before the fix, this would throw:
* "You must freeze() a MutableObjectType before fetching its fields."
*/
public function testSchemaValidationDoesNotThrowForMutationOnlyInterface(): void
{
$this->schema->assertValid();
$this->addToAssertionCount(1);
}
/**
* Executing a mutation that returns an interface type should work
* without any query also returning that interface.
*/
public function testMutationReturningInterfaceCanBeExecuted(): void
{
$queryString = '
mutation {
mutateResult {
message
}
}
';
$result = GraphQL::executeQuery(
$this->schema,
$queryString,
);
$this->assertSame(
['mutateResult' => ['message' => 'success']],
$result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS)['data'],
);
}
/**
* Inline fragments on an interface type in a mutation should work.
* Before the fix, the OverlappingFieldsCanBeMerged validator would
* access the unfrozen MutableInterfaceType and crash.
*/
public function testMutationWithInlineFragmentOnInterface(): void
{
$queryString = '
mutation {
mutateResult {
... on ResultInterface {
message
}
... on ConcreteResult {
message
extra
}
}
}
';
$result = GraphQL::executeQuery(
$this->schema,
$queryString,
);
$this->assertSame(
[
'mutateResult' => [
'message' => 'success',
'extra' => 'extra',
],
],
$result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS)['data'],
);
}
/**
* Introspection must include the mutation return interface type
* and it must be resolvable without errors.
*/
public function testIntrospectionIncludesMutationInterfaceType(): void
{
$queryString = '
{
__type(name: "ResultInterface") {
kind
name
fields {
name
}
}
}
';
$result = GraphQL::executeQuery(
$this->schema,
$queryString,
);
$data = $result->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS)['data'];
$this->assertSame('INTERFACE', $data['__type']['kind']);
$this->assertSame('ResultInterface', $data['__type']['name']);
$this->assertContains(
['name' => 'message'],
$data['__type']['fields'],
);
}
}