drupal-graphql / drupal-graphql/graphql

[4.x] - Entity reference producer with a single value?

Open
#1,124 5 comments 0 reactions 0 assignees View on GitHub
4.x question
Dominant language
PHP
Stars
287
Forks
198
PR merge metrics
No merged PRs in 30d

Description

Hello!

I have a case where I have an entity with a reference to a media entity, which has a reference to an image entity which I would like to produce the URL for.

I expected to be able to get the referenced entity by just chaining `entity_reference` producers, something like this:
```
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_image')),
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_media_image')),
$builder->produce('image_url')
->map('entity', $builder->fromParent())
)
);
```
The problem I'm facing is that the first `entity_reference` producer always returns an array, even though I know in this case it's a singleton item, and that doesn't play nicely with the second `entity_reference` producer call. Could it be possible to map in a context value that tells the producer to only return the first item in the array? It would make use cases like this possible.

For now, I solved this by chaining `property_path` and `entity_load` for each reference but it feels like it's very verbose in comparison to the above example.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.