File tree 2 files changed +21
-17
lines changed
tests/Fixtures/Integration/Models
2 files changed +21
-17
lines changed Original file line number Diff line number Diff line change @@ -74,15 +74,7 @@ public function __construct(
74
74
$ callResolver = function (...$ args ) use ($ originalResolver , $ source , $ resolver ) {
75
75
$ result = $ resolver ($ source , ...$ args );
76
76
77
- try {
78
- $ this ->assertReturnType ($ result );
79
- } catch (TypeMismatchRuntimeException $ e ) {
80
- $ e ->addInfo ($ this ->name , $ originalResolver ->toString ());
81
-
82
- throw $ e ;
83
- }
84
-
85
- return $ result ;
77
+ return $ this ->unwrapReturnType ($ result , $ originalResolver );
86
78
};
87
79
88
80
// GraphQL allows deferring resolving the field's value using promises, i.e. they call the resolve
@@ -98,6 +90,21 @@ public function __construct(
98
90
parent ::__construct ($ config );
99
91
}
100
92
93
+ private function unwrapReturnType (mixed $ result , ResolverInterface $ originalResolver ): mixed
94
+ {
95
+ if ($ result instanceof SyncPromise) {
96
+ return $ result ->then (fn ($ resolvedValue ) => $ this ->unwrapReturnType ($ resolvedValue , $ originalResolver ));
97
+ }
98
+ try {
99
+ $ this ->assertReturnType ($ result );
100
+ } catch (TypeMismatchRuntimeException $ e ) {
101
+ $ e ->addInfo ($ this ->name , $ originalResolver ->toString ());
102
+
103
+ throw $ e ;
104
+ }
105
+ return $ result ;
106
+ }
107
+
101
108
/**
102
109
* This method checks the returned value of the resolver to be sure it matches the documented return type.
103
110
* We are sure the returned value is of the correct type... except if the return type is type-hinted as an array.
Original file line number Diff line number Diff line change 4
4
5
5
namespace TheCodingMachine \GraphQLite \Fixtures \Integration \Models ;
6
6
7
+ use GraphQL \Deferred ;
7
8
use TheCodingMachine \GraphQLite \Annotations \Field ;
8
9
use TheCodingMachine \GraphQLite \Annotations \Prefetch ;
9
10
use TheCodingMachine \GraphQLite \Annotations \Type ;
@@ -35,17 +36,13 @@ public function getPosts(
35
36
return $ prefetchedPosts [$ this ->id ];
36
37
}
37
38
38
- /**
39
- * @param Blog[][] $prefetchedSubBlogs
40
- *
41
- * @return Blog[]
42
- */
43
- #[Field]
39
+ /** @param Blog[][] $prefetchedSubBlogs */
40
+ #[Field(outputType: '[Blog!]! ' )]
44
41
public function getSubBlogs (
45
42
#[Prefetch('prefetchSubBlogs ' )]
46
43
array $ prefetchedSubBlogs ,
47
- ): array {
48
- return $ prefetchedSubBlogs [$ this ->id ];
44
+ ): Deferred {
45
+ return new Deferred ( fn () => $ prefetchedSubBlogs [$ this ->id ]) ;
49
46
}
50
47
51
48
/**
You can’t perform that action at this time.
0 commit comments