> For the complete documentation index, see [llms.txt](https://drupal-graphql.gitbook.io/graphql/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://drupal-graphql.gitbook.io/graphql/8.x-4.x/data-producers/composing.md).

# Composing producers

Often you will find that you need to call multiple producers in a sequence in order to get the data you actually want. Maybe because you call a producer that only returns an `id` and then you need an `entity_load` producer to use that `id` to return the actual entity, or maybe a route that returns a URL Object and then you want to take that URL can get the entity out of it using the `route_entity` data producer.

This can be accomplished using some of the built-in helpers inside the `$builder` object called `compose`. given our example for the previous example `current_user`, this is how it works :

```php
$registry->addFieldResolver('Query', 'currentUser', $builder->compose(
  $builder->produce('current_user'),
  $builder->produce('entity_load')
    ->map('type', $builder->fromValue('user'))
    ->map('id', $builder->fromParent())
));
```

We are chaining the two data producers together here, one after the other and calling `fromParent` will give us the result that was returned in the previous step.

## Custom steps

What if we need to do some massaging but not necessarily using any data producer? The `$builder` object includes a callback property as well that we can use for this :

```php
$registry->addFieldResolver('Query', 'currentUser', $builder->compose(
  $builder->produce('current_user'),
  $builder->produce('entity_load')
    ->map('type', $builder->fromValue('user'))
    ->map('id', $builder->fromParent()),
  $builder->callback(function ($entity) {
    // Here we can do anything we want to the data. We get as a parameter anything that was returned
    // in the previous step.
  })
));
```

## Debugging producers

Note that you can always easily tap into the chain and e.g. use xdebug to debug the values:

```php
  $builder->compose(
      $builder->tap($builder->callback(function ($parent, $args) {
          // YOU CAN SET A XDEBUG BREAKPOINT IN THESE CALLBACKS TO CHECK THE VALUES.
          $compose_step = 0;
      })),
      // Load the file object from the field.
      $builder->produce('property_path')
        ->map('type', $builder->fromValue('entity:YOUR_ENTITY_TYPE'))
        ->map('value', $builder->fromParent())
        ->map('path', $builder->fromValue('YOUR_FIELD_NAME.YOUR_FIELD_PROPERTY')),
      $builder->tap($builder->callback(function ($parent, $args) {
          // YOU CAN SET A XDEBUG BREAKPOINT IN THESE CALLBACKS TO CHECK THE VALUES.
          $compose_step = 1;
      })),
      // Load the image style derivative of the file.
      $builder->produce('image_derivative')
        ->map('entity', $builder->fromParent())
        ->map('style', $builder->fromValue('YOUR_IMAGE_STYLE')),
      $builder->tap($builder->callback(function ($parent, $args) {
          // YOU CAN SET A XDEBUG BREAKPOINT IN THESE CALLBACKS TO CHECK THE VALUES.
          $compose_step = 2;
      })),
      // Retrieve the url of the generated image.
      $builder->produce('image_style_url')
        ->map('derivative', $builder->fromParent()),
      $builder->tap($builder->callback(function ($parent, $args) {
          // YOU CAN SET A XDEBUG BREAKPOINT IN THESE CALLBACKS TO CHECK THE VALUES.
          $compose_step = 3;
      }))
    )
  );
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://drupal-graphql.gitbook.io/graphql/8.x-4.x/data-producers/composing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
