added entity field def access to graphql

This commit is contained in:
Bachir Soussi Chiadmi 2023-04-17 13:17:08 +02:00
parent db6dcbf508
commit f6611f839b
3 changed files with 106 additions and 3 deletions

View File

@ -12,7 +12,8 @@ type Concernement implements NodeInterface {
title: String!
path: String!
author: String
texte: String
description: String
caillou: String
recit: Filefield
entites: [Entiteintegre]
}
@ -134,3 +135,14 @@ type Date {
start: String
end: String
}
type EntityDefinition {
fields: [FieldDef]
}
type FieldDef {
field_name: String
type: String
label: String
description: String
}

View File

@ -54,3 +54,7 @@ extend type Query {
extend type Query {
group(id: Int!): Group
}
extend type Query {
entitydef(type: String, bundle: String): EntityDefinition
}

View File

@ -8,6 +8,7 @@ use Drupal\graphql\GraphQL\Response\ResponseInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
use Drupal\materio_graphql\GraphQL\Response\MaterioResponse;
use Drupal\Core\Url;
use Drupal\Node\Entity\NodeType;
use GraphQL\Error\Error;
@ -55,6 +56,9 @@ class OuattSchemaExtension extends SdlSchemaExtensionPluginBase {
//
$this->addLink($registry, $builder);
$this->addEntityDefinition($registry, $builder);
$this->addFieldDef($registry, $builder);
}
// ___ _
@ -162,11 +166,18 @@ class OuattSchemaExtension extends SdlSchemaExtensionPluginBase {
->map('entity', $builder->fromParent())
));
$registry->addFieldResolver('Concernement', 'texte',
$registry->addFieldResolver('Concernement', 'description',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('body.value'))
->map('path', $builder->fromValue('field_description.value'))
);
$registry->addFieldResolver('Concernement', 'caillou',
$builder->produce('property_path')
->map('type', $builder->fromValue('entity:node'))
->map('value', $builder->fromParent())
->map('path', $builder->fromValue('field_caillou.value'))
);
$registry->addFieldResolver('Concernement', 'author',
@ -882,4 +893,80 @@ class OuattSchemaExtension extends SdlSchemaExtensionPluginBase {
})
);
}
protected function addEntityDefinition(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('Query', 'entitydef',
$builder->compose(
$builder->callback(function($parent, $arg){
// $entity_object = NodeType::load($builder->fromArgument('bundle'));
$field_defintions = \Drupal::service('entity_field.manager')->getFieldDefinitions($arg['type'], $arg['bundle']);
return $field_defintions;
})
)
);
$registry->addFieldResolver('EntityDefinition', 'fields',
$builder->callback(function ($parent, $args) {
return $parent;
})
);
}
protected function addFieldDef(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry->addFieldResolver('FieldDef', 'type',
$builder->callback(function ($parent, $args) {
return $parent->getType();
})
);
$registry->addFieldResolver('FieldDef', 'field_name',
$builder->callback(function ($parent, $args) {
return $parent->getName();
})
);
$registry->addFieldResolver('FieldDef', 'label',
$builder->callback(function ($parent, $args) {
$label = $parent->getLabel();
$vartype = gettype($label);
switch ($vartype) {
case 'string':
return $label;
break;
case 'object':
switch (get_class($label)) {
case 'TranslatableMarkup':
return $label->render();
break;
}
break;
}
return "";
})
);
$registry->addFieldResolver('FieldDef', 'description',
$builder->callback(function ($parent, $args) {
$description = $parent->getDescription();
$vartype = gettype($description);
switch ($vartype) {
case 'string':
return $description;
break;
case 'object':
switch (get_class($description)) {
case 'TranslatableMarkup':
return $description->render();
break;
}
break;
}
return "";
})
);
}
}