From c24f7daec2d66d87acc62aa4aaa046a0b6da5eaf Mon Sep 17 00:00:00 2001
From: Steve Reis <stevereis93@gmail.com>
Date: Wed, 17 Aug 2022 10:18:19 +0000
Subject: [PATCH] GitBook: [#28] No subject

---
 docs/SUMMARY.md                               |  2 +-
 .../recipes/add-a-result-type.md              | 96 +++++++++++++++++++
 .../recipes/add-a-visualization.md            |  3 -
 3 files changed, 97 insertions(+), 4 deletions(-)
 create mode 100644 docs/for-developers/recipes/add-a-result-type.md
 delete mode 100644 docs/for-developers/recipes/add-a-visualization.md

diff --git a/docs/SUMMARY.md b/docs/SUMMARY.md
index eed5cf9..9322e85 100644
--- a/docs/SUMMARY.md
+++ b/docs/SUMMARY.md
@@ -26,5 +26,5 @@
     * [Update GraphQL Queries](for-developers/frontend/Update-queries-GrahpQL-in-the-frontend.md)
   * [📖 Storybook](for-developers/frontend/visualisations.md)
 * [👩🍳 Recipes](for-developers/recipes/README.md)
-  * [🚧 Add a visualization](for-developers/recipes/add-a-visualization.md)
+  * [Add a result type](for-developers/recipes/add-a-result-type.md)
   * [Add an algorithm handler on Exareme](for-developers/recipes/add-an-algorithm-handler-on-exareme.md)
diff --git a/docs/for-developers/recipes/add-a-result-type.md b/docs/for-developers/recipes/add-a-result-type.md
new file mode 100644
index 0000000..565d1e7
--- /dev/null
+++ b/docs/for-developers/recipes/add-a-result-type.md
@@ -0,0 +1,96 @@
+---
+description: This page describe the process to add a type of result
+---
+
+# Add a result type
+
+Result's types are defined in the gateway, in order to add a new type of result for the Frontend we need to create a new kind of result inside the gateway.&#x20;
+
+All results are stored inside the `engine` module, specifically inside the `src/engine/models/result` folder.&#x20;
+
+Let's create a model for the demonstration
+
+{% code title="my-custom.model.ts" lineNumbers="true" %}
+```typescript
+@ObjectType()
+export class MyCustomResult extends Result {
+    @Field()
+    name: string;
+    
+    @Field()
+    customProperty: string;
+}
+```
+{% endcode %}
+
+Annotations are related to GraphQL, see more about GraphQL's annotation [here](https://docs.nestjs.com/graphql/resolvers#object-types).
+
+Once our model is created we need to declare it as a part of the possible result types, for this open  the file `src/engine/models/result/common/result-union.model.ts` and add the type to the list
+
+{% code title="result-union.model.ts" lineNumbers="true" %}
+```typescript
+export const ResultUnion = createUnionType({
+  name: 'ResultUnion',
+  types: () => [
+    ...
+    MyCustomResult,
+  ],
+  resolveType(value) {
+    ...
+
+    if (value.customProperty) return MyCustomResult 
+
+    return RawResult;
+  },
+});
+
+```
+{% endcode %}
+
+{% hint style="info" %}
+The `resolveType` function help GraphQL determines the result's type when the object receive is a literal JavaScript object (not a class).
+{% endhint %}
+
+#### Integration in the Frontend
+
+First of all, as describe in [this issue](https://github.com/apollographql/apollo-client/issues/7050), we need tell Apollo which types we should expect from this `union field`. Open the file `src/components/API/GraphQL/cache.tsx` and in the cache configuration, we need to add a mention to our new type
+
+{% code title="cache.tsx" lineNumbers="true" %}
+```typescript
+export const cacheConfig = {
+  possibleTypes: {
+    // https://github.com/apollographql/apollo-client/issues/7050
+    ResultUnion: [
+      ...
+      'MyCustomResult' // <- Our new result type
+    ],
+    ...
+  },
+  ...
+}
+```
+{% endcode %}
+
+Once the possible types is defined, we need to tell Apollo what we want to extract from the the specific result.&#x20;
+
+open the file `src/components/API/GraphQL/fragments.ts` and add the details of our new result.&#x20;
+
+{% code title="fragments.ts" lineNumbers="true" %}
+```typescript
+export const coreInfoResult = gql`
+  fragment coreInfoResult on ResultUnion {
+    ... on RawResult {
+      rawdata
+    }
+    ... on MyCustomResult {
+      name
+      customProperty
+    }
+    (...)
+  }
+`;
+```
+{% endcode %}
+
+After that we are all setup to receive a `MyCustomResult` from any Experiment.
+
diff --git a/docs/for-developers/recipes/add-a-visualization.md b/docs/for-developers/recipes/add-a-visualization.md
deleted file mode 100644
index 8a4ddcd..0000000
--- a/docs/for-developers/recipes/add-a-visualization.md
+++ /dev/null
@@ -1,3 +0,0 @@
-# 🚧 Add a visualization
-
-//TODO ResultUnion, gateway and frontend
-- 
GitLab