From c44a2f42b4d8049a85af6d2703c78c2e173ac85b Mon Sep 17 00:00:00 2001 From: paidge <paidge_cs@hotmail.com> Date: Wed, 5 Jan 2022 03:06:37 +0100 Subject: [PATCH] factorization of the graphQL layer for future alt servers --- graphql/cache.js | 21 +++++++++++++++++++++ graphql/endpoint.js | 3 --- graphql/endpoints.js | 3 +++ graphql/index.js | 29 ++++------------------------- graphql/schemaQuery.js | 4 ++-- 5 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 graphql/cache.js delete mode 100644 graphql/endpoint.js create mode 100644 graphql/endpoints.js diff --git a/graphql/cache.js b/graphql/cache.js new file mode 100644 index 0000000..a8035d3 --- /dev/null +++ b/graphql/cache.js @@ -0,0 +1,21 @@ +import { InMemoryCache, IntrospectionFragmentMatcher, defaultDataIdFromObject } from 'apollo-cache-inmemory' +import introspectionQueryResultData from './fragmentTypes.json'; + +const fragmentMatcher = new IntrospectionFragmentMatcher({ + introspectionQueryResultData + }) + + // Apparemment il faut utiliser la syntaxe Apollo v2 +export const cache = new InMemoryCache({ + addTypename: false, + fragmentMatcher, + dataIdFromObject: object => { + switch (object.__typename) { + case 'Identity': return object.hash + case 'Event': return object.block.number + case 'EventId': return `${object.member.hash}:${object.inOut}` + case 'Forecast': return `${object.member.hash}:${object.date}:${object.after}:${object.proba}` + default: return defaultDataIdFromObject(object); // fall back to default handling + } + } +}) \ No newline at end of file diff --git a/graphql/endpoint.js b/graphql/endpoint.js deleted file mode 100644 index 2d504da..0000000 --- a/graphql/endpoint.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - ENDPOINT : 'https://wwgql.coinduf.eu' -} \ No newline at end of file diff --git a/graphql/endpoints.js b/graphql/endpoints.js new file mode 100644 index 0000000..907db9d --- /dev/null +++ b/graphql/endpoints.js @@ -0,0 +1,3 @@ +module.exports = { + ENDPOINT1 : 'https://wwgql.coinduf.eu' +} \ No newline at end of file diff --git a/graphql/index.js b/graphql/index.js index 03471a5..c8e3991 100644 --- a/graphql/index.js +++ b/graphql/index.js @@ -1,10 +1,9 @@ import { HttpLink } from 'apollo-link-http' import { setContext } from 'apollo-link-context' import { from } from 'apollo-link' -import { InMemoryCache, IntrospectionFragmentMatcher, defaultDataIdFromObject } from 'apollo-cache-inmemory' -import {ENDPOINT} from './endpoint' -import introspectionQueryResultData from './fragmentTypes.json'; +import {ENDPOINT1} from './endpoints' +import {cache} from './cache' export default ctx => { const ssrMiddleware = setContext((_, { headers }) => { @@ -15,33 +14,13 @@ export default ctx => { }) const httpLink = new HttpLink({ - uri: ENDPOINT + uri: ENDPOINT1 }) const link = from([ssrMiddleware, httpLink]) - const fragmentMatcher = new IntrospectionFragmentMatcher({ - introspectionQueryResultData - }) - - // Apparemment il faut utiliser la syntaxe Apollo v2 - const cache = new InMemoryCache({ - addTypename: false, - fragmentMatcher, - dataIdFromObject: object => { - switch (object.__typename) { - case 'Identity': return object.hash - case 'Event': return object.block.number - case 'EventId': return `${object.member.hash}:${object.inOut}` - case 'Forecast': return `${object.member.hash}:${object.date}:${object.after}:${object.proba}` - default: return defaultDataIdFromObject(object); // fall back to default handling - } - } - }) - return { link, - cache, - defaultHttpLink: false + cache } } \ No newline at end of file diff --git a/graphql/schemaQuery.js b/graphql/schemaQuery.js index 1745426..d9ef29c 100644 --- a/graphql/schemaQuery.js +++ b/graphql/schemaQuery.js @@ -1,8 +1,8 @@ -const {ENDPOINT} = require('./endpoint'); +const {ENDPOINT1} = require('./endpoints'); const fetch = require('node-fetch'); const fs = require('fs'); -fetch(ENDPOINT, { +fetch(ENDPOINT1, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ -- GitLab