cleaned materio_gqp, fixed gql linked_materials bad language

This commit is contained in:
Bachir Soussi Chiadmi 2021-03-09 16:35:48 +01:00
parent 86670565ba
commit bc48c9e876
6 changed files with 213 additions and 193 deletions

View File

@ -1,40 +0,0 @@
<?php
namespace Drupal\materio_graphql\GraphQL\Response;
use Drupal\Core\Entity\EntityInterface;
use Drupal\graphql\GraphQL\Response\Response;
/**
* Type of response used when an materiau is returned.
*/
class MateriauResponse extends Response {
/**
* The materiau to be served.
*
* @var \Drupal\Core\Entity\EntityInterface|null
*/
protected $materiau;
/**
* Sets the content.
*
* @param \Drupal\Core\Entity\EntityInterface|null $materiau
* The materiau to be served.
*/
public function setMateriau(?EntityInterface $materiau): void {
$this->materiau = $materiau;
}
/**
* Gets the materiau to be served.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The materiau to be served.
*/
public function materiau(): ?EntityInterface {
return $this->materiau;
}
}

View File

@ -1,98 +0,0 @@
<?php
namespace Drupal\materio_graphql\Plugin\GraphQL\DataProducer;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\materio_graphql\GraphQL\Response\MateriauResponse;
use Drupal\node\Entity\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Creates a new materiau entity.
*
* @DataProducer(
* id = "create_materiau",
* name = @Translation("Create Materiau"),
* description = @Translation("Creates a new materiau."),
* produces = @ContextDefinition("any",
* label = @Translation("Materiau")
* ),
* consumes = {
* "data" = @ContextDefinition("any",
* label = @Translation("Materiau data")
* )
* }
* )
*/
class CreateMateriau extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_user')
);
}
/**
* CreateMateriau constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(array $configuration, string $plugin_id, array $plugin_definition, AccountInterface $current_user) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->currentUser = $current_user;
}
/**
* Creates an materiau.
*
* @param array $data
* The submitted values for the materiau.
*
* @return \Drupal\graphql_composable\GraphQL\Response\MateriauResponse
* The newly created materiau.
*
* @throws \Exception
*/
public function resolve(array $data) {
$response = new MateriauResponse();
if ($this->currentUser->hasPermission("create materiau content")) {
$values = [
'type' => 'materiau',
'title' => $data['title'],
'body' => $data['description'],
];
$node = Node::create($values);
$node->save();
$response->setMateriau($node);
}
else {
$response->addViolation(
$this->t('You do not have permissions to create materiaus.')
);
}
return $response;
}
}

View File

@ -10,7 +10,7 @@ use GraphQL\Error\Error;
/**
* @Schema(
* id = "materio",
* name = "Materio schema",
* name = "Materio",
* extensions = "materio",
* )
*/

View File

@ -200,18 +200,66 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
// https://blog.chrismitchellonline.com/posts/custom_graphql_data/
$registry->addFieldResolver('Materiau', 'linked_materials',
// $builder->compose(
// $builder->produce('entity_reference')
// ->map('entity', $builder->fromParent())
// ->map('field', $builder->fromValue('field_linked_materials')))
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_materials'))
));
$builder->callback(function($parent, $args){
$linkedmaterials = $parent->get('field_linked_materials')->getValue();
$nids = [];
foreach ($linkedmaterials as $key => $value) {
$nids[] = $value['target_id'];
}
$lang = $parent->language()->getId();
$test="test";
return [
"ids" => $nids,
"language" => $lang
];
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['materiau']))
->map('ids', $builder->callback(function($parent, $args){
return $parent['ids'];
}))
->map('language', $builder->callback(function($parent, $args){
return $parent['language'];
}))
)
);
$registry->addFieldResolver('Materiau', 'linked_articles',
// $builder->compose(
// $builder->produce('entity_reference')
// ->map('entity', $builder->fromParent())
// ->map('field', $builder->fromValue('field_linked_articles')))
$builder->compose(
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_articles'))
));
$builder->callback(function($parent, $args){
$linkedmaterials = $parent->get('field_linked_articles')->getValue();
$nids = [];
foreach ($linkedmaterials as $key => $value) {
$nids[] = $value['target_id'];
}
$lang = $parent->language()->getId();
$test="test";
return [
"ids" => $nids,
"language" => $lang
];
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['materiau']))
->map('ids', $builder->callback(function($parent, $args){
return $parent['ids'];
}))
->map('language', $builder->callback(function($parent, $args){
return $parent['language'];
}))
)
);
$registry->addFieldResolver('Materiau', 'memo',
$builder->produce('property_path')
@ -557,6 +605,7 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
->map('type', $builder->fromValue('node'))
// ->map('bundles', $builder->fromValue(['thematique']))
->map('id', $builder->fromArgument('id'))
->map('language', $builder->fromArgument('lang'))
);
$registry->addFieldResolver('Thematique', 'id',
@ -621,9 +670,33 @@ class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
// https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md
// https://blog.chrismitchellonline.com/posts/custom_graphql_data/
$registry->addFieldResolver('Thematique', 'linked_materials',
$builder->produce('entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('field_linked_materials'))
// $builder->produce('entity_reference')
// ->map('entity', $builder->fromParent())
// ->map('field', $builder->fromValue('field_linked_materials'))
$builder->compose(
$builder->callback(function($parent, $args){
$linkedmaterials = $parent->get('field_linked_materials')->getValue();
$nids = [];
foreach ($linkedmaterials as $key => $value) {
$nids[] = $value['target_id'];
}
$lang = $parent->language()->getId();
$test="test";
return [
"ids" => $nids,
"language" => $lang
];
}),
$builder->produce('entity_load_multiple')
->map('type', $builder->fromValue('node'))
->map('bundles', $builder->fromValue(['materiau']))
->map('ids', $builder->callback(function($parent, $args){
return $parent['ids'];
}))
->map('language', $builder->callback(function($parent, $args){
return $parent['language'];
}))
)
);
$registry->addFieldResolver('Thematique', 'images',

View File

@ -1,42 +0,0 @@
<?php
declare(strict_types = 1);
namespace Drupal\materio_graphql\Wrappers\Response;
use Drupal\Core\Entity\EntityInterface;
use Drupal\graphql\GraphQL\Response\Response;
/**
* Type of response used when an materiau is returned.
*/
class MateriauResponse extends Response {
/**
* The materiau to be served.
*
* @var \Drupal\Core\Entity\EntityInterface|null
*/
protected $materiau;
/**
* Sets the content.
*
* @param \Drupal\Core\Entity\EntityInterface|null $materiau
* The materiau to be served.
*/
public function setMateriau(?EntityInterface $materiau): void {
$this->materiau = $materiau;
}
/**
* Gets the materiau to be served.
*
* @return \Drupal\Core\Entity\EntityInterface|null
* The materiau to be served.
*/
public function materiau(): ?EntityInterface {
return $this->materiau;
}
}

File diff suppressed because one or more lines are too long