Problem/Motivation

GraphQL 3 provided a way to fetch the rendered HTML of a whole node via the entityRendered property:

query {
  route(path:"/the/meaning/of/42") {
    ... on EntityCanonicalUrl {
      entity {
        entityLabel
        ... on Node {
          entityRendered
        }
      }
    }
  }
}

It would be nice if graphql_core_schema would provide that, too.

Proposed resolution

GraphQL 4 already has an in-built data producer for that: Entity rendered (entity_rendered).

Comments

cweiske created an issue.

cweiske’s picture

A simple implementation would be:

.graphqls file:

type Entity {
  entityRendered(mode: String = "full"): String!
}

Schema extension php file:

namespace Drupal\my_module\Plugin\GraphQL\SchemaExtension;

use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
use GraphQL\Type\Definition\ResolveInfo;

/**
 * Adds additional GraphQL fields.
 *
 * @SchemaExtension(
 *   id = "my_module",
 *   name = "",
 *   description = "Additional fields",
 *   schema = "graphql_schema_core"
 * )
 */
class CoreSchemaExtension extends SdlSchemaExtensionPluginBase {
  /**
   * {@inheritdoc}
   */
  public function registerResolvers(ResolverRegistryInterface $registry) {
    $builder = new ResolverBuilder();
    $this->addEntityFields($registry, $builder);
  }

  /**
   * Add fields to the Entity class.
   */
  protected function addEntityFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
    // https://www.drupal.org/project/graphql_core_schema/issues/3581858
    // #3581858: Add support for rendered entities.
    $registry->addFieldResolver(
      'Entity', 'entityRendered',
      $builder->produce('entity_rendered')
        ->map('entity', $builder->fromParent())
        ->map('mode', $builder->fromArgument('mode'))
    );
  }
}