Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
af0d080
Adds useDeferStream to Realtime
dthyresson Sep 25, 2023
801990c
Don't overwrite the result with just data and errors to support repea…
dthyresson Sep 25, 2023
ee4a4ea
change option to enableDeferStream
dthyresson Sep 25, 2023
f1a6c8a
Adds defer and stream examples to RT setup
dthyresson Sep 25, 2023
ba27ce4
Rework defer and stream example templates
dthyresson Sep 25, 2023
1423fc8
countdown example now uses Repeater
dthyresson Sep 25, 2023
fd7966a
Merge branch 'main' into dt-realtime-defer-stream
dthyresson Sep 25, 2023
8fc3e19
Merge branch 'main' into dt-realtime-defer-stream
dthyresson Oct 16, 2023
fdf42b0
Documents realtime and defer and stream
dthyresson Oct 16, 2023
471cb34
clarify docs of SSE and serverful deploy
dthyresson Oct 16, 2023
8bb557b
Merge branch 'main' into dt-realtime-defer-stream
dthyresson Oct 20, 2023
5332b66
Updates realtime config docs
dthyresson Oct 20, 2023
561b2ab
Support schema coordinates. Adds enable defer to template
dthyresson Oct 23, 2023
ef65e05
Merge branch 'main' into dt-realtime-defer-stream
dthyresson Oct 23, 2023
53b2b7c
Update docs/docs/realtime.md
dthyresson Oct 31, 2023
5244e11
Update docs/docs/realtime.md
dthyresson Oct 31, 2023
bdf80bd
Remove duplicate enableDeferStream
dthyresson Oct 31, 2023
7576e9d
Update docs/docs/realtime.md
dthyresson Oct 31, 2023
7f1081b
Update docs/docs/realtime.md
dthyresson Oct 31, 2023
d8e0432
Update docs/docs/realtime.md
dthyresson Oct 31, 2023
83e4211
Update docs/docs/realtime.md
dthyresson Oct 31, 2023
b7748db
Delete packages/cli/src/commands/experimental/templates/defer/.keep
dthyresson Oct 31, 2023
c476f75
Delete packages/cli/src/commands/experimental/templates/stream/.keep
dthyresson Oct 31, 2023
41d4b16
Merge branch 'main' into dt-realtime-defer-stream
dthyresson Oct 31, 2023
8489f2d
Adds examples for stream and defer in docs
dthyresson Oct 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds defer and stream examples to RT setup
  • Loading branch information
dthyresson committed Sep 25, 2023
commit f1a6c8a59b0077905ab34f7ac1e6d6a930cc320c
120 changes: 120 additions & 0 deletions packages/cli/src/commands/experimental/setupRealtimeHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,126 @@ export async function handler({ force, includeExamples, verbose }) {
]
},
},

{
title: 'Adding Defer example queries ...',
enabled: () => includeExamples,
task: () => {
// sdl

const exampleSdlTemplateContent = fs.readFileSync(
path.resolve(
__dirname,
'templates',
'defer',
'fastAndSlowFields',
`fastAndSlowFields.sdl.ts.template`
),
'utf-8'
)

const sdlFile = path.join(
redwoodPaths.api.graphql,
`fastAndSlowFields.sdl.${isTypeScriptProject() ? 'ts' : 'js'}`
)

const sdlContent = ts
? exampleSdlTemplateContent
: transformTSToJS(sdlFile, exampleSdlTemplateContent)

// service

const exampleServiceTemplateContent = fs.readFileSync(
path.resolve(
__dirname,
'templates',
'defer',
'fastAndSlowFields',
`fastAndSlowFields.ts.template`
),
'utf-8'
)
const serviceFile = path.join(
redwoodPaths.api.services,
'fastAndSlowFields',
`fastAndSlowFields.${isTypeScriptProject() ? 'ts' : 'js'}`
)

const serviceContent = ts
? exampleServiceTemplateContent
: transformTSToJS(serviceFile, exampleServiceTemplateContent)

// write all files
return [
writeFile(sdlFile, sdlContent, {
overwriteExisting: force,
}),
writeFile(serviceFile, serviceContent, {
overwriteExisting: force,
}),
]
},
},

{
title: 'Adding Stream example queries ...',
enabled: () => includeExamples,
task: () => {
// sdl

const exampleSdlTemplateContent = fs.readFileSync(
path.resolve(
__dirname,
'templates',
'stream',
'alphabet',
`alphabet.sdl.ts.template`
),
'utf-8'
)

const sdlFile = path.join(
redwoodPaths.api.graphql,
`alphabet.sdl.${isTypeScriptProject() ? 'ts' : 'js'}`
)

const sdlContent = ts
? exampleSdlTemplateContent
: transformTSToJS(sdlFile, exampleSdlTemplateContent)

// service

const exampleServiceTemplateContent = fs.readFileSync(
path.resolve(
__dirname,
'templates',
'stream',
'alphabet',
`alphabet.ts.template`
),
'utf-8'
)
const serviceFile = path.join(
redwoodPaths.api.services,
'alphabet',
`alphabet.${isTypeScriptProject() ? 'ts' : 'js'}`
)

const serviceContent = ts
? exampleServiceTemplateContent
: transformTSToJS(serviceFile, exampleServiceTemplateContent)

// write all files
return [
writeFile(sdlFile, sdlContent, {
overwriteExisting: force,
}),
writeFile(serviceFile, serviceContent, {
overwriteExisting: force,
}),
]
},
},
{
title: 'Adding config to redwood.toml...',
task: (_ctx, task) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const schema = gql`
type Query {
"""
A field that resolves fast.
"""
fastField: String! @skipAuth

"""
A field that resolves slowly.
Maybe you want to @defer this field ;)
"""
slowField(waitFor: Int! = 5000): String @skipAuth
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Repeater } from '@redwoodjs/realtime'

import { logger } from 'src/lib/logger'

const wait = (time: number) =>
new Promise((resolve) => setTimeout(resolve, time))

export const fastField = async () => {
await wait(100)
return 'I am speedyyy'
}

export const slowField = async (_, { waitFor = 5000 }) => {
logger.debug('waiting on slowField')
await wait(waitFor)
return 'I am slowey'
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ import subscriptions from 'src/subscriptions/**/*.{js,ts}'
* Redwood Realtime
* - uses a publish/subscribe model to broadcast data to clients.
* - uses a store to persist Live Query and Subscription data.
* - and enable defer and stream directives to improve latency
* for clients by sending data the most important data as soon as it's ready.
*
* Redwood Realtime supports in-memory and Redis stores:
* - In-memory stores are useful for development and testing.
* - Redis stores are useful for production.
*
*/
export const realtime: RedwoodRealtimeOptions = {
enableDeferStream: true,
subscriptions: {
subscriptions,
store: 'in-memory',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const schema = gql`
type Query {
"""
A field that spells out the letters of the alphabet
Maybe you want to @stream this field ;)
"""
alphabet: [String!]! @skipAuth
}
`
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Repeater } from '@redwoodjs/realtime'

import { logger } from 'src/lib/logger'

export const alphabet = async () => {
return new Repeater<string>(async (push, stop) => {
const values = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
const publish = () => {
const value = values.shift()

if (value) {
logger.debug({ value }, 'publishing')
push(value)
}

if (values.length === 0) {
stop()
}
}

const interval = setInterval(publish, 1000)

stop.then(() => {
logger.debug('cancel')
clearInterval(interval)
})

publish()
})
}