joaogarin / mydrupalgql

7 stars 3 forks source link

Example for entity_reference not working #1

Open alesbencina opened 5 years ago

alesbencina commented 5 years ago

Greetings,

first of all thank you for contribution.

I have enabled your module and tested it. But I cannot get the tags from article. I always get NULL. Do I do something wrong? What can I do to make it right? Bellow is my schema and resolvers which are already provided in example.

type Article implements NodeInterface {
    id: Int!
    uid: String
    title: String!
    render: String
    creator: String
    tags: [TagTerm]
}

type TagTerm {
    id: Int
    name: String
}
$registry->addFieldResolver('Article', 'tags',
      $builder->produce('entity_reference', [
        'mapping' => [
          'entity' => $builder->fromParent(),
          'field' => $builder->fromValue('field_tags'),
        ],
      ])
    );

    $registry->addFieldResolver('TagTerm', 'id',
      $builder->produce('entity_id', ['mapping' => [
        'entity' => $builder->fromParent(),
      ]])
    );
{
      "message": "Missing input mapper for argument entity.",
      "locations": [
        {
          "line": 7,
          "column": 7
        }
      ],
      "path": [
        "articles",
        "items",
        0,
        "render"
      ]
    },
blackbeario commented 4 years ago

What worked for me was updating the syntax.

For example

$registry->addFieldResolver('Article', 'tags',
      $builder->produce('entity_reference', [
        'mapping' => [
          'entity' => $builder->fromParent(),
          'field' => $builder->fromValue('field_tags'),
        ],
      ])
    );

should be

    $registry->addFieldResolver('Article', 'tags',
      $builder->produce('entity_reference')
        ->map('entity', $builder->fromParent())
        ->map('field', $builder->fromValue('field_tags'))
    );

After updating, I am able to get tags. Update the syntax for every entity type.

{
  "data": {
    "article": {
      "tags": [
        {
          "name": "flutter"
        }
      ],
      "title": "My New Flutter Article"
    }
  }
}