File tree Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Expand file tree Collapse file tree 3 files changed +45
-1
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ public function serialize($value)
43
43
if ($ value === null ) {
44
44
return 'null ' ;
45
45
}
46
- if (!is_scalar ($ value )) {
46
+ if (!is_scalar ($ value ) && (! is_object ( $ value ) || ! method_exists ( $ value , ' __toString ' )) ) {
47
47
throw new InvariantViolation ("ID type cannot represent non scalar value: " . Utils::printSafe ($ value ));
48
48
}
49
49
return (string ) $ value ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace GraphQL \Tests \Type ;
3
+
4
+ class ObjectIdStub
5
+ {
6
+ /**
7
+ * @var int
8
+ */
9
+ private $ id ;
10
+
11
+ /**
12
+ * @param int $id
13
+ */
14
+ public function __construct ($ id )
15
+ {
16
+ $ this ->id = $ id ;
17
+ }
18
+
19
+ public function __toString ()
20
+ {
21
+ return (string ) $ this ->id ;
22
+ }
23
+ }
Original file line number Diff line number Diff line change @@ -178,4 +178,25 @@ public function testSerializesOutputBoolean()
178
178
179
179
// TODO: how should it behave on '0'?
180
180
}
181
+
182
+ public function testSerializesOutputID ()
183
+ {
184
+ $ idType = Type::id ();
185
+
186
+ $ this ->assertSame ('string ' , $ idType ->serialize ('string ' ));
187
+ $ this ->assertSame ('' , $ idType ->serialize ('' ));
188
+ $ this ->assertSame ('1 ' , $ idType ->serialize ('1 ' ));
189
+ $ this ->assertSame ('1 ' , $ idType ->serialize (1 ));
190
+ $ this ->assertSame ('0 ' , $ idType ->serialize (0 ));
191
+ $ this ->assertSame ('true ' , $ idType ->serialize (true ));
192
+ $ this ->assertSame ('false ' , $ idType ->serialize (false ));
193
+ $ this ->assertSame ('2 ' , $ idType ->serialize (new ObjectIdStub (2 )));
194
+
195
+ try {
196
+ $ idType ->serialize (new \stdClass ());
197
+ $ this ->fail ('Expected exception was not thrown ' );
198
+ } catch (InvariantViolation $ e ) {
199
+ $ this ->assertEquals ('ID type cannot represent non scalar value: instance of stdClass ' , $ e ->getMessage ());
200
+ }
201
+ }
181
202
}
You can’t perform that action at this time.
0 commit comments