|
@@ -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.');
|
|
|
+ }
|
|
|
+
|
|
|
+}
|