Skip to content

Make ImageDerivative configurable to return additional properties#1122

Open
chasinhues wants to merge 1 commit into
drupal-graphql:8.x-4.xfrom
chasinhues:1120-add-image-id
Open

Make ImageDerivative configurable to return additional properties#1122
chasinhues wants to merge 1 commit into
drupal-graphql:8.x-4.xfrom
chasinhues:1120-add-image-id

Conversation

@chasinhues

@chasinhues chasinhues commented Nov 26, 2020

Copy link
Copy Markdown

Resolves #1120

Usage

Use ImageDerivative to extract extra properties from File entities such as filename, uri, filemime, etc. See Drupal's File entity for full list of field_name's that can be used.

Make sure you update your schema definition to include the additional properties you use from ImageDerivative.

Example

    // MySchemaExtension.php

    ...

    $registry->addFieldResolver('MyResolveType', 'images',
      $builder->compose(
        $builder->produce('entity_reference')
          ->map('entity', $builder->fromParent())
          ->map('field', $builder->fromValue('field_images')),
        $builder->map(
          $builder->produce('image_derivative')
            ->map('entity', $builder->fromParent())
            ->map('style', $builder->fromValue('submission_media_carousel'))
            ->map('additional_properties', $builder->fromValue(['filename', 'filemime']))
        )
      )
    );
// my_graphql.graphqls

...

type Image {
  id: Int!
  url: String!
  width: Int
  height: Int
  filename: String!
  filemime: String!
}

@chasinhues chasinhues changed the title Add image ID to ImageDerivative Make ImageDerivative configurable to return additional properties Nov 27, 2020
@chasinhues chasinhues changed the title Make ImageDerivative configurable to return additional properties Make ImageDerivative configurable to return additional properties Nov 27, 2020
Change field name to id

Make plugin configurable to return additional properties

Remove id from return

Add ID back to return
@Boby

Boby commented Feb 23, 2022

Copy link
Copy Markdown

tried it I got a :

TypeError: Argument 1 passed to Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\Fields\Image\ImageDerivative::resolve() must implement interface Drupal\file\FileInterface or be null, instance of Drupal\media\Entity\Media given in Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\Fields\Image\ImageDerivative->resolve() (line 91 of modules/contrib/graphql/src/Plugin/GraphQL/DataProducer/Entity/Fields/Image/ImageDerivative.php).

@Berdir

Berdir commented Apr 29, 2022

Copy link
Copy Markdown

that information is on the image. seems a bit strange to put it on this level, can't you get that elsewhere?

I actually came here looking for a way to not return image/width as it can be expensive to calculate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make ImageDerivative resolver configurable to return additional properties

4 participants