Skip to content

Commit 37ea03d

Browse files
committed
feat(esm): Support ESM in GQL Realtime setup and generation (#375)
1 parent 8de3f16 commit 37ea03d

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed

packages/cli/src/commands/generate/realtime/__tests__/realtimeHandler.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { handler } from '../realtimeHandler.js'
88
const mocks = vi.hoisted(() => ({
99
realtimeTs: '',
1010
serverTs: '',
11+
isEsm: false,
1112
writtenFiles: {} as Record<string, string>,
1213
}))
1314

@@ -57,6 +58,7 @@ vi.mock('@cedarjs/project-config', () => ({
5758
},
5859
getConfig: () => ({ experimental: { streamingSsr: { enabled: false } } }),
5960
resolveFile: (path: string) => path,
61+
projectIsEsm: () => mocks.isEsm,
6062
}))
6163

6264
beforeEach(() => {
@@ -65,6 +67,7 @@ beforeEach(() => {
6567
vi.spyOn(process, 'exit').mockImplementation(() => void 0 as never)
6668
mocks.realtimeTs = 'export const realtime: RedwoodRealtimeOptions = {}'
6769
mocks.serverTs = 'export const serverFile: RedwoodServerFileOptions = {}'
70+
mocks.isEsm = false
6871
})
6972

7073
afterEach(() => {
@@ -122,4 +125,18 @@ describe('realtimeHandler', () => {
122125
"pubSub.subscribe('Foobar', id)",
123126
)
124127
})
128+
129+
it('should use the correct graphql-tag import in ESM projects', async () => {
130+
mocks.isEsm = true
131+
132+
await handler({
133+
name: 'foobar',
134+
type: 'subscription',
135+
silent: true,
136+
})
137+
138+
expect(mocks.writtenFiles['foobar/foobar.ts']).toMatch(
139+
"import { gql } from 'graphql-tag'",
140+
)
141+
})
125142
})

packages/cli/src/commands/generate/realtime/realtimeHandler.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import pluralize, { singular } from 'pluralize'
77
import prompts from 'prompts'
88

99
import { generate as generateTypes } from '@cedarjs/internal/dist/generate/generate'
10+
import { projectIsEsm } from '@cedarjs/project-config'
1011
import { errorTelemetry } from '@cedarjs/telemetry'
1112

1213
// Move this check out of experimental when server file is moved as well
@@ -144,6 +145,18 @@ export async function handler({ name, type, force, verbose, silent }) {
144145
exampleSubscriptionTemplateContent,
145146
)
146147

148+
let blankTemplateContent = await generateTemplate(
149+
setupScriptContent,
150+
templateVariables(name),
151+
)
152+
153+
if (projectIsEsm()) {
154+
blankTemplateContent = blankTemplateContent.replace(
155+
"import gql from 'graphql-tag'",
156+
"import { gql } from 'graphql-tag'",
157+
)
158+
}
159+
147160
// write all files
148161
return [
149162
writeFile(
@@ -160,16 +173,9 @@ export async function handler({ name, type, force, verbose, silent }) {
160173
overwriteExisting: force,
161174
},
162175
),
163-
writeFile(
164-
exampleFile,
165-
await generateTemplate(
166-
setupScriptContent,
167-
templateVariables(name),
168-
),
169-
{
170-
overwriteExisting: force,
171-
},
172-
),
176+
writeFile(exampleFile, blankTemplateContent, {
177+
overwriteExisting: force,
178+
}),
173179
]
174180
},
175181
},

packages/cli/src/commands/setup/realtime/realtimeHandler.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Listr } from 'listr2'
55

66
import { addApiPackages } from '@cedarjs/cli-helpers'
77
import { generate as generateTypes } from '@cedarjs/internal/dist/generate/generate'
8+
import { projectIsEsm } from '@cedarjs/project-config'
89
import { errorTelemetry } from '@cedarjs/telemetry'
910

1011
import c from '../../../lib/colors.js'
@@ -61,7 +62,7 @@ export async function handler({ force, includeExamples, verbose }) {
6162
title: 'Adding Countdown example subscription ...',
6263
enabled: () => includeExamples,
6364
task: async () => {
64-
const exampleSubscriptionTemplateContent = fs.readFileSync(
65+
let exampleSubscriptionTemplateContent = fs.readFileSync(
6566
path.resolve(
6667
import.meta.dirname,
6768
'templates',
@@ -72,6 +73,14 @@ export async function handler({ force, includeExamples, verbose }) {
7273
'utf-8',
7374
)
7475

76+
if (projectIsEsm()) {
77+
exampleSubscriptionTemplateContent =
78+
exampleSubscriptionTemplateContent.replace(
79+
"import gql from 'graphql-tag'",
80+
"import { gql } from 'graphql-tag'",
81+
)
82+
}
83+
7584
const exampleFile = path.join(
7685
redwoodPaths.api.subscriptions,
7786
'countdown',
@@ -142,17 +151,25 @@ export async function handler({ force, includeExamples, verbose }) {
142151

143152
// subscription
144153

145-
const exampleSubscriptionTemplateContent = fs.readFileSync(
154+
let exampleSubscriptionTemplateContent = fs.readFileSync(
146155
path.resolve(
147156
import.meta.dirname,
148157
'templates',
149158
'subscriptions',
150159
'newMessage',
151-
`newMessage.ts.template`,
160+
'newMessage.ts.template',
152161
),
153162
'utf-8',
154163
)
155164

165+
if (projectIsEsm()) {
166+
exampleSubscriptionTemplateContent =
167+
exampleSubscriptionTemplateContent.replace(
168+
"import gql from 'graphql-tag'",
169+
"import { gql } from 'graphql-tag'",
170+
)
171+
}
172+
156173
const exampleFile = path.join(
157174
redwoodPaths.api.subscriptions,
158175
'newMessage',

0 commit comments

Comments
 (0)