Bulent B.
Bulent B.

Reputation: 11

Get data from multivalue field with Graphql and Drupal

I am using the Drupal graphql module to expose my data from within Drupal. All works well when retrieving simple data like id, author or a custom made field.

My problem is I have a field which stores 25 integers.

My typedefenition:

type Card {
  id: Int!
  title: String
  author: String
  numbers: [Int]
}

My fieldresolver:

    $registry->addFieldResolver('Card', 'numbers',
$builder->compose(
  $builder->produce('property_path')
  ->map('type', $builder->fromValue('entity:node:bingo_card'))
  ->map('path', $builder->fromValue('field_bingo_card_numbers'))
  ->map('value', $builder->fromParent()),
),

);

I receive all the data perfectly except the numbers. It returns 25 times null and an error like:

"extensions": [
    {
      "message": "Expected a value of type \"Int\" but received: {\"value\":\"27\"}",

So the problem is that 25 objects (?) are returned instead of the value of each item in the multivalue field.

If I try:

->map('path', $builder->fromValue('field_bingo_card_numbers.value'))

the error becomes:

 "message": "User Error: expected iterable, but did not find one for field Card.numbers.",

And now only returns 1 time null for the numbers instead of null for each single number.

Anybody any suggestions?

Upvotes: 1

Views: 493

Answers (1)

hhvardan
hhvardan

Reputation: 57

you need to add something like the following inside "compose"

$builder->callback(function ($entity) {
  $list = [];
  foreach($entity as $item){
    array_push($list, $item['value']);
  }
  return $list;
})

Upvotes: 1

Related Questions