You are here

protected function ContentEntityTypeResolver::resolveFieldFromProperty in GraphQL 8

Same name and namespace in other branches
  1. 8.2 src/TypeResolver/ContentEntityTypeResolver.php \Drupal\graphql\TypeResolver\ContentEntityTypeResolver::resolveFieldFromProperty()

Helper function to resolve a field definition from a typed data property.

Parameters

string $propertyKey: The key of the typed data property.

\Drupal\Core\TypedData\DataDefinitionInterface $propertyDefinition: The data definition of the property.

Return value

array|null The resolved field definition.

Overrides TypedDataTypeResolver::resolveFieldFromProperty

1 call to ContentEntityTypeResolver::resolveFieldFromProperty()
ContentEntityTypeResolver::resolveFields in src/TypeResolver/ContentEntityTypeResolver.php
Helper function to resolve the list of available fields for a type.

File

src/TypeResolver/ContentEntityTypeResolver.php, line 234

Class

ContentEntityTypeResolver
Resolves the schema for content entities.

Namespace

Drupal\graphql\TypeResolver

Code

protected function resolveFieldFromProperty($propertyKey, DataDefinitionInterface $propertyDefinition) {
  if (!$propertyDefinition instanceof FieldDefinitionInterface) {

    // Treat non-field properties via the default typed data resolver.
    return parent::resolveFieldFromProperty($propertyKey, $propertyDefinition);
  }
  $storageDefinition = $propertyDefinition
    ->getFieldStorageDefinition();
  $isRequired = $propertyDefinition
    ->isRequired();

  // Skip the list if the cardinality is 1.
  $skipList = $storageDefinition
    ->getCardinality() === 1;

  // Skip the sub-selection if there is just one field item property and it is
  // defined as the main property.

  /** @var \Drupal\Core\TypedData\ComplexDataDefinitionInterface $itemDefinition */
  $itemDefinition = $propertyDefinition
    ->getItemDefinition();
  $subProperties = $itemDefinition
    ->getPropertyDefinitions();
  $mainPropertyName = $itemDefinition
    ->getMainPropertyName();
  $mainProperty = $itemDefinition
    ->getPropertyDefinition($mainPropertyName);
  $skipSubSelection = count($subProperties) === 1 && $mainPropertyName && $mainPropertyName === key($subProperties);

  // Use the default typed data resolver if we can't simplify this field.
  if (!$skipList && !$skipSubSelection) {
    return parent::resolveFieldFromProperty($propertyKey, $propertyDefinition);
  }
  $propertyDefinition = $skipList ? $itemDefinition : $propertyDefinition;
  $propertyDefinition = $skipSubSelection ? $mainProperty : $propertyDefinition;
  $finalResolver = $this
    ->getPropertyResolverFunction($propertyDefinition);
  if (!($propertyType = $this->typeResolver
    ->resolveRecursive($propertyDefinition))) {
    return NULL;
  }
  $propertyType = $skipList ? $propertyType : new ListModifier($propertyType);
  $propertyType = $isRequired ? new NonNullModifier($propertyType) : $propertyType;
  return [
    'type' => $propertyType,
    'resolve' => [
      __CLASS__,
      'getFieldValueSimplified',
    ],
    'resolveData' => [
      'skipList' => $skipList,
      'skipSubSelection' => $skipSubSelection,
      'property' => $propertyKey,
      'subProperty' => $mainPropertyName,
      'finalResolver' => $finalResolver,
    ],
  ];
}