Skip to content

Instantly share code, notes, and snippets.

@blazeyo
Created April 27, 2019 09:46
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 blazeyo/ed0d2f77b90cfd90c14574786990287e to your computer and use it in GitHub Desktop.
Save blazeyo/ed0d2f77b90cfd90c14574786990287e to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\my_module\Plugin\GraphQL\Fields\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GraphQL\Type\Definition\ResolveInfo;
/**
* @GraphQLField(
* id = "node_story_field_paragraph_image",
* secure = true,
* name = "fieldImageFromParagraph",
* type = " FieldParagraphImageFieldImage",
* parents = {"NodeStory"}
* )
*/
class NodeStoryParagraphImage extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
if ($value instanceof EntityInterface) {
$node = $value;
$paragraph = $this->getParagraphFromNode($node);
yield $parapraph->get('field_paragraph_image)->getValue();
}
}
}
/*
Example usage:
{
nodeById(id: "1") {
... on NodeStory {
fieldImageFromParagraph {
alt
title
url
}
}
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment