Skip to content

Commit 1fa637d

Browse files
committed
move themeCreate to gql
1 parent 7db60d6 commit 1fa637d

File tree

13 files changed

+223
-219
lines changed

13 files changed

+223
-219
lines changed

packages/app/src/cli/utilities/extensions/theme/host-theme-manager.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {waitForThemeToBeProcessed} from './host-theme-watcher.js'
22
import {HostThemeManager, DEFAULT_THEME_ZIP, FALLBACK_THEME_ZIP} from './host-theme-manager.js'
3-
import {createTheme} from '@shopify/cli-kit/node/themes/api'
3+
import {themeCreate} from '@shopify/cli-kit/node/themes/api'
44
import {beforeEach, describe, expect, test, vi} from 'vitest'
55
import {DEVELOPMENT_THEME_ROLE} from '@shopify/cli-kit/node/themes/utils'
66
import {AdminSession} from '@shopify/cli-kit/node/session'
@@ -18,8 +18,8 @@ describe('HostThemeManager', () => {
1818
vi.spyOn(ThemeManager.prototype, 'generateThemeName').mockImplementation(() => 'App Ext. Host Name')
1919
})
2020

21-
test('should call createTheme with the provided name and src param', async () => {
22-
vi.mocked(createTheme).mockResolvedValue({
21+
test('should call themeCreate with the provided name and src param', async () => {
22+
vi.mocked(themeCreate).mockResolvedValue({
2323
id: 12345,
2424
name: 'Theme',
2525
role: 'development',
@@ -31,7 +31,7 @@ describe('HostThemeManager', () => {
3131
await themeManager.findOrCreate()
3232

3333
// Then
34-
expect(createTheme).toHaveBeenCalledWith(
34+
expect(themeCreate).toHaveBeenCalledWith(
3535
{
3636
name: 'App Ext. Host Name',
3737
role: DEVELOPMENT_THEME_ROLE,
@@ -42,9 +42,9 @@ describe('HostThemeManager', () => {
4242
})
4343

4444
describe('dev preview', () => {
45-
test('should call createTheme with the provided name and src param', async () => {
45+
test('should call themeCreate with the provided name and src param', async () => {
4646
// Given
47-
vi.mocked(createTheme).mockResolvedValue({
47+
vi.mocked(themeCreate).mockResolvedValue({
4848
id: 12345,
4949
name: 'Theme',
5050
role: 'development',
@@ -56,7 +56,7 @@ describe('HostThemeManager', () => {
5656
await themeManager.findOrCreate()
5757

5858
// Then
59-
expect(createTheme).toHaveBeenCalledWith(
59+
expect(themeCreate).toHaveBeenCalledWith(
6060
{
6161
name: 'App Ext. Host Name',
6262
role: DEVELOPMENT_THEME_ROLE,
@@ -68,7 +68,7 @@ describe('HostThemeManager', () => {
6868

6969
test('should wait for the theme to be processed', async () => {
7070
// Given
71-
vi.mocked(createTheme).mockResolvedValue({
71+
vi.mocked(themeCreate).mockResolvedValue({
7272
id: 12345,
7373
name: 'Theme',
7474
role: 'development',
@@ -86,7 +86,7 @@ describe('HostThemeManager', () => {
8686

8787
test('should retry creating the theme if the first attempt fails', async () => {
8888
// Given
89-
vi.mocked(createTheme).mockResolvedValueOnce(undefined).mockResolvedValueOnce({
89+
vi.mocked(themeCreate).mockResolvedValueOnce(undefined).mockResolvedValueOnce({
9090
id: 12345,
9191
name: 'Theme',
9292
role: 'development',
@@ -98,8 +98,8 @@ describe('HostThemeManager', () => {
9898
await themeManager.findOrCreate()
9999

100100
// Then
101-
expect(createTheme).toHaveBeenCalledTimes(2)
102-
expect(createTheme).toHaveBeenNthCalledWith(
101+
expect(themeCreate).toHaveBeenCalledTimes(2)
102+
expect(themeCreate).toHaveBeenNthCalledWith(
103103
1,
104104
{
105105
role: DEVELOPMENT_THEME_ROLE,
@@ -108,7 +108,7 @@ describe('HostThemeManager', () => {
108108
},
109109
adminSession,
110110
)
111-
expect(createTheme).toHaveBeenNthCalledWith(
111+
expect(themeCreate).toHaveBeenNthCalledWith(
112112
2,
113113
{
114114
role: DEVELOPMENT_THEME_ROLE,
@@ -121,7 +121,7 @@ describe('HostThemeManager', () => {
121121

122122
test('should gracefully handle a 422 from the server during theme creation', async () => {
123123
// Given
124-
vi.mocked(createTheme)
124+
vi.mocked(themeCreate)
125125
.mockRejectedValueOnce(new Error('API request unprocessable content: {"src":["is empty"]}'))
126126
.mockRejectedValueOnce(new Error('API request unprocessable content: {"src":["is empty"]}'))
127127
.mockResolvedValueOnce({
@@ -136,12 +136,12 @@ describe('HostThemeManager', () => {
136136
await themeManager.findOrCreate()
137137

138138
// Then
139-
expect(createTheme).toHaveBeenCalledTimes(3)
139+
expect(themeCreate).toHaveBeenCalledTimes(3)
140140
})
141141

142142
test('should retry creating the theme with the Fallback theme zip after 3 failed retry attempts', async () => {
143143
// Given
144-
vi.mocked(createTheme)
144+
vi.mocked(themeCreate)
145145
.mockResolvedValueOnce(undefined)
146146
.mockResolvedValueOnce(undefined)
147147
.mockResolvedValueOnce(undefined)
@@ -157,8 +157,8 @@ describe('HostThemeManager', () => {
157157
await themeManager.findOrCreate()
158158

159159
// Then
160-
expect(createTheme).toHaveBeenCalledTimes(4)
161-
expect(createTheme).toHaveBeenLastCalledWith(
160+
expect(themeCreate).toHaveBeenCalledTimes(4)
161+
expect(themeCreate).toHaveBeenLastCalledWith(
162162
{
163163
role: DEVELOPMENT_THEME_ROLE,
164164
name: 'App Ext. Host Name',
@@ -170,14 +170,14 @@ describe('HostThemeManager', () => {
170170

171171
test('should throw a BugError if the theme cannot be created', async () => {
172172
// Given
173-
vi.mocked(createTheme).mockResolvedValue(undefined)
173+
vi.mocked(themeCreate).mockResolvedValue(undefined)
174174

175175
// When
176176
// Then
177177
await expect(themeManager.findOrCreate()).rejects.toThrow(
178178
'Could not create theme with name "App Ext. Host Name" and role "development"',
179179
)
180-
expect(createTheme).toHaveBeenCalledTimes(4)
180+
expect(themeCreate).toHaveBeenCalledTimes(4)
181181
})
182182
})
183183
})

packages/app/src/cli/utilities/extensions/theme/host-theme-manager.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {getHostTheme, removeHostTheme, setHostTheme} from '@shopify/cli-kit/node
33
import {ThemeManager} from '@shopify/cli-kit/node/themes/theme-manager'
44
import {AdminSession} from '@shopify/cli-kit/node/session'
55
import {Theme} from '@shopify/cli-kit/node/themes/types'
6-
import {createTheme} from '@shopify/cli-kit/node/themes/api'
6+
import {themeCreate} from '@shopify/cli-kit/node/themes/api'
77
import {DEVELOPMENT_THEME_ROLE} from '@shopify/cli-kit/node/themes/utils'
88
import {BugError} from '@shopify/cli-kit/node/error'
99
import {outputDebug} from '@shopify/cli-kit/node/output'
@@ -52,7 +52,7 @@ export class HostThemeManager extends ThemeManager {
5252

5353
try {
5454
// eslint-disable-next-line no-await-in-loop
55-
const theme = await createTheme(options, this.adminSession)
55+
const theme = await themeCreate(options, this.adminSession)
5656
if (theme) {
5757
this.setTheme(theme.id.toString())
5858
outputDebug(`Waiting for theme with id "${theme.id}" to be processed`)
@@ -69,7 +69,7 @@ export class HostThemeManager extends ThemeManager {
6969
}
7070

7171
outputDebug(`Theme creation failed after ${retryAttemps} retries. Creating theme using fallback theme zip`)
72-
const theme = await createTheme({...options, src: FALLBACK_THEME_ZIP}, this.adminSession)
72+
const theme = await themeCreate({...options, src: FALLBACK_THEME_ZIP}, this.adminSession)
7373
if (!theme) {
7474
outputDebug(`Theme creation failed. Exiting process.`)
7575
throw new BugError(`Could not create theme with name "${options.name}" and role "${options.role}"`)
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* eslint-disable @typescript-eslint/consistent-type-definitions */
2+
import * as Types from './types.js'
3+
4+
import {TypedDocumentNode as DocumentNode} from '@graphql-typed-document-node/core'
5+
6+
export type ThemeCreateMutationVariables = Types.Exact<{
7+
name: Types.Scalars['String']['input']
8+
source: Types.Scalars['URL']['input']
9+
role: Types.ThemeRole
10+
}>
11+
12+
export type ThemeCreateMutation = {
13+
themeCreate?: {
14+
theme?: {id: string; name: string; role: Types.ThemeRole} | null
15+
userErrors: {field?: string[] | null; message: string}[]
16+
} | null
17+
}
18+
19+
export const ThemeCreate = {
20+
kind: 'Document',
21+
definitions: [
22+
{
23+
kind: 'OperationDefinition',
24+
operation: 'mutation',
25+
name: {kind: 'Name', value: 'themeCreate'},
26+
variableDefinitions: [
27+
{
28+
kind: 'VariableDefinition',
29+
variable: {kind: 'Variable', name: {kind: 'Name', value: 'name'}},
30+
type: {kind: 'NonNullType', type: {kind: 'NamedType', name: {kind: 'Name', value: 'String'}}},
31+
},
32+
{
33+
kind: 'VariableDefinition',
34+
variable: {kind: 'Variable', name: {kind: 'Name', value: 'source'}},
35+
type: {kind: 'NonNullType', type: {kind: 'NamedType', name: {kind: 'Name', value: 'URL'}}},
36+
},
37+
{
38+
kind: 'VariableDefinition',
39+
variable: {kind: 'Variable', name: {kind: 'Name', value: 'role'}},
40+
type: {kind: 'NonNullType', type: {kind: 'NamedType', name: {kind: 'Name', value: 'ThemeRole'}}},
41+
},
42+
],
43+
selectionSet: {
44+
kind: 'SelectionSet',
45+
selections: [
46+
{
47+
kind: 'Field',
48+
name: {kind: 'Name', value: 'themeCreate'},
49+
arguments: [
50+
{
51+
kind: 'Argument',
52+
name: {kind: 'Name', value: 'name'},
53+
value: {kind: 'Variable', name: {kind: 'Name', value: 'name'}},
54+
},
55+
{
56+
kind: 'Argument',
57+
name: {kind: 'Name', value: 'source'},
58+
value: {kind: 'Variable', name: {kind: 'Name', value: 'source'}},
59+
},
60+
{
61+
kind: 'Argument',
62+
name: {kind: 'Name', value: 'role'},
63+
value: {kind: 'Variable', name: {kind: 'Name', value: 'role'}},
64+
},
65+
],
66+
selectionSet: {
67+
kind: 'SelectionSet',
68+
selections: [
69+
{
70+
kind: 'Field',
71+
name: {kind: 'Name', value: 'theme'},
72+
selectionSet: {
73+
kind: 'SelectionSet',
74+
selections: [
75+
{kind: 'Field', name: {kind: 'Name', value: 'id'}},
76+
{kind: 'Field', name: {kind: 'Name', value: 'name'}},
77+
{kind: 'Field', name: {kind: 'Name', value: 'role'}},
78+
{kind: 'Field', name: {kind: 'Name', value: '__typename'}},
79+
],
80+
},
81+
},
82+
{
83+
kind: 'Field',
84+
name: {kind: 'Name', value: 'userErrors'},
85+
selectionSet: {
86+
kind: 'SelectionSet',
87+
selections: [
88+
{kind: 'Field', name: {kind: 'Name', value: 'field'}},
89+
{kind: 'Field', name: {kind: 'Name', value: 'message'}},
90+
{kind: 'Field', name: {kind: 'Name', value: '__typename'}},
91+
],
92+
},
93+
},
94+
{kind: 'Field', name: {kind: 'Name', value: '__typename'}},
95+
],
96+
},
97+
},
98+
],
99+
},
100+
},
101+
],
102+
} as unknown as DocumentNode<ThemeCreateMutation, ThemeCreateMutationVariables>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mutation themeCreate($name: String!, $source: URL!, $role: ThemeRole!) {
2+
themeCreate(name: $name, source: $source, role: $role) {
3+
theme {
4+
id
5+
name
6+
role
7+
}
8+
userErrors {
9+
field
10+
message
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)