Skip to content

Commit ca0823a

Browse files
author
Andrii Sudiev
committed
add Promise type mapping docs
1 parent b6c54c2 commit ca0823a

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

website/docs/type-mapping.mdx

+41
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,47 @@ query {
330330
}
331331
```
332332

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+
333374
## More scalar types
334375

335376
<small>Available in GraphQLite 4.0+</small>

0 commit comments

Comments
 (0)