Skip to content

Commit ab37a2a

Browse files
committed
pnpm extension generation fixed
1 parent 0aa573f commit ab37a2a

File tree

7 files changed

+113
-64
lines changed

7 files changed

+113
-64
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
"@oclif/core": "4.4.0",
105105
"whatwg-url": "14.0.0",
106106
"supports-hyperlinks": "3.1.0",
107-
"@graphql-tools/utils": "10.7.2",
107+
"@graphql-tools/utils": "10.8.0",
108108
"@shopify/cli-hydrogen>@shopify/cli-kit": "link:./packages/cli-kit",
109109
"@shopify/cli-hydrogen>@shopify/plugin-cloudflare": "link:./packages/plugin-cloudflare",
110110
"nanoid": "3.3.8"

packages/app/src/cli/commands/app/function/typegen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default class FunctionTypegen extends AppUnlinkedCommand {
2929

3030
const ourFunction = await chooseFunction(app, flags.path)
3131

32-
await buildGraphqlTypes(ourFunction, {stdout: process.stdout, stderr: process.stderr, app})
32+
await buildGraphqlTypes(ourFunction)
3333
renderSuccess({headline: 'GraphQL types generated successfully.'})
3434

3535
return {app}

packages/app/src/cli/services/function/build.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ import {beforeEach, describe, expect, test, vi} from 'vitest'
2222
import {exec} from '@shopify/cli-kit/node/system'
2323
import {dirname, joinPath} from '@shopify/cli-kit/node/path'
2424
import {inTemporaryDirectory, mkdir, writeFile, removeFile} from '@shopify/cli-kit/node/fs'
25+
import * as fsNode from '@shopify/cli-kit/node/fs'
2526
import {build as esBuild} from 'esbuild'
27+
import {generate} from '@graphql-codegen/cli'
2628

2729
vi.mock('@shopify/cli-kit/node/system')
2830
vi.mock('esbuild', async () => {
@@ -32,6 +34,9 @@ vi.mock('esbuild', async () => {
3234
build: vi.fn(),
3335
}
3436
})
37+
vi.mock('@graphql-codegen/cli', () => ({
38+
generate: vi.fn().mockResolvedValue(undefined),
39+
}))
3540

3641
let stdout: any
3742
let stderr: any
@@ -56,15 +61,34 @@ describe('buildGraphqlTypes', () => {
5661
// Given
5762
const ourFunction = await testFunctionExtension({entryPath: 'src/index.js'})
5863

64+
// Mock the package.json
65+
const packageJson = {
66+
codegen: {
67+
schema: 'schema.graphql',
68+
documents: 'src/*.graphql',
69+
generates: {
70+
'./generated/api.ts': {
71+
plugins: ['typescript'],
72+
},
73+
},
74+
},
75+
}
76+
vi.spyOn(fsNode, 'readFile').mockImplementation(async (path: string) => {
77+
if (path === joinPath(ourFunction.directory, 'package.json')) {
78+
return JSON.stringify(packageJson) as any
79+
}
80+
return JSON.stringify({}) as any
81+
})
82+
5983
// When
60-
const got = buildGraphqlTypes(ourFunction, {stdout, stderr, signal, app})
84+
const got = buildGraphqlTypes(ourFunction)
6185

6286
// Then
6387
await expect(got).resolves.toBeUndefined()
64-
expect(exec).toHaveBeenCalledWith('npm', ['exec', '--', 'graphql-code-generator', '--config', 'package.json'], {
88+
expect(fsNode.readFile).toHaveBeenCalledWith(joinPath(ourFunction.directory, 'package.json'))
89+
expect(vi.mocked(generate)).toHaveBeenCalledWith({
6590
cwd: ourFunction.directory,
66-
stderr,
67-
signal,
91+
...packageJson.codegen,
6892
})
6993
})
7094

@@ -74,7 +98,7 @@ describe('buildGraphqlTypes', () => {
7498
ourFunction.entrySourceFilePath = 'src/main.rs'
7599

76100
// When
77-
const got = buildGraphqlTypes(ourFunction, {stdout, stderr, signal, app})
101+
const got = buildGraphqlTypes(ourFunction)
78102

79103
// Then
80104
await expect(got).rejects.toThrow(/GraphQL types can only be built for JavaScript functions/)

packages/app/src/cli/services/function/build.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {renderTasks} from '@shopify/cli-kit/node/ui'
2323
import {pickBy} from '@shopify/cli-kit/common/object'
2424
import {runWithTimer} from '@shopify/cli-kit/node/metadata'
2525
import {AbortError} from '@shopify/cli-kit/node/error'
26+
import {generate} from '@graphql-codegen/cli'
2627
import {Writable} from 'stream'
2728

2829
export const PREFERRED_FUNCTION_NPM_PACKAGE_MAJOR_VERSION = '2'
@@ -77,7 +78,7 @@ async function buildJSFunctionWithoutTasks(
7778
if (!options.signal?.aborted) {
7879
options.stdout.write(`Building function ${fun.localIdentifier}...`)
7980
options.stdout.write(`Building GraphQL types...\n`)
80-
await buildGraphqlTypes(fun, options)
81+
await buildGraphqlTypes(fun)
8182
}
8283
if (!options.signal?.aborted) {
8384
options.stdout.write(`Bundling JS function...\n`)
@@ -102,7 +103,7 @@ async function buildJSFunctionWithTasks(
102103
{
103104
title: 'Building GraphQL types',
104105
task: async () => {
105-
await buildGraphqlTypes(fun, options)
106+
await buildGraphqlTypes(fun)
106107
},
107108
},
108109
{
@@ -120,19 +121,22 @@ async function buildJSFunctionWithTasks(
120121
])
121122
}
122123

123-
export async function buildGraphqlTypes(
124-
fun: {directory: string; isJavaScript: boolean},
125-
options: JSFunctionBuildOptions,
126-
) {
124+
export async function buildGraphqlTypes(fun: {directory: string; isJavaScript: boolean}) {
127125
if (!fun.isJavaScript) {
128126
throw new Error('GraphQL types can only be built for JavaScript functions')
129127
}
130128

131129
return runWithTimer('cmd_all_timing_network_ms')(async () => {
132-
return exec('npm', ['exec', '--', 'graphql-code-generator', '--config', 'package.json'], {
130+
const packageJsonPath = joinPath(fun.directory, 'package.json')
131+
const packageJson = JSON.parse(await readFile(packageJsonPath))
132+
133+
if (!packageJson.codegen) {
134+
throw new AbortError('No `codegen` config found in package.json')
135+
}
136+
137+
return generate({
138+
...packageJson.codegen,
133139
cwd: fun.directory,
134-
stderr: options.stderr,
135-
signal: options.signal,
136140
})
137141
})
138142
}

packages/app/src/cli/services/generate/extension.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ import {joinPath, dirname} from '@shopify/cli-kit/node/path'
3232
import {slugify} from '@shopify/cli-kit/common/string'
3333

3434
vi.mock('../../models/app/validation/multi-cli-warning.js')
35+
vi.mock('../function/build.js', async () => {
36+
const actual: any = await vi.importActual('../function/build.js')
37+
return {
38+
...actual,
39+
buildGraphqlTypes: vi.fn().mockResolvedValue(undefined),
40+
}
41+
})
3542
vi.mock('@shopify/cli-kit/node/node-package-manager', async () => {
3643
const actual: any = await vi.importActual('@shopify/cli-kit/node/node-package-manager')
3744
return {
@@ -360,7 +367,7 @@ describe('initialize a extension', async () => {
360367
await file.mkdir(joinPath(destination, 'src'))
361368
await file.writeFile(joinPath(destination, 'src', 'index'), '')
362369
})
363-
const buildGraphqlTypesSpy = vi.spyOn(functionBuild, 'buildGraphqlTypes').mockResolvedValue()
370+
const buildGraphqlTypesSpy = vi.spyOn(functionBuild, 'buildGraphqlTypes').mockResolvedValue(undefined)
364371

365372
// When
366373
const extensionDir = await createFromTemplate({
@@ -408,6 +415,20 @@ describe('initialize a extension', async () => {
408415
await file.mkdir(joinPath(destination, 'src'))
409416
await file.writeFile(joinPath(destination, 'src', 'index'), '')
410417
await file.writeFile(joinPath(destination, 'src', 'run.graphql'), '')
418+
await file.writeFile(
419+
joinPath(destination, 'package.json'),
420+
JSON.stringify({
421+
codegen: {
422+
schema: 'schema.graphql',
423+
documents: 'src/*.graphql',
424+
generates: {
425+
'./generated/api.ts': {
426+
plugins: ['typescript'],
427+
},
428+
},
429+
},
430+
}),
431+
)
411432
})
412433

413434
// When

packages/app/src/cli/services/generate/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ async function functionExtensionInit({
207207
taskList.push({
208208
title: `Building GraphQL types`,
209209
task: async () => {
210-
await buildGraphqlTypes({directory, isJavaScript: true}, {stdout: process.stdout, stderr: process.stderr, app})
210+
await buildGraphqlTypes({directory, isJavaScript: true})
211211
},
212212
})
213213
}

0 commit comments

Comments
 (0)