drupal-graphql / drupal-graphql/graphql

Best practise in handling Media entities

Open
#1,150 2 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

Using the documentation, the following seems to be provided (and working) to get a url of an file/image:

```
$registry->addFieldResolver('Paragraph', 'media',
builder->compose(
// Load the file object from the field.
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:paragraph'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_image.target_id')),
$builder->produce('entity_load')
->map('type', $builder->fromValue('file'))
->map('id', $builder->fromParent()),
// Load the image style derivative of the file.
$builder->produce('image_derivative')
->map('entity', $builder->fromParent())
->map('style', $builder->fromValue('large')),
// Retrieve the url of the generated image.
$builder->produce('image_style_url')
->map('derivative', $builder->fromParent()),
)
);
```

But what I intend to do is create a separate schema for a media file as follows:
```
type Paragraph {
id: Int!
type: String!
text: String
media: Media
}

type Media{
id: Int!
file: String
alt: String
}
```

Now, what would be the best way to handle the Media further on. Loading it in the following way gives no result:
```
$registry->addFieldResolver('Paragraph', 'media',
$builder->compose(
// Load the file object from the field.
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:paragraph'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_image.target_id')),
$builder->produce('entity_load')
->map('type', $builder->fromValue('file'))
->map('id', $builder->fromParent()),
)
);

// Load in the paragraph image(s).
$registry->addFieldResolver('Media', 'id',
$builder->produce('entity_id')
->map('entity', $builder->fromParent())
);
```

This is indeed using paragraphs, but I don't think this is the issue here.

Any help would be appreciated!

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.