Skip to content

Commit

Permalink
chore: RedwoodJS GraphQL subscription templates now use Dynamic Topic…
Browse files Browse the repository at this point in the history
… IDs (redwoodjs#8766)

* Update pub sub types to send dynamic id

* Updates realtime channel type

* Fix type
  • Loading branch information
dthyresson authored and jtoar committed Jun 30, 2023
1 parent a714041 commit 5d5b156
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const liveQueryStore = new InMemoryLiveQueryStore()
// Note: If you did not setup examples, please replace with your current subscriptions
import type { NewMessageChannel } from 'src/subscriptions/newMessage/newMessage'

// then use merge the types by intersecting each like: `NewMessageChannel<string> & OtherChannel`
export type PubSubChannels = NewMessageChannel<string>
// then use merge the types by intersecting each like: `NewMessageChannel & OtherChannel`
export type PubSubChannels = NewMessageChannel

export const pubSub = createPubSub<PubSubChannels>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const ${subscriptionServiceResolver} = async (

const { id, from, body } = input

context.pubSub.publish(`${typeName}:<%= '$' %>{id}`, { from, body })
context.pubSub.publish('${typeName}', id, { from, body })

return input
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const schema = gql`
}
`
// Be sure to import this type and then in `api/src/lib/realtime.ts` extend PubSubChannels:
// export type PubSubChannels = ExistingChannel<string> & Publish${typeName}Channel<string>
export type Publish${typeName}Channel<T extends string> = {
[Key in `${typeName}:<%= '$' %>{T}`]: [payload: { from: string; body: string }]
// export type PubSubChannels = ExistingChannel<string> & Publish${typeName}Channel
export type Publish${typeName}Channel = {
${typeName}: [id: string, payload: { from: string; body: string }]
}

/**
Expand Down Expand Up @@ -38,7 +38,7 @@ const ${subscriptionName}Subscription = {
subscribe: (_, { id }, { pubSub }) => {
logger.debug({ id }, '${name} subscription')

return pubSub.subscribe(`${typeName}:<%= '$' %>{id}`)
return pubSub.subscribe('${typeName}', id)
},
resolve: (payload) => {
logger.debug({ payload }, '${name} subscription resolve')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export const schema = gql`
newMessage(roomId: ID!): Message!
}
`
export type NewMessageChannel<T extends string> = {
[Key in `newMessage:${T}`]: [payload: { from: string; body: string }]
export type NewMessageChannel = {
newMessage: [roomId: string, payload: { from: string; body: string }]
}

/**
Expand Down Expand Up @@ -36,7 +36,7 @@ const newMessage = {
subscribe: (_, { roomId }, { pubSub }) => {
logger.debug({ roomId }, 'newMessage subscription')

return pubSub.subscribe(`newMessage:${roomId}`)
return pubSub.subscribe('newMessage', roomId)
},
resolve: (payload) => {
logger.debug({ payload }, 'newMessage subscription resolve')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const sendMessage = async (

const { roomId, from, body } = input

context.pubSub.publish(`newMessage:${roomId}`, { from, body })
context.pubSub.publish('newMessage', roomId, { from, body })

return input
}

0 comments on commit 5d5b156

Please sign in to comment.