From 7d37e3e5649fbcdc963261f100c3e0332febd834 Mon Sep 17 00:00:00 2001 From: InfiniteTF Date: Wed, 25 Nov 2020 23:50:36 +0100 Subject: [PATCH] Add type policies for entity fetch queries (#958) --- ui/v2.5/src/core/createClient.ts | 61 +++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/ui/v2.5/src/core/createClient.ts b/ui/v2.5/src/core/createClient.ts index 658b6d51d..c64e74fc1 100644 --- a/ui/v2.5/src/core/createClient.ts +++ b/ui/v2.5/src/core/createClient.ts @@ -4,12 +4,71 @@ import { split, from, ServerError, + TypePolicies, } from "@apollo/client"; import { WebSocketLink } from "@apollo/client/link/ws"; import { onError } from "@apollo/client/link/error"; import { getMainDefinition } from "@apollo/client/utilities"; import { createUploadLink } from "apollo-upload-client"; +// Policies that tell apollo what the type of the returned object will be. +// In many cases this allows it to return from cache immediately rather than fetching. +const typePolicies: TypePolicies = { + Query: { + fields: { + findImage: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Image", + id: args?.id, + }), + }, + findPerformer: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Performer", + id: args?.id, + }), + }, + findStudio: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Studio", + id: args?.id, + }), + }, + findMovie: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Movie", + id: args?.id, + }), + }, + findGallery: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Gallery", + id: args?.id, + }), + }, + findScene: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Scene", + id: args?.id, + }), + }, + findTag: { + read: (_, { args, toReference }) => + toReference({ + __typename: "Tag", + id: args?.id, + }), + }, + }, + }, +}; + export const getPlatformURL = (ws?: boolean) => { const platformUrl = new URL(window.location.origin); @@ -73,7 +132,7 @@ export const createClient = () => { const link = from([errorLink, splitLink]); - const cache = new InMemoryCache(); + const cache = new InMemoryCache({ typePolicies }); const client = new ApolloClient({ link, cache,