From a49edd4c483f2de6698160c68ec6d10f28696ad6 Mon Sep 17 00:00:00 2001 From: bach Date: Thu, 25 Feb 2021 17:32:22 +0100 Subject: [PATCH] graphql: added TextInterface and Texts query --- .../graphql/enfr_extension.base.graphqls | 33 +++++++++--- .../graphql/enfr_extension.extension.graphqls | 4 ++ .../SchemaExtension/EnFrSchemaExtension.php | 50 ++++++++++++++++++- 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.base.graphqls b/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.base.graphqls index 7c2f497..3b6c53b 100644 --- a/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.base.graphqls +++ b/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.base.graphqls @@ -5,39 +5,58 @@ interface NodeInterface { path: String! } -type Textref implements NodeInterface { +interface TextInterface { id: Int! uuid: String! + bundle: String! title: String! path: String! author: String texte: String - text_en_rebond: [Textref] - text_produits: [Textprod] images: [Image] videos: [VideoLink] - tags: [Taxoterm] familles: [Taxoterm] +} + +type Textref implements NodeInterface & TextInterface{ + id: Int! + uuid: String! + bundle: String! + title: String! + path: String! + author: String + texte: String + images: [Image] + videos: [VideoLink] + familles: [Taxoterm] + # + text_produits: [Textprod] + text_en_rebond: [Textref] + tags: [Taxoterm] # field_degres_detrangement # notes: [Note] + # field_titre # auteur: Auteur # edition: Edition # Date: String } -type Textprod implements NodeInterface { +type Textprod implements NodeInterface & TextInterface { id: Int! uuid: String! + bundle: String! title: String! path: String! author: String texte: String - text_de_depart: [Textref] images: [Image] videos: [VideoLink] - tagsprod: [Taxoterm] familles: [Taxoterm] + # + text_de_depart: [Textref] + tagsprod: [Taxoterm] # notes: [Note] + # field_titre # auteur: Auteur # edition: Edition # date: Date diff --git a/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.extension.graphqls b/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.extension.graphqls index 019e039..6137cdd 100644 --- a/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.extension.graphqls +++ b/src/web/modules/custom/enfrancais_graphql/graphql/enfr_extension.extension.graphqls @@ -2,6 +2,10 @@ # route(path: String!): NodeInterface # } +extend type Query { + texts: [TextInterface] +} + extend type Query { textsdepart: [Textref] } diff --git a/src/web/modules/custom/enfrancais_graphql/src/Plugin/GraphQL/SchemaExtension/EnFrSchemaExtension.php b/src/web/modules/custom/enfrancais_graphql/src/Plugin/GraphQL/SchemaExtension/EnFrSchemaExtension.php index 55addb5..69fac0f 100644 --- a/src/web/modules/custom/enfrancais_graphql/src/Plugin/GraphQL/SchemaExtension/EnFrSchemaExtension.php +++ b/src/web/modules/custom/enfrancais_graphql/src/Plugin/GraphQL/SchemaExtension/EnFrSchemaExtension.php @@ -29,6 +29,8 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase { $this->addRouteResolver($registry, $builder); + $this->addTexts($registry, $builder); + $this->addTextsdepart($registry, $builder); $this->addTextref($registry, $builder); @@ -90,7 +92,7 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase { if ($class === 'Node') { switch ($value->bundle()) { case 'texte': return 'Textref'; - case 'texte_prod': return 'TextProd'; + case 'texte_prod': return 'Textprod'; } } throw new Error('Could not resolve content type.'); @@ -105,6 +107,33 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase { )); } + protected function addTexts(ResolverRegistryInterface $registry, ResolverBuilder $builder){ + // Tell GraphQL how to resolve types of a common interface. + $registry->addTypeResolver('TextInterface', function ($value) { + switch ($value->bundle()) { + case 'texte': return 'Textref'; + case 'texte_prod': return 'Textprod'; + } + throw new Error('Could not resolve content type.'); + }); + + $registry->addFieldResolver('Query', 'texts', + $builder->compose( + $builder->callback(function($parent, $arg){ + $entity_storage = \Drupal::entityTypeManager()->getStorage('node'); + $query = $entity_storage->getQuery() + ->condition('type', ['texte', 'texte_prod'], 'IN') + ->accessCheck(TRUE); + $results = $query->execute(); + return $results; + }), + $builder->produce('entity_load_multiple') + ->map('type', $builder->fromValue('node')) + ->map('ids', $builder->fromParent()) + ) + ); + } + // _____ _ ___ _ // |_ _|____ _| |_ ___ | \ ___ _ __ __ _ _ _| |_ @@ -178,6 +207,12 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('entity', $builder->fromParent()) )); + $registry->addFieldResolver('Textref', 'bundle', + $builder->compose( + $builder->produce('entity_bundle') + ->map('entity', $builder->fromParent()) + )); + $registry->addFieldResolver('Textref', 'texte', $builder->produce('property_path') ->map('type', $builder->fromValue('entity:node')) @@ -220,6 +255,13 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('path', $builder->fromValue('field_video')) ); + $registry->addFieldResolver('Textref', 'titre_ref', + $builder->produce('property_path') + ->map('type', $builder->fromValue('entity:node')) + ->map('value', $builder->fromParent()) + ->map('path', $builder->fromValue('field_titre')) + ); + $registry->addFieldResolver('Textref', 'tags', $builder->produce('entity_reference') ->map('entity', $builder->fromParent()) @@ -280,6 +322,12 @@ class EnFrSchemaExtension extends SdlSchemaExtensionPluginBase { ->map('entity', $builder->fromParent()) )); + $registry->addFieldResolver('Textprod', 'bundle', + $builder->compose( + $builder->produce('entity_bundle') + ->map('entity', $builder->fromParent()) + )); + $registry->addFieldResolver('Textprod', 'texte', $builder->produce('property_path') ->map('type', $builder->fromValue('entity:node'))