From 20b4219c6c0fd6251e606fd57c29d6f526ff89a2 Mon Sep 17 00:00:00 2001
From: paidge <paidge_cs@hotmail.com>
Date: Mon, 10 Jan 2022 20:50:40 +0100
Subject: [PATCH] prepare fallbacks for graphQL API server

---
 README.md                                  |  4 +++-
 graphql/{index.js => clients/coindufeu.js} |  2 +-
 graphql/clients/endpoints.js               |  4 ++++
 graphql/clients/otherclient.js             | 28 ++++++++++++++++++++++
 graphql/endpoints.js                       |  3 ---
 graphql/schemaQuery.js                     |  2 +-
 nuxt.config.js                             |  4 +++-
 7 files changed, 40 insertions(+), 7 deletions(-)
 rename graphql/{index.js => clients/coindufeu.js} (94%)
 create mode 100644 graphql/clients/endpoints.js
 create mode 100644 graphql/clients/otherclient.js
 delete mode 100644 graphql/endpoints.js

diff --git a/README.md b/README.md
index 37bebb2..099cced 100644
--- a/README.md
+++ b/README.md
@@ -94,4 +94,6 @@ $ git push
 Il suffit de créer un fichier `./pages/mapage.vue` pour que la nouvelle page soit accessible directement sur http://localhost/mapage. Pensez à insérer la logique pour le fil d'ariane dans ce fichier et à mettre à jour le menu dans la variable `menus` du fichier `./layouts/default.vue`. 3 pages d'exemple sont fournies pour montrer l'utilisation de l'API graphQL et de chartJS.
 
 ## GraphQL
-Tout ce qui touche à GraphQL se trouve dans le dossier `./graphql`. Le fichier endpoints.js contient la liste des serveurs graphQL, le fichier queries.js contient toutes les requêtes de l'application.
\ No newline at end of file
+All conf files for Apollo Graphql are stored in `./graphql`.
+The file `queries.js` contains all queries.
+If you want to add a 2nd graphQL server, edit the file `./graphql/clients/endpoints.js` to put your URL and uncomment the line concerning the `apollo.clientConfigs.myotherclient` option in `nuxt.config.js`. I have not tested this functionnality.
\ No newline at end of file
diff --git a/graphql/index.js b/graphql/clients/coindufeu.js
similarity index 94%
rename from graphql/index.js
rename to graphql/clients/coindufeu.js
index 178821e..f0db19e 100644
--- a/graphql/index.js
+++ b/graphql/clients/coindufeu.js
@@ -3,7 +3,7 @@ import { setContext } from 'apollo-link-context'
 import { from } from 'apollo-link'
 
 import {ENDPOINT1} from './endpoints'
-import {cache} from './cache'
+import {cache} from '../cache'
 
 export default ctx => {
   const ssrMiddleware = setContext((_, { headers }) => {
diff --git a/graphql/clients/endpoints.js b/graphql/clients/endpoints.js
new file mode 100644
index 0000000..5108540
--- /dev/null
+++ b/graphql/clients/endpoints.js
@@ -0,0 +1,4 @@
+module.exports = {
+  ENDPOINT1 : 'https://wwgql.coinduf.eu',
+  ENDPOINT2 : 'https://myserver.com'
+}
\ No newline at end of file
diff --git a/graphql/clients/otherclient.js b/graphql/clients/otherclient.js
new file mode 100644
index 0000000..1a6b095
--- /dev/null
+++ b/graphql/clients/otherclient.js
@@ -0,0 +1,28 @@
+import { HttpLink } from 'apollo-link-http'
+import { setContext } from 'apollo-link-context'
+import { from } from 'apollo-link'
+
+import {ENDPOINT2} from './endpoints'
+import {cache} from '../cache'
+
+export default ctx => {
+  const ssrMiddleware = setContext((_, { headers }) => {
+    if (process.client) return headers
+    return {
+      headers
+    }
+  })
+
+  const httpLink = new HttpLink({
+    uri: ENDPOINT2
+  })
+
+  const link = from([ssrMiddleware, httpLink])
+
+  return {
+    link,
+    cache,
+    // https://github.com/nuxt-community/apollo-module/issues/306#issuecomment-607225431
+    defaultHttpLink: false
+  }
+}
\ No newline at end of file
diff --git a/graphql/endpoints.js b/graphql/endpoints.js
deleted file mode 100644
index 907db9d..0000000
--- a/graphql/endpoints.js
+++ /dev/null
@@ -1,3 +0,0 @@
-module.exports = {
-  ENDPOINT1 : 'https://wwgql.coinduf.eu'
-}
\ No newline at end of file
diff --git a/graphql/schemaQuery.js b/graphql/schemaQuery.js
index d9ef29c..736bec3 100644
--- a/graphql/schemaQuery.js
+++ b/graphql/schemaQuery.js
@@ -1,4 +1,4 @@
-const {ENDPOINT1} = require('./endpoints');
+const {ENDPOINT1} = require('./clients/endpoints');
 const fetch = require('node-fetch');
 const fs = require('fs');
 
diff --git a/nuxt.config.js b/nuxt.config.js
index 6ce6da7..2b2f1f8 100644
--- a/nuxt.config.js
+++ b/nuxt.config.js
@@ -79,7 +79,9 @@ export default {
 
   apollo: {
     clientConfigs: {
-      default: '~/graphql'
+      default: '~/graphql/clients/coindufeu'
+      //you can setup multiple clients with arbitrary names
+      //, myotherclient : '~/graphql/clients/otherclient'
     },
   },
 
-- 
GitLab