Skip to content

Composing data producer example is broken #1121

@Kingdutch

Description

@Kingdutch
Contributor

The code snippet that shows how $builder->compose can be used to load the currentUser is broken. The current_user data producer produces an AccountInterface instance, but the entity_load id field requires a string or an int.

The current incorrect snippet is the following part.

$builder->compose(
  $builder->produce('current_user'),
  $builder->produce('entity_load')
    ->map('type', $builder->fromValue('user'))
    ->map('id', $builder->fromParent())
);

For a query

query {
  currentUser { uuid }
}

It causes the following output

{
  "data": {
    "currentUser": null
  },
  "extensions": [
    {
      "message": "Object of class Drupal\\Core\\Session\\UserSession could not be converted to string",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "currentUser"
      ]
    }
  ]
}

Activity

mcortes19

mcortes19 commented on Feb 3, 2023

@mcortes19

This is the way I implemented, basically use callback function to get and pass the id from the AccountProxyInterface object returned by the current_user data-producer

use Drupal\Core\Session\AccountProxyInterface;
$registry->addFieldResolver('Query', 'currentUser', $builder->compose(
  $builder->produce('current_user'),
  $builder->produce('entity_load')
     ->map('type', $builder->fromValue('user'))
     ->map('id', $builder->callback(function (AccountProxyInterface $account) {
       return $account->id();
    }))
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Kingdutch@mcortes19

      Issue actions

        Composing data producer example is broken · Issue #1121 · drupal-graphql/graphql