You are here

public function CreateEntityBase::resolve in GraphQL 8.3

File

modules/graphql_core/src/Plugin/GraphQL/Mutations/Entity/CreateEntityBase.php, line 73

Class

CreateEntityBase

Namespace

Drupal\graphql_core\Plugin\GraphQL\Mutations\Entity

Code

public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {

  // There are cases where the Drupal entity API calls emit the cache metadata
  // in the current render context. In such cases
  // EarlyRenderingControllerWrapperSubscriber throws the leaked cache
  // metadata exception. To avoid this, wrap the execution in its own render
  // context.
  return $this->renderer
    ->executeInRenderContext(new RenderContext(), function () use ($value, $args, $context, $info) {
    $entityTypeId = $this->pluginDefinition['entity_type'];

    // The raw input needs to be converted to use the proper field and property
    // keys because we usually convert them to camel case when adding them to
    // the schema.
    $input = $this
      ->extractEntityInput($value, $args, $context, $info);
    $entityDefinition = $this->entityTypeManager
      ->getDefinition($entityTypeId);
    if ($entityDefinition
      ->hasKey('bundle')) {
      $bundleName = $this->pluginDefinition['entity_bundle'];
      $bundleKey = $entityDefinition
        ->getKey('bundle');

      // Add the entity's bundle with the correct key.
      $input[$bundleKey] = $bundleName;
    }
    $storage = $this->entityTypeManager
      ->getStorage($entityTypeId);
    $entity = $storage
      ->create($input);
    return $this
      ->resolveOutput($entity, $args, $info);
  });
}