From f81ca1a16ac0b44af9eb0ae6a1c21abf5c911b49 Mon Sep 17 00:00:00 2001 From: Alec Aivazis Date: Fri, 8 Mar 2024 20:01:55 -0800 Subject: [PATCH] thread cache reference to document plugins --- .../src/runtime/client/documentStore.test.ts | 2 + packages/houdini/src/runtime/client/index.ts | 47 +++++++++++-------- .../src/runtime/client/plugins/fragment.ts | 4 +- .../src/runtime/client/plugins/query.ts | 4 +- .../runtime/client/utils/documentPlugins.ts | 11 +++-- 5 files changed, 39 insertions(+), 29 deletions(-) diff --git a/packages/houdini/src/runtime/client/documentStore.test.ts b/packages/houdini/src/runtime/client/documentStore.test.ts index c6e291eeca..a3a5e10111 100644 --- a/packages/houdini/src/runtime/client/documentStore.test.ts +++ b/packages/houdini/src/runtime/client/documentStore.test.ts @@ -1221,6 +1221,8 @@ export function createFragmentStore( date1: 'Date', date2: 'Date', }, + defaults: {}, + runtimeScalars: {}, }, pluginData: {}, }, diff --git a/packages/houdini/src/runtime/client/index.ts b/packages/houdini/src/runtime/client/index.ts index 7dfad634c5..e6f81320b6 100644 --- a/packages/houdini/src/runtime/client/index.ts +++ b/packages/houdini/src/runtime/client/index.ts @@ -45,13 +45,14 @@ export class HoudiniClient { // the URL of the api url: string - // the list of plugins for the client - readonly plugins: ClientPlugin[] - // expose operations settings readonly throwOnError_operations: ThrowOnErrorOperations[] private cache: Cache | null = null + private throwOnError: ThrowOnErrorParams | undefined + private fetchParams: FetchParamFn | undefined + private pipeline: NestedList | undefined + private extraPlugins: NestedList | undefined proxies: Record< string, @@ -87,26 +88,40 @@ export class HoudiniClient { this.throwOnError_operations = throwOnError?.operations ?? [] - // a few middlewares _have_ to run to setup the pipeline - this.plugins = flatten( + let serverPort = globalThis.process?.env?.HOUDINI_PORT ?? '5173' + + // if there is no url provided then assume we are using the internal local api + this.url = + url ?? + (globalThis.window ? '' : `https://localhost:${serverPort}`) + + localApiEndpoint(getCurrentConfig()) + + this.throwOnError = throwOnError + this.fetchParams = fetchParams + this.pipeline = pipeline + this.extraPlugins = plugins + } + + get plugins(): ClientPlugin[] { + return flatten( ([] as NestedList).concat( // if they specified a throw behavior - throwOnError ? [throwOnErrorPlugin(throwOnError)] : [], - fetchParamsPlugin(fetchParams), + this.throwOnError ? [throwOnErrorPlugin(this.throwOnError)] : [], + fetchParamsPlugin(this.fetchParams), // if the user wants to specify the entire pipeline, let them do so - pipeline ?? + this.pipeline ?? // the user doesn't have a specific pipeline so we should just add their desired plugins // to the standard set ( [ // make sure that documents always work - queryPlugin, - mutationPlugin, - fragmentPlugin, + queryPlugin(this.cache), + mutationPlugin(this.cache), + fragmentPlugin(this.cache), ] as NestedList ).concat( // add the specified middlewares - plugins ?? [], + this.extraPlugins ?? [], // and any middlewares we got from plugins pluginsFromPlugins, // if they provided a fetch function, use it as the body for the fetch middleware @@ -114,14 +129,6 @@ export class HoudiniClient { ) ) ) - - let serverPort = globalThis.process?.env?.HOUDINI_PORT ?? '5173' - - // if there is no url provided then assume we are using the internal local api - this.url = - url ?? - (globalThis.window ? '' : `https://localhost:${serverPort}`) + - localApiEndpoint(getCurrentConfig()) } observe<_Data extends GraphQLObject, _Input extends GraphQLVariables>({ diff --git a/packages/houdini/src/runtime/client/plugins/fragment.ts b/packages/houdini/src/runtime/client/plugins/fragment.ts index 6fbcd8aa8c..3ea5aafade 100644 --- a/packages/houdini/src/runtime/client/plugins/fragment.ts +++ b/packages/houdini/src/runtime/client/plugins/fragment.ts @@ -1,12 +1,10 @@ -import cache from '../../cache' import { deepEquals } from '../../lib/deepEquals' import { type SubscriptionSpec, ArtifactKind, DataSource } from '../../lib/types' -import type { ClientPlugin } from '../documentStore' import { documentPlugin } from '../utils' // the purpose of the fragment plugin is to provide fine-reactivity for cache updates // there are no network requests that get sent. send() always returns the initial value -export const fragment: ClientPlugin = documentPlugin(ArtifactKind.Fragment, function () { +export const fragment = documentPlugin(ArtifactKind.Fragment, function (cache) { // track the bits of state we need to hold onto let subscriptionSpec: SubscriptionSpec | null = null diff --git a/packages/houdini/src/runtime/client/plugins/query.ts b/packages/houdini/src/runtime/client/plugins/query.ts index 381ef4f865..4958bf8b0a 100644 --- a/packages/houdini/src/runtime/client/plugins/query.ts +++ b/packages/houdini/src/runtime/client/plugins/query.ts @@ -1,10 +1,8 @@ -import cache from '../../cache' import type { RuntimeScalarPayload } from '../../lib' import { type SubscriptionSpec, ArtifactKind, DataSource } from '../../lib/types' -import type { ClientPlugin } from '../documentStore' import { documentPlugin } from '../utils' -export const query: ClientPlugin = documentPlugin(ArtifactKind.Query, function () { +export const query = documentPlugin(ArtifactKind.Query, function (cache) { // track the bits of state we need to hold onto let subscriptionSpec: SubscriptionSpec | null = null diff --git a/packages/houdini/src/runtime/client/utils/documentPlugins.ts b/packages/houdini/src/runtime/client/utils/documentPlugins.ts index 5d23f9d85e..07e8421418 100644 --- a/packages/houdini/src/runtime/client/utils/documentPlugins.ts +++ b/packages/houdini/src/runtime/client/utils/documentPlugins.ts @@ -1,3 +1,5 @@ +import cache from '../../cache' +import type { Cache } from '../../cache/cache' import type { ArtifactKinds } from '../../lib/types' import type { ClientPlugin, @@ -6,10 +8,13 @@ import type { ClientHooks, } from '../documentStore' -export const documentPlugin = (kind: ArtifactKinds, source: () => ClientHooks): ClientPlugin => { - return () => { +export const documentPlugin = ( + kind: ArtifactKinds, + source: (cache: Cache) => ClientHooks +): ((cache: Cache | null) => ClientPlugin) => { + return (cacheRef: Cache | null) => () => { // pull out the hooks we care about - const sourceHandlers = source() + const sourceHandlers = source(cacheRef ?? cache) const enterWrapper = ( handler?: ClientPluginEnterPhase