Explorar o código

GraphQL: Change union to interface #2644

Angel Fernando Quiroz Campos %!s(int64=6) %!d(string=hai) anos
pai
achega
d63670f7e8

+ 2 - 2
config/packages/graphql.yaml

@@ -6,7 +6,7 @@ overblog_graphql:
             resolver_maps:
                 - '%chamilo_graphql.resolver_map.query.class%'
                 - '%chamilo_graphql.resolver_map.enum.class%'
-                - '%chamilo_graphql.resolver_map.union.class%'
+                - '%chamilo_graphql.resolver_map.interface.class%'
                 - '%chamilo_graphql.resolver_map.scalar.class%'
                 - '%chamilo_graphql.resolver_map.mutation.class%'
         mappings:
@@ -18,7 +18,7 @@ overblog_graphql:
 parameters:
     chamilo_graphql.resolver_map.query.class: Chamilo\GraphQlBundle\Map\QueryMap
     chamilo_graphql.resolver_map.enum.class: Chamilo\GraphQlBundle\Map\EnumMap
-    chamilo_graphql.resolver_map.union.class: Chamilo\GraphQlBundle\Map\UnionMap
+    chamilo_graphql.resolver_map.interface.class: Chamilo\GraphQlBundle\Map\InterfaceMap
     chamilo_graphql.resolver_map.scalar.class: Chamilo\GraphQlBundle\Map\ScalarMap
     chamilo_graphql.resolver_map.mutation.class: Chamilo\GraphQlBundle\Map\MutationMap
 

+ 36 - 0
src/GraphQlBundle/Map/InterfaceMap.php

@@ -0,0 +1,36 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+namespace Chamilo\GraphQlBundle\Map;
+
+use Chamilo\GraphQlBundle\Resolver\CourseResolver;
+use Chamilo\GraphQlBundle\Traits\GraphQLTrait;
+use Chamilo\CourseBundle\Entity\CTool;
+use Overblog\GraphQLBundle\Resolver\ResolverMap;
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
+
+/**
+ * Class InterfaceMap.
+ *
+ * @package Chamilo\GraphQlBundle\Map
+ */
+class InterfaceMap extends ResolverMap implements ContainerAwareInterface
+{
+    use GraphQLTrait;
+
+    /**
+     * @return array
+     */
+    protected function map()
+    {
+        return [
+            'CourseTool' => [
+                self::RESOLVE_TYPE => function (CTool $tool) {
+                    $toolNames = CourseResolver::getAvailableTools();
+
+                    return $toolNames[$tool->getName()];
+                },
+            ],
+        ];
+    }
+}

+ 0 - 48
src/GraphQlBundle/Map/UnionMap.php

@@ -1,48 +0,0 @@
-<?php
-/* For licensing terms, see /license.txt */
-
-namespace Chamilo\GraphQlBundle\Map;
-
-use Chamilo\GraphQlBundle\Traits\GraphQLTrait;
-use Chamilo\CourseBundle\Entity\CTool;
-use Overblog\GraphQLBundle\Resolver\ResolverMap;
-use Symfony\Component\DependencyInjection\ContainerAwareInterface;
-
-/**
- * Class UnionMap.
- *
- * @package Chamilo\GraphQlBundle\Map
- */
-class UnionMap extends ResolverMap implements ContainerAwareInterface
-{
-    use GraphQLTrait;
-
-    /**
-     * @return array
-     */
-    protected function map()
-    {
-        return [
-            'CourseTool' => [
-                self::RESOLVE_TYPE => function (CTool $tool) {
-                    switch ($tool->getName()) {
-                        case TOOL_COURSE_DESCRIPTION:
-                            return 'ToolDescription';
-                        case TOOL_ANNOUNCEMENT:
-                            return 'ToolAnnouncements';
-                        case TOOL_NOTEBOOK:
-                            return 'ToolNotebook';
-                        case TOOL_FORUM:
-                            return 'ToolForums';
-                        case TOOL_CALENDAR_EVENT:
-                            return 'ToolAgenda';
-                        case TOOL_DOCUMENT:
-                            return 'ToolDocuments';
-                        case TOOL_LEARNPATH:
-                            return 'ToolLearningPath';
-                    }
-                },
-            ],
-        ];
-    }
-}

+ 21 - 1
src/GraphQlBundle/Resolver/CourseResolver.php

@@ -31,6 +31,22 @@ class CourseResolver implements ContainerAwareInterface
 {
     use GraphQLTrait;
 
+    /**
+     * @return array
+     */
+    public static function getAvailableTools()
+    {
+        return [
+            TOOL_COURSE_DESCRIPTION => 'ToolDescription',
+            TOOL_ANNOUNCEMENT => 'ToolAnnouncements',
+            TOOL_NOTEBOOK => 'ToolNotebook',
+            TOOL_FORUM => 'ToolForums',
+            TOOL_CALENDAR_EVENT => 'ToolAgenda',
+            TOOL_DOCUMENT => 'ToolDocuments',
+            TOOL_LEARNPATH => 'ToolLearningPath',
+        ];
+    }
+
     /**
      * @param Course   $course
      * @param Argument $args
@@ -94,7 +110,11 @@ class CourseResolver implements ContainerAwareInterface
         }
 
         if (empty($args['type'])) {
-            return $course->getTools($session);
+            return $course
+                ->getTools($session)
+                ->filter(function (CTool $tool) {
+                    return array_key_exists($tool->getName(), self::getAvailableTools());
+                });
         }
 
         $criteria = Criteria::create()

+ 14 - 9
src/GraphQlBundle/Resources/config/schema.types.graphql

@@ -100,7 +100,7 @@ type Course {
 }
 
 "Global summary of the course."
-type ToolDescription {
+type ToolDescription implements CourseTool {
     name: String
     category: String
     image: String
@@ -117,7 +117,7 @@ type CourseDescription {
 }
 
 "Personal notes relevant to student or coursework."
-type ToolNotebook {
+type ToolNotebook implements CourseTool {
     name: String
     category: String
     image: String
@@ -136,7 +136,7 @@ type CourseNote {
 }
 
 "Announcements related to the course."
-type ToolAnnouncements {
+type ToolAnnouncements implements CourseTool {
     name: String
     category: String
     image: String
@@ -155,7 +155,7 @@ type CourseAnnouncement {
 }
 
 "Course forum tool."
-type ToolForums {
+type ToolForums implements CourseTool {
     name: String
     category: String
     image: String
@@ -208,7 +208,7 @@ type CourseForumPost {
 }
 
 "A comprehensive diary/calendar tool"
-type ToolAgenda {
+type ToolAgenda implements CourseTool {
     name: String
     category: String
     image: String
@@ -228,7 +228,7 @@ type CourseAgendaEvent {
     backgroundColor: String
 }
 
-type ToolDocuments {
+type ToolDocuments implements CourseTool {
     name: String
     category: String
     image: String
@@ -248,7 +248,7 @@ type CourseDocument {
 }
 
 "A specific sequence of learning objects/experiences."
-type ToolLearningPath {
+type ToolLearningPath implements CourseTool {
     name: String
     category: String
     image: String
@@ -298,9 +298,14 @@ type SessionCategory {
     endDate: DateTime
 }
 
-# Unions
+# Interfaces
 
-union CourseTool = ToolDescription | ToolAnnouncements | ToolNotebook | ToolForums | ToolAgenda | ToolDocuments | ToolLearningPath
+interface CourseTool {
+  name: String
+  category: String
+  image: String
+  customIcon: String
+}
 
 # Enums