1+ <?php
2+ /**
3+ * Created by PhpStorm.
4+ * User: hunguyen
5+ * Date: 20/09/2018
6+ * Time: 10.55
7+ */
8+
9+ namespace Digia \GraphQL \Test \Functional \Schema ;
10+
11+
12+ use Digia \GraphQL \Test \TestCase ;
13+ use function Digia \GraphQL \buildSchema ;
14+ use function Digia \GraphQL \graphql ;
15+
16+ class SchemaBuilderTest extends TestCase
17+ {
18+ /**
19+ * @throws \Digia\GraphQL\Error\InvariantException
20+ */
21+ public function testSpecifyingInterfaceTypeUsingTypeNameMetaFieldDefinition ()
22+ {
23+ $ source = '
24+ type Query {
25+ characters: [Character]
26+ }
27+
28+ interface Character {
29+ name: String!
30+ }
31+
32+ type Human implements Character {
33+ name: String!
34+ totalCredits: Int
35+ }
36+
37+ type Droid implements Character {
38+ name: String!
39+ primaryFunction: String
40+ }
41+ ' ;
42+
43+ /** @noinspection PhpUnhandledExceptionInspection */
44+ $ schema = buildSchema ($ source );
45+
46+ $ query = '
47+ {
48+ characters {
49+ name
50+ ... on Human {
51+ totalCredits
52+ }
53+ ... on Droid {
54+ primaryFunction
55+ }
56+ }
57+ }
58+ ' ;
59+
60+ $ rootValue = [
61+ 'characters ' => [
62+ [
63+ 'name ' => 'Han Solo ' ,
64+ 'totalCredits ' => 10 ,
65+ '__typename ' => 'Human ' ,
66+ ],
67+ [
68+ 'name ' => 'R2-D2 ' ,
69+ 'primaryFunction ' => 'Astromech ' ,
70+ '__typename ' => 'Droid ' ,
71+ ],
72+ ],
73+ ];
74+
75+ $ result = graphql ($ schema , $ query , $ rootValue );
76+
77+ $ this ->assertEquals ([
78+ 'data ' => [
79+ 'characters ' => [
80+ [
81+ 'name ' => 'Han Solo ' ,
82+ 'totalCredits ' => 10 ,
83+ ],
84+ [
85+ 'name ' => 'R2-D2 ' ,
86+ 'primaryFunction ' => 'Astromech ' ,
87+ ],
88+ ],
89+ ],
90+ ], $ result );
91+ }
92+
93+ /**
94+ * @throws \Digia\GraphQL\Error\InvariantException
95+ */
96+ public function testSpecifyingUnionTypeUsingTypeNameMetaFieldDefinition ()
97+ {
98+ $ source = '
99+ type Query {
100+ fruits: [Fruit]
101+ }
102+
103+ union Fruit = Apple | Banana
104+
105+ type Apple {
106+ color: String
107+ }
108+
109+ type Banana {
110+ length: Int
111+ }
112+ ' ;
113+
114+ /** @noinspection PhpUnhandledExceptionInspection */
115+ $ schema = buildSchema ($ source );
116+
117+ $ query = '
118+ {
119+ fruits {
120+ ... on Apple {
121+ color
122+ }
123+ ... on Banana {
124+ length
125+ }
126+ }
127+ }
128+ ' ;
129+
130+ $ rootValue = [
131+ 'fruits ' => [
132+ [
133+ 'color ' => 'green ' ,
134+ '__typename ' => 'Apple ' ,
135+ ],
136+ [
137+ 'length ' => 5 ,
138+ '__typename ' => 'Banana ' ,
139+ ],
140+ ],
141+ ];
142+
143+ $ result = graphql ($ schema , $ query , $ rootValue );
144+
145+ $ this ->assertEquals ([
146+ 'data ' => [
147+ 'fruits ' => [
148+ [
149+ 'color ' => 'green ' ,
150+ ],
151+ [
152+ 'length ' => 5 ,
153+ ],
154+ ],
155+ ],
156+ ], $ result );
157+ }
158+ }
0 commit comments