added working graphql 4 module and started materio_graphql
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
type Mutation
|
||||
|
||||
scalar Violation
|
||||
|
||||
type Materiau {
|
||||
id: Int!
|
||||
uuid: String!
|
||||
title: String!
|
||||
author: String
|
||||
memo: String
|
||||
linked_materials: [Materiau]
|
||||
images: [Image]
|
||||
}
|
||||
|
||||
type MateriauResponse implements Response {
|
||||
errors: [Violation]
|
||||
materiau: Materiau
|
||||
}
|
||||
|
||||
interface Response {
|
||||
errors: [Violation]
|
||||
}
|
||||
|
||||
input MateriauInput {
|
||||
title: String!
|
||||
description: String
|
||||
}
|
||||
|
||||
type Image {
|
||||
id: Int!
|
||||
url: String!
|
||||
alt: String
|
||||
style_minicard: ImageStyle
|
||||
}
|
||||
|
||||
type ImageStyle {
|
||||
width: Int
|
||||
height: Int
|
||||
url: String
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
extend type Query {
|
||||
materiau(id: Int!): Materiau
|
||||
}
|
||||
|
||||
extend type Mutation {
|
||||
createMateriau(data: MateriauInput): MateriauResponse
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
name: Materio GraphQL
|
||||
type: module
|
||||
description: 'Materio GraphQL schema.'
|
||||
package: Materio
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- graphql:graphql
|
||||
- node:node
|
||||
core_version_requirement: ^8 || ^9
|
@@ -0,0 +1,40 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_graphql\Plugin\GraphQL\Schema;
|
||||
|
||||
use Drupal\graphql\Plugin\GraphQL\Schema\ComposableSchema;
|
||||
|
||||
/**
|
||||
* @Schema(
|
||||
* id = "materio",
|
||||
* name = "Materio schema",
|
||||
* extensions = "materio",
|
||||
* )
|
||||
*/
|
||||
class MaterioSchema extends ComposableSchema {
|
||||
|
||||
}
|
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\materio_graphql\Plugin\GraphQL\SchemaExtension;
|
||||
|
||||
use Drupal\graphql\GraphQL\ResolverBuilder;
|
||||
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
|
||||
use Drupal\graphql\GraphQL\Response\ResponseInterface;
|
||||
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
|
||||
use Drupal\materio_graphql\GraphQL\Response\MaterioResponse;
|
||||
|
||||
/**
|
||||
* @SchemaExtension(
|
||||
* id = "materio_extension",
|
||||
* name = "Materio extension",
|
||||
* description = "Materio extension.",
|
||||
* schema = "materio"
|
||||
* )
|
||||
*/
|
||||
class MaterioSchemaExtension extends SdlSchemaExtensionPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function registerResolvers(ResolverRegistryInterface $registry) {
|
||||
$builder = new ResolverBuilder();
|
||||
|
||||
$registry->addFieldResolver('Query', 'materiau',
|
||||
$builder->produce('entity_load')
|
||||
->map('type', $builder->fromValue('node'))
|
||||
->map('bundles', $builder->fromValue(['materiau']))
|
||||
->map('id', $builder->fromArgument('id'))
|
||||
);
|
||||
|
||||
// Create materiau mutation.
|
||||
$registry->addFieldResolver('Mutation', 'createMateriau',
|
||||
$builder->produce('create_materiau')
|
||||
->map('data', $builder->fromArgument('data'))
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('MateriauResponse', 'materiau',
|
||||
$builder->callback(function (MateriauResponse $response) {
|
||||
return $response->materiau();
|
||||
})
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('MateriauResponse', 'errors',
|
||||
$builder->callback(function (MateriauResponse $response) {
|
||||
return $response->getViolations();
|
||||
})
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Materiau', 'id',
|
||||
$builder->produce('entity_id')
|
||||
->map('entity', $builder->fromParent())
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Materiau', 'uuid',
|
||||
$builder->produce('entity_uuid')
|
||||
->map('entity', $builder->fromParent())
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Materiau', 'title',
|
||||
$builder->compose(
|
||||
$builder->produce('entity_label')
|
||||
->map('entity', $builder->fromParent())
|
||||
)
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Materiau', 'author',
|
||||
$builder->compose(
|
||||
$builder->produce('entity_owner')
|
||||
->map('entity', $builder->fromParent()),
|
||||
$builder->produce('entity_label')
|
||||
->map('entity', $builder->fromParent())
|
||||
)
|
||||
);
|
||||
|
||||
// https://github.com/drupal-graphql/graphql/blob/8.x-4.x/doc/SUMMARY.md
|
||||
// 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'))
|
||||
)
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Materiau', 'memo',
|
||||
$builder->produce('property_path')
|
||||
->map('type', $builder->fromValue('entity:node'))
|
||||
->map('value', $builder->fromParent())
|
||||
->map('path', $builder->fromValue('field_memo.value'))
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Materiau', 'images',
|
||||
$builder->produce('entity_reference')
|
||||
->map('entity', $builder->fromParent())
|
||||
->map('field', $builder->fromValue('field_materiau_images'))
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Image', 'id',
|
||||
$builder->produce('entity_id')
|
||||
->map('entity', $builder->fromParent())
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Image', 'url',
|
||||
$builder->produce('image_url')
|
||||
->map('entity', $builder->fromParent())
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Image', 'alt',
|
||||
$builder->produce('property_path')
|
||||
->map('type', $builder->fromValue('entity:node'))
|
||||
->map('value', $builder->fromParent())
|
||||
->map('path', $builder->fromValue('field_image.alt'))
|
||||
);
|
||||
|
||||
$registry->addFieldResolver('Image', 'style_minicard',
|
||||
$builder->produce('image_derivative')
|
||||
->map('entity', $builder->fromParent())
|
||||
->map('style', $builder->fromValue('card_medium_half'))
|
||||
);
|
||||
|
||||
|
||||
// Response type resolver.
|
||||
$registry->addTypeResolver('Response', [
|
||||
__CLASS__,
|
||||
'resolveResponse',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the response type.
|
||||
*
|
||||
* @param \Drupal\graphql\GraphQL\Response\ResponseInterface $response
|
||||
* Response object.
|
||||
*
|
||||
* @return string
|
||||
* Response type.
|
||||
*
|
||||
* @throws \Exception
|
||||
* Invalid response type.
|
||||
*/
|
||||
public static function resolveResponse(ResponseInterface $response): string {
|
||||
// Resolve content response.
|
||||
if ($response instanceof MateriauResponse) {
|
||||
return 'MateriauResponse';
|
||||
}
|
||||
throw new \Exception('Invalid response type.');
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user