File tree 5 files changed +139
-0
lines changed
_files/Magento/TestModuleGraphQlQuery
testsuite/Magento/GraphQl/TestModule 5 files changed +139
-0
lines changed Original file line number Diff line number Diff line change 11
11
use Magento \Framework \GraphQl \Config \Element \Field ;
12
12
use Magento \Framework \GraphQl \Query \ResolverInterface ;
13
13
14
+ /**
15
+ * Resolver for Item
16
+ */
14
17
class Item implements ResolverInterface
15
18
{
16
19
/**
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Copyright © Magento, Inc. All rights reserved.
4
+ * See COPYING.txt for license details.
5
+ */
6
+ declare (strict_types=1 );
7
+
8
+ namespace Magento \TestModuleGraphQlQuery \Model \Resolver ;
9
+
10
+ use Magento \Framework \GraphQl \Config \Element \Field ;
11
+ use Magento \Framework \GraphQl \Query \ResolverInterface ;
12
+ use Magento \Framework \GraphQl \Schema \Type \ResolveInfo ;
13
+ use Magento \Sales \Model \ResourceModel \Order \CollectionFactoryInterface ;
14
+
15
+ /**
16
+ * Resolver for Union Type 1
17
+ */
18
+ class UnionType1 implements ResolverInterface
19
+ {
20
+ /**
21
+ * @var CollectionFactoryInterface
22
+ */
23
+ private $ collectionFactory ;
24
+
25
+ /**
26
+ * @param CollectionFactoryInterface $collectionFactory
27
+ */
28
+ public function __construct (
29
+ CollectionFactoryInterface $ collectionFactory
30
+ ) {
31
+ $ this ->collectionFactory = $ collectionFactory ;
32
+ }
33
+
34
+ /**
35
+ * @inheritDoc
36
+ */
37
+ public function resolve (
38
+ Field $ field ,
39
+ $ context ,
40
+ ResolveInfo $ info ,
41
+ array $ value = null ,
42
+ array $ args = null
43
+ ) {
44
+ return [
45
+ 'custom_name1 ' => 'custom_name1_value ' ,
46
+ 'custom_name2 ' => 'custom_name2_value ' ,
47
+ ];
48
+ }
49
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+ /**
3
+ * Copyright © Magento, Inc. All rights reserved.
4
+ * See COPYING.txt for license details.
5
+ */
6
+ declare (strict_types=1 );
7
+
8
+ namespace Magento \TestModuleGraphQlQuery \Model \Resolver ;
9
+
10
+ use Magento \Framework \GraphQl \Query \Resolver \TypeResolverInterface ;
11
+
12
+ /**
13
+ * Type Resolver for union
14
+ */
15
+ class UnionTypeResolver implements TypeResolverInterface
16
+ {
17
+ /**
18
+ * @inheritDoc
19
+ */
20
+ public function resolveType (array $ data ): string
21
+ {
22
+ if (!empty ($ data )) {
23
+ return 'TypeCustom1 ' ;
24
+ }
25
+ return '' ;
26
+ }
27
+
28
+ }
Original file line number Diff line number Diff line change 3
3
4
4
type Query {
5
5
testItem (id : Int ! ) : Item @resolver (class : " Magento\\ TestModuleGraphQlQuery\\ Model\\ Resolver\\ Item" )
6
+ testUnion : UnionType1 @resolver (class : " Magento\\ TestModuleGraphQlQuery\\ Model\\ Resolver\\ UnionType1" )
6
7
}
7
8
8
9
type Mutation {
@@ -18,3 +19,14 @@ type MutationItem {
18
19
item_id : Int
19
20
name : String
20
21
}
22
+
23
+ union UnionType1 @doc (description : " some kind of union" ) @typeResolver (class : " Magento\\ TestModuleGraphQlQuery\\ Model\\ Resolver\\ UnionTypeResolver" ) =
24
+ TypeCustom1 | TypeCustom2
25
+
26
+ type TypeCustom1 {
27
+ custom_name1 : String
28
+ }
29
+
30
+ type TypeCustom2 {
31
+ custom_name2 : String
32
+ }
Original file line number Diff line number Diff line change @@ -25,6 +25,15 @@ public function testQueryTestModuleReturnsResults()
25
25
item_id
26
26
name
27
27
}
28
+ testUnion {
29
+ __typename
30
+ ... on TypeCustom1 {
31
+ custom_name1
32
+ }
33
+ ... on TypeCustom2 {
34
+ custom_name2
35
+ }
36
+ }
28
37
}
29
38
QUERY ;
30
39
@@ -100,4 +109,42 @@ public function testQueryViaGetRequestWithVariablesReturnsResults()
100
109
101
110
$ this ->assertArrayHasKey ('testItem ' , $ response );
102
111
}
112
+
113
+ public function testQueryTestUnionResults ()
114
+ {
115
+ $ id = 1 ;
116
+
117
+ $ query = <<<QUERY
118
+ {
119
+ testItem(id: {$ id })
120
+ {
121
+ item_id
122
+ name
123
+ }
124
+ testUnion {
125
+ __typename
126
+ ... on TypeCustom1 {
127
+ custom_name1
128
+ }
129
+ ... on TypeCustom2 {
130
+ custom_name2
131
+ }
132
+ }
133
+ }
134
+ QUERY ;
135
+
136
+ $ response = $ this ->graphQlQuery ($ query );
137
+ $ this ->assertArrayHasKey ('testItem ' , $ response );
138
+ $ testItem = $ response ['testItem ' ];
139
+ $ this ->assertArrayHasKey ('item_id ' , $ testItem );
140
+ $ this ->assertArrayHasKey ('name ' , $ testItem );
141
+ $ this ->assertEquals (1 , $ testItem ['item_id ' ]);
142
+ $ this ->assertEquals ('itemName ' , $ testItem ['name ' ]);
143
+
144
+ $ this ->assertArrayHasKey ('testUnion ' , $ response );
145
+ $ testUnion = $ response ['testUnion ' ];
146
+ $ this ->assertArrayHasKey ('custom_name1 ' , $ testUnion );
147
+ $ this ->assertEquals ('custom_name1_value ' , $ testUnion ['custom_name1 ' ]);
148
+ $ this ->assertArrayNotHasKey ('custom_name2 ' , $ testUnion );
149
+ }
103
150
}
You can’t perform that action at this time.
0 commit comments