|
| 1 | +The following examples should help accessing images and their derivatives based on image styles: |
| 2 | + |
| 3 | +## Add the schema declaration |
| 4 | + |
| 5 | +```gql |
| 6 | +type Article implements NodeInterface { |
| 7 | +... |
| 8 | + image_url: String |
| 9 | + image_alt: String |
| 10 | + image_thumbnail: Image |
| 11 | + image_large: Image |
| 12 | +... |
| 13 | +} |
| 14 | + |
| 15 | +type Image { |
| 16 | + url: String |
| 17 | + width: Int |
| 18 | + height: Int |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +## Adding resolvers |
| 23 | + |
| 24 | +```php |
| 25 | + $registry->addFieldResolver('Article', 'image_url', |
| 26 | + $builder->compose( |
| 27 | + $builder->produce('property_path') |
| 28 | + ->map('type', $builder->fromValue('entity:file')) |
| 29 | + ->map('value', $builder->fromParent()) |
| 30 | + ->map('path', $builder->fromValue('field_image.entity')), |
| 31 | + $builder->produce('image_url') |
| 32 | + ->map('entity', $builder->fromParent()) |
| 33 | + ) |
| 34 | + ); |
| 35 | + |
| 36 | + $registry->addFieldResolver('Article', 'image_alt', |
| 37 | + $builder->produce('property_path') |
| 38 | + ->map('type', $builder->fromValue('entity:node')) |
| 39 | + ->map('value', $builder->fromParent()) |
| 40 | + ->map('path', $builder->fromValue('field_image.alt')) |
| 41 | + ); |
| 42 | + |
| 43 | + $registry->addFieldResolver('Article', 'image_thumbnail', |
| 44 | + $builder->compose( |
| 45 | + $builder->produce('property_path') |
| 46 | + ->map('type', $builder->fromValue('entity:file')) |
| 47 | + ->map('value', $builder->fromParent()) |
| 48 | + ->map('path', $builder->fromValue('field_image.entity')), |
| 49 | + $builder->produce('image_derivative') |
| 50 | + ->map('entity', $builder->fromParent()) |
| 51 | + ->map('style', $builder->fromValue('thumbnail')) |
| 52 | + ) |
| 53 | + ); |
| 54 | + |
| 55 | + $registry->addFieldResolver('ProductDetailPage', 'image_large', |
| 56 | + $builder->compose( |
| 57 | + $builder->produce('property_path') |
| 58 | + ->map('type', $builder->fromValue('entity:file')) |
| 59 | + ->map('value', $builder->fromParent()) |
| 60 | + ->map('path', $builder->fromValue('field_image.entity')), |
| 61 | + $builder->produce('image_derivative') |
| 62 | + ->map('entity', $builder->fromParent()) |
| 63 | + ->map('style', $builder->fromValue('large')) |
| 64 | + ) |
| 65 | + ); |
| 66 | +``` |
0 commit comments