Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjguru/f8d21fc7ee9c001a4328b31b6bf6956e to your computer and use it in GitHub Desktop.
Save davidjguru/f8d21fc7ee9c001a4328b31b6bf6956e to your computer and use it in GitHub Desktop.
Drupal 8 || 9 - Getting Entity Reference ID from GraphQL Field Resolvers
$registry->addFieldResolver('ContentType', 'graphqlField',
 $builder->compose(
   $builder->produce('entity_id')
     ->map('entity', $builder->fromParent()),
   $builder->callback(function ($id) {
     // First: get node by id.
     $node = \Drupal::entityTypeManager()->getStorage('node')->load($id);
     // Second: get entity reference id.
     $entity_id = $node->get('field_entity_reference_name')->first()->getValue()['target_id'];
     
     return $entity_id;
   })
 )
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment