File tree 1 file changed +41
-0
lines changed
1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -330,6 +330,47 @@ query {
330
330
}
331
331
```
332
332
333
+ ## Promise mapping
334
+
335
+ GraphQL includes a native \GraphQL\Deferred type.
336
+ You can map the return type by adding a detailed ` @return ` statement in the PHPDoc.
337
+ An alternative to the ` @return ` statement is using ` #[Field(outputType: SomeGQLType)] ` .
338
+
339
+ All the previously mentioned mappings work with Promises, except when a return type is explicitly declared
340
+ in the method signature.
341
+
342
+ This allows you to use \Overblog\DataLoader\DataLoader as an alternative
343
+ for resolving N+1 query issues and caching intermediate results.
344
+
345
+ ``` php
346
+ #[Type]
347
+ class Product
348
+ {
349
+ // ...
350
+
351
+ /**
352
+ * @return string
353
+ */
354
+ #[Field]
355
+ public function getName(): Deferred
356
+ {
357
+ return new Deferred(fn() => $this->name);
358
+ }
359
+
360
+ #[Field(outputType: "Float")]
361
+ public function getPrice(): Deferred
362
+ {
363
+ return new Deferred(fn() => $this->price);
364
+ }
365
+
366
+ #[Field(outputType: "[String!]!")]
367
+ public function getCategories(#[Autowire('categoryDataLoader')] DataLoader $categoryDataLoader): SyncPromise
368
+ {
369
+ return $categoryDataLoader->load($this->id)->adoptedPromise;
370
+ }
371
+ }
372
+ ```
373
+
333
374
## More scalar types
334
375
335
376
<small >Available in GraphQLite 4.0+</small >
You can’t perform that action at this time.
0 commit comments