From 1683c0d46776793211fa976d7ce32013a95a7344 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 22 Nov 2021 01:28:22 +0100 Subject: [PATCH] moved apollo cache in seperate file --- src/Cache.ts | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/index.tsx | 55 +++------------------------------------------------ 2 files changed, 58 insertions(+), 52 deletions(-) create mode 100644 src/Cache.ts diff --git a/src/Cache.ts b/src/Cache.ts new file mode 100644 index 0000000..bf69451 --- /dev/null +++ b/src/Cache.ts @@ -0,0 +1,55 @@ +import { InMemoryCache } from "@apollo/client" +import ObjID from "./types/ObjID" + +const cache = new InMemoryCache({ + typePolicies:{ + File:{ + fields:{ + id:{ + merge(_,incomming){ + // HACK: i use the merge function to change the id from a string to ObjID object. + // afaik apollo does not yet support custom scalar types. + if (!incomming){ + return incomming + }else if (incomming instanceof ObjID){ + return incomming + }else{ + return ObjID.fromString(incomming as string) + } + } + } + } + }, + Directory:{ + fields:{ + id:{ + merge(_,incomming){ + if (!incomming){ + return incomming + }else if (incomming instanceof ObjID){ + return incomming + }else{ + return ObjID.fromString(incomming as string) + } + } + } + } + }, + Query: { + fields: { + files: { + merge(existing, incoming){ + return incoming + } + }, + directorys:{ + merge(existing, incoming){ + return incoming + } + } + } + } + } +}) + +export default cache \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index 2e1e612..8b6e3e9 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -2,62 +2,13 @@ import React from "react" import ReactDOM from "react-dom" import "./index.scss" import App from "./App" -import { ApolloClient, ApolloProvider, InMemoryCache } from "@apollo/client" +import { ApolloClient, ApolloProvider } from "@apollo/client" import "react-contexify/dist/ReactContexify.css" -import ObjID from "./types/ObjID" +import cache from "./Cache" const client = new ApolloClient({ uri: "/api/graphql", - cache: new InMemoryCache({ - typePolicies:{ - File:{ - fields:{ - id:{ - merge(_,incomming){ - // HACK: i use the merge function to change the id from a string to ObjID object. - // afaik apollo does not yet support custom scalar types. - if (!incomming){ - return incomming - }else if (incomming instanceof ObjID){ - return incomming - }else{ - return ObjID.fromString(incomming as string) - } - } - } - } - }, - Directory:{ - fields:{ - id:{ - merge(_,incomming){ - if (!incomming){ - return incomming - }else if (incomming instanceof ObjID){ - return incomming - }else{ - return ObjID.fromString(incomming as string) - } - } - } - } - }, - Query: { - fields: { - files: { - merge(existing, incoming){ - return incoming - } - }, - directorys:{ - merge(existing, incoming){ - return incoming - } - } - } - } - } - }) + cache: cache, }) // Disable drag and drop behaviour on document