瀏覽代碼

graphql: added TextInterface and Texts query

bach 3 年之前
父節點
當前提交
a49edd4c48

+ 26 - 7
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

+ 4 - 0
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]
 }

+ 49 - 1
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'))