Skip to content

Navigation Menu

Sign in
Sign up

Auto-resolving with ids #1046

vlydev started this conversation in General
Discussion options

Hi.

I have next:

class PersonQuery
{
 #[Query]
 public function inventory(): InventoryResponse
 {
 $response = new InventoryResponse();
 $personInventories = $this->personInventoryRepository->findByPerson(1);
 foreach ($personInventories as $personInventory) {
 $item = new InventoryItem();
 $item->id = $personInventory->getId()->toString();
 $item->updatedAt = $personInventory->getUpdatedAt();
 $description = $this->descriptionRepository->findOneById($personInventory->getDescriptionId());
 if ($itemEntity) {
 $item->description = new DescriptionItem();
 $item->description->id =$description->getId();
 $item->description->name = $description->getName();
 $item->description->updatedAt = $description->getUpdatedAt();
 }
 $response->items[] = $item;
 }
 return $response;
 }
}
#[Type]
class InventoryResponse {
 #[Field]
 /**
 * @var InventoryItem[]
 */
 public array $items = [];
}
#[Type(name: "InventoryItem")]
class InventoryItem
{
 #[Field]
 public string $id;
 #[Field(type: "DateTime!")]
 public \DateTimeImmutable $updatedAt;
 #[Field(type: "DescriptionItem"]
 public ?SteamItem $description;
}
#[Type(name: "DescriptionItem")]
class DescriptionItem
{
 #[Field]
 public string $Id;
 #[Field]
 public string $name;
 #[Field(type: "DateTime!")]
 public \DateTimeImmutable $updatedAt;
}

I want to fill only string ids in inventory query. Then resolvers or something else do dirty work, fetch from DB and map from Doctrine entities to related GraphQL types. Also it must do it for all nodes. Its mean if client ask InventoryResponse -> InventoryItem -> DescriptionItem, then its must resolve it recursively

Is it possible via php attributes only?

You must be logged in to vote

Replies: 1 comment

Comment options

Hi @vladdnepr!
You can use custom scalar to achieve what you want.
Take a look here: https://github.com/sparklink-pro/GraphQLToolsBundle/blob/master/src/GraphQL/Doctrine/EntityIdType.php
The idea is to have a special scalar for each entity. For example InventoryItemId to represent InventoryItem.
And the scalar will do the fetch and stuff.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /