Skip to content

Commit

Permalink
feat(realtime): Expose store and pubsub for easier access (#9138)
Browse files Browse the repository at this point in the history
**Problem**
We want to be able to do things like invalidate the live query store
from outside of a full graphql request - so without access to the
context which these properties are typically attached to.

Eg: Watching a file for changes and then invalidating a query when that
file updates.

**Changes**
1. Expose the pubsub and liveQueryStore from the realtime package
2. Minor renaming to make the exporting more convenient 

**Outstanding**
1. We may wish to do a future refactor to prevent inadvertent mutations
of these but this does not need to be done right now.
  • Loading branch information
Josh-Walker-GM authored and thedavidprice committed Sep 8, 2023
1 parent d584fd8 commit 0c79b30
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/realtime/src/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export {
liveDirectiveTypeDefs,
InMemoryLiveQueryStore,
RedisLiveQueryStore,
liveQueryStore,
pubSub,
} from './plugins/useRedwoodRealtime'

export type {
Expand Down
18 changes: 11 additions & 7 deletions packages/realtime/src/graphql/plugins/useRedwoodRealtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,19 @@ export class RedisLiveQueryStore {
}
}

// These are exported to allow access to the store and pubsub outside of graphql execution
export let liveQueryStore: LiveQueryStorageMechanism | undefined = undefined
export let pubSub: ReturnType<typeof createPubSub> | undefined = undefined

export const useRedwoodRealtime = (options: RedwoodRealtimeOptions): Plugin => {
let liveQueriesEnabled = false
let subscriptionsEnabled = false

let liveQueryPlugin = {} as Plugin
let liveQueryStorageMechanism = {} as LiveQueryStorageMechanism
const inMemoryLiveQueryStore = new InMemoryLiveQueryStore()

let pubSub = {} as ReturnType<typeof createPubSub>
liveQueryStore = {} as LiveQueryStorageMechanism
pubSub = {} as ReturnType<typeof createPubSub>

/**
* This symbol is added to the schema extensions for checking whether the live query was added to the schema only once.
Expand All @@ -162,21 +166,21 @@ export const useRedwoodRealtime = (options: RedwoodRealtimeOptions): Plugin => {
if (options.liveQueries.store === 'in-memory') {
liveQueriesEnabled = true

liveQueryStorageMechanism = inMemoryLiveQueryStore
liveQueryStore = inMemoryLiveQueryStore
liveQueryPlugin = useLiveQuery({
liveQueryStore: liveQueryStorageMechanism,
liveQueryStore,
})
} else if (options.liveQueries.store.redis) {
liveQueriesEnabled = true

liveQueryStorageMechanism = new RedisLiveQueryStore(
liveQueryStore = new RedisLiveQueryStore(
options.liveQueries.store.redis.publishClient,
options.liveQueries.store.redis.subscribeClient,
options.liveQueries.store.redis.channel || 'live-query-invalidations',
inMemoryLiveQueryStore
) as unknown as InMemoryLiveQueryStore
liveQueryPlugin = useLiveQuery({
liveQueryStore: liveQueryStorageMechanism,
liveQueryStore,
})
} else {
throw new Error('Invalid live query store configuration.')
Expand Down Expand Up @@ -228,7 +232,7 @@ export const useRedwoodRealtime = (options: RedwoodRealtimeOptions): Plugin => {
onContextBuilding() {
return ({ extendContext }) => {
extendContext({
liveQueryStore: liveQueriesEnabled && liveQueryStorageMechanism,
liveQueryStore: liveQueriesEnabled && liveQueryStore,
pubSub: subscriptionsEnabled && pubSub,
})
}
Expand Down
2 changes: 2 additions & 0 deletions packages/realtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export {
liveDirectiveTypeDefs,
InMemoryLiveQueryStore,
RedisLiveQueryStore,
liveQueryStore,
pubSub,
} from './graphql'

export type {
Expand Down

0 comments on commit 0c79b30

Please sign in to comment.