Skip to content

Commit

Permalink
thread cache reference to document plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Mar 9, 2024
1 parent 018e0cf commit f81ca1a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
2 changes: 2 additions & 0 deletions packages/houdini/src/runtime/client/documentStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,8 @@ export function createFragmentStore(
date1: 'Date',
date2: 'Date',
},
defaults: {},
runtimeScalars: {},
},
pluginData: {},
},
Expand Down
47 changes: 27 additions & 20 deletions packages/houdini/src/runtime/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClientPlugin> | undefined
private extraPlugins: NestedList<ClientPlugin> | undefined

proxies: Record<
string,
Expand Down Expand Up @@ -87,41 +88,47 @@ 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<ClientPlugin>).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<ClientPlugin>
).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
fetchPlugin()
)
)
)

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>({
Expand Down
4 changes: 1 addition & 3 deletions packages/houdini/src/runtime/client/plugins/fragment.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 1 addition & 3 deletions packages/houdini/src/runtime/client/plugins/query.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down
11 changes: 8 additions & 3 deletions packages/houdini/src/runtime/client/utils/documentPlugins.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cache from '../../cache'
import type { Cache } from '../../cache/cache'
import type { ArtifactKinds } from '../../lib/types'
import type {
ClientPlugin,
Expand All @@ -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
Expand Down

0 comments on commit f81ca1a

Please sign in to comment.