You are here

public function EntitySchemaProvider::getQuerySchema in GraphQL 8.2

Same name and namespace in other branches
  1. 8 src/SchemaProvider/EntitySchemaProvider.php \Drupal\graphql\SchemaProvider\EntitySchemaProvider::getQuerySchema()

Return value

array

Overrides SchemaProviderBase::getQuerySchema

File

src/SchemaProvider/EntitySchemaProvider.php, line 66

Class

EntitySchemaProvider
Generates a GraphQL Schema for content entity types.

Namespace

Drupal\graphql\SchemaProvider

Code

public function getQuerySchema() {
  $fields = [];

  // We only support content entity types for now.
  $types = array_filter($this->entityManager
    ->getDefinitions(), function (EntityTypeInterface $entityType) {
    return $entityType
      ->isSubclassOf('\\Drupal\\Core\\Entity\\ContentEntityInterface');
  });

  // Format the entity type names as camel-cased strings.
  $names = StringHelper::formatPropertyNameList(array_keys($types));
  foreach ($types as $key => $type) {

    /** @var \Drupal\Core\Entity\TypedData\EntityDataDefinition $definition */
    $definition = $this->typedDataManager
      ->createDataDefinition("entity:{$key}");
    if (!($resolved = $this->typeResolver
      ->resolveRecursive($definition))) {
      continue;
    }
    $fields[$names[$key]] = [
      'type' => $resolved,
      'args' => [
        'id' => [
          'type' => new NonNullModifier(Type::intType()),
        ],
      ],
      'resolve' => [
        __CLASS__,
        'getEntitySingle',
      ],
      'resolveData' => [
        'type' => $key,
      ],
    ];
    $arguments = $this
      ->getQueryArguments($definition);
    $argumentNames = StringHelper::formatPropertyNameList(array_keys($arguments));
    $fields["{$names[$key]}Query"] = [
      'type' => new ListModifier($resolved),
      'args' => [
        'offset' => [
          'type' => Type::intType(),
        ],
        'limit' => [
          'type' => Type::intType(),
        ],
      ] + array_combine($argumentNames, $arguments),
      'resolve' => [
        __CLASS__,
        'getEntityList',
      ],
      'resolveData' => [
        'type' => $key,
        'args' => array_flip($argumentNames),
      ],
    ];
  }
  return $fields;
}