/
gql-Mutation.ts
754 lines (635 loc) · 21.3 KB
/
gql-Mutation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
import { arg, booleanArg, enumType, idArg, mutationType, nonNull, stringArg, list } from 'nexus'
import { Wizard } from './gql-Wizard'
import { CodeGenTypeEnum } from '../enumTypes/gql-CodeGenTypeEnum'
import { TestingTypeEnum } from '../enumTypes/gql-WizardEnums'
import { PreferencesTypeEnum } from '../enumTypes/gql-PreferencesTypeEnum'
import { FileDetailsInput } from '../inputTypes/gql-FileDetailsInput'
import { WizardUpdateInput } from '../inputTypes/gql-WizardUpdateInput'
import { CurrentProject } from './gql-CurrentProject'
import { GenerateSpecResponse } from './gql-GenerateSpecResponse'
import { Cohort, CohortInput } from './gql-Cohorts'
import { Query } from './gql-Query'
import { ScaffoldedFile } from './gql-ScaffoldedFile'
import { WIZARD_BUNDLERS, WIZARD_FRAMEWORKS } from '@packages/scaffold-config'
import debugLib from 'debug'
const debug = debugLib('cypress:graphql:mutation')
export const mutation = mutationType({
definition (t) {
t.field('copyTextToClipboard', {
type: 'Boolean',
description: 'add the passed text to the local clipboard',
args: {
text: nonNull(stringArg()),
},
resolve: (_, { text }, ctx) => {
ctx.electronApi.copyTextToClipboard(text)
return true
},
})
t.field('resetErrorAndLoadConfig', {
type: Query,
description: 'Resets error and attempts to reload the config',
args: {
id: nonNull(idArg()),
},
resolve: async (_, args, ctx) => {
ctx.actions.error.clearError(args.id)
await ctx.lifecycleManager.refreshLifecycle().catch((e) => ctx.lifecycleManager.onLoadError(e))
return {}
},
})
t.field('devRelaunch', {
type: 'Boolean',
description: 'Development only: Triggers or dismisses a prompted refresh by touching the file watched by our development scripts',
args: {
action: nonNull(enumType({
name: 'DevRelaunchAction',
members: ['trigger', 'dismiss'],
}).asArg()),
},
resolve: async (_, args, ctx) => {
if (args.action === 'trigger') {
ctx.actions.dev.triggerRelaunch()
} else {
ctx.actions.dev.dismissRelaunch()
}
return true
},
})
t.nonNull.boolean('matchesSpecPattern', {
description: 'Check if a give spec file will match the project spec pattern',
args: {
specFile: nonNull(stringArg()),
},
resolve: (source, args, ctx) => {
if (!ctx.currentProject) {
return false
}
return ctx.project.matchesSpecPattern(args.specFile)
},
})
t.field('internal_clearLatestProjectCache', {
type: 'Boolean',
resolve: async (_, args, ctx) => {
await ctx.actions.project.clearLatestProjectCache()
return true
},
})
t.field('openExternal', {
type: 'Boolean',
args: {
url: nonNull(stringArg()),
includeGraphqlPort: booleanArg(),
},
resolve: (_, args, ctx) => {
let url = args.url
// the `port` param is included in external links to create a cloud organization
// so that the app can be notified when the org has been created
if (args.includeGraphqlPort && process.env.CYPRESS_INTERNAL_GRAPHQL_PORT) {
const joinCharacter = args.url.includes('?') ? '&' : '?'
url = `${args.url}${joinCharacter}port=${process.env.CYPRESS_INTERNAL_GRAPHQL_PORT}`
}
ctx.actions.electron.openExternal(url)
return true
},
})
t.field('internal_clearProjectPreferencesCache', {
type: 'Boolean',
args: {
projectTitle: nonNull(stringArg()),
},
resolve: async (_, args, ctx) => {
await ctx.actions.project.clearProjectPreferencesCache(args.projectTitle)
return true
},
})
t.field('internal_clearAllProjectPreferencesCache', {
type: 'Boolean',
resolve: async (_, args, ctx) => {
await ctx.actions.project.clearAllProjectPreferencesCache()
return true
},
})
t.field('scaffoldTestingType', {
type: 'Query',
resolve: async (_, args, ctx) => {
await ctx.actions.wizard.scaffoldTestingType()
return {}
},
})
t.field('completeSetup', {
type: 'Query',
resolve: async (_, args, ctx) => {
await ctx.actions.wizard.completeSetup()
return {}
},
})
t.field('clearCurrentProject', {
type: 'Query',
description: 'Clears the currently active project',
resolve: async (_, args, ctx) => {
await ctx.actions.project.clearCurrentProject()
ctx.actions.wizard.resetWizard()
return {}
},
})
t.field('clearCurrentTestingType', {
type: 'Query',
resolve: (_, args, ctx) => {
ctx.lifecycleManager.setAndLoadCurrentTestingType(null)
return {}
},
})
t.field('setAndLoadCurrentTestingType', {
type: 'Query',
args: {
testingType: nonNull(arg({ type: TestingTypeEnum })),
},
resolve: async (source, args, ctx) => {
ctx.actions.project.setAndLoadCurrentTestingType(args.testingType)
// if necessary init the wizard for configuration
if (ctx.coreData.currentTestingType && !ctx.lifecycleManager.isTestingTypeConfigured(ctx.coreData.currentTestingType)) {
// Component Testing has a wizard to help users configure their project
if (ctx.coreData.currentTestingType === 'component') {
await ctx.actions.wizard.initialize()
} else {
// E2E doesn't have such a wizard, we just create/update their cypress.config.js.
await ctx.actions.wizard.scaffoldTestingType()
}
}
return {}
},
})
// TODO: remove server-side setPromptShown helpers in #23768,
// since this will be handled by usePromptManager via existing
// `setPreferences` mutation, there is no need for this other
//way to modify saved sate
t.field('setPromptShown', {
type: 'Boolean',
description: 'Save the prompt-shown state for this project',
args: { slug: nonNull('String') },
resolve: (_, args, ctx) => {
ctx.actions.project.setPromptShown(args.slug)
return true
},
})
t.field('wizardUpdate', {
type: Wizard,
description: 'Updates the different fields of the wizard data store',
args: {
input: nonNull(arg({ type: WizardUpdateInput })),
},
resolve: async (source, args, ctx) => {
if (args.input.framework) {
ctx.actions.wizard.setFramework(WIZARD_FRAMEWORKS.find((x) => x.type === args.input.framework) ?? null)
}
if (args.input.bundler) {
ctx.actions.wizard.setBundler(WIZARD_BUNDLERS.find((x) => x.type === args.input.bundler) ?? null)
}
// TODO: remove when live-mutations are implements
// signal to launchpad to reload the data context
ctx.emitter.toLaunchpad()
return ctx.wizardData
},
})
t.field('launchpadSetBrowser', {
type: CurrentProject,
description: 'Sets the active browser',
args: {
id: nonNull(idArg({
description: 'ID of the browser that we want to set',
})),
},
async resolve (_, args, ctx) {
await ctx.actions.browser.setActiveBrowserById(args.id)
return ctx.lifecycleManager
},
})
t.field('generateSpecFromSource', {
type: GenerateSpecResponse,
description: 'Generate spec from source',
args: {
codeGenCandidate: nonNull(stringArg()),
type: nonNull(CodeGenTypeEnum),
},
resolve: (_, args, ctx) => {
return ctx.actions.project.codeGenSpec(args.codeGenCandidate, args.type)
},
})
t.nonNull.list.nonNull.field('scaffoldIntegration', {
type: ScaffoldedFile,
resolve: (src, args, ctx) => {
return ctx.actions.project.scaffoldIntegration()
},
})
t.field('login', {
type: Query,
description: 'Auth with Cypress Cloud',
args: {
utmMedium: nonNull(stringArg()),
utmContent: stringArg(),
utmSource: nonNull(stringArg()),
},
resolve: async (_, args, ctx) => {
await ctx.actions.auth.login(args.utmSource, args.utmMedium, args.utmContent)
return {}
},
})
t.field('logout', {
type: Query,
description: 'Log out of Cypress Cloud',
resolve: async (_, args, ctx) => {
await ctx.actions.auth.logout()
return {}
},
})
t.field('launchOpenProject', {
type: CurrentProject,
description: 'Launches project from open_project global singleton',
args: {
shouldLaunchNewTab: booleanArg(),
specPath: stringArg(),
},
resolve: async (_, args, ctx) => {
await ctx.actions.project.launchProject(ctx.coreData.currentTestingType, { shouldLaunchNewTab: args.shouldLaunchNewTab ?? false }, args.specPath)
return ctx.lifecycleManager
},
})
t.field('addProject', {
type: Query,
description: 'Add project to projects array and cache it',
args: {
path: stringArg(),
open: booleanArg({ description: 'Whether to open the project when added' }),
},
resolve: async (_, args, ctx) => {
ctx.actions.wizard.resetWizard()
let path = args.path
if (!path) {
await ctx.actions.project.addProjectFromElectronNativeFolderSelect()
return {}
}
await ctx.actions.project.addProject({
...args,
path,
})
return {}
},
})
t.field('removeProject', {
type: Query,
description: 'Remove project from projects array and cache',
args: {
path: nonNull(stringArg()),
},
resolve: async (_, args, ctx) => {
await ctx.actions.project.removeProject(args.path)
return {}
},
})
t.field('setCurrentProject', {
type: Query,
description: 'Set active project to run tests on',
args: {
path: nonNull(stringArg()),
},
resolve: async (_, args, ctx) => {
await ctx.actions.project.setCurrentProject(args.path)
return {}
},
})
// TODO: #23202 hopefully we can stop using this for project data, and use `setPreferences` instead
t.nonNull.field('setProjectPreferencesInGlobalCache', {
type: Query,
description: 'Save the projects preferences to cache, e.g. in dev: Library/Application Support/Cypress/cy/staging/cache',
args: {
testingType: nonNull(TestingTypeEnum),
},
async resolve (_, args, ctx) {
await ctx.actions.project.setProjectPreferencesInGlobalCache(args)
return {}
},
})
t.nonNull.field('resetAuthState', {
type: Query,
description: 'Reset the Auth State',
resolve (_, args, ctx) {
ctx.actions.auth.resetAuthState()
return {}
},
})
t.nonNull.field('resetWizard', {
type: 'Boolean',
description: 'Reset the Wizard to the starting position',
resolve: (_, args, ctx) => {
ctx.actions.wizard.resetWizard()
ctx.actions.electron.refreshBrowserWindow()
return true
},
})
t.nonNull.field('resetLatestVersionTelemetry', {
type: 'Boolean',
description: 'Resets the latest version call to capture additional telemetry for the current user',
resolve: async (_, args, ctx) => {
ctx.actions.versions.resetLatestVersionTelemetry()
return true
},
})
t.nonNull.field('focusActiveBrowserWindow', {
type: 'Boolean',
description: 'Sets focus to the active browser window',
resolve: async (_, args, ctx) => {
await ctx.actions.browser.focusActiveBrowserWindow()
return true
},
})
t.nonNull.field('reconfigureProject', {
type: 'Boolean',
description: 'show the launchpad windows',
resolve: async (_, args, ctx) => {
ctx.actions.project.setForceReconfigureProjectByTestingType({ forceReconfigureProject: true })
await ctx.actions.project.reconfigureProject()
return true
},
})
t.field('setPreferences', {
type: Query,
description: [
'Update local preferences (also known as appData).',
'The payload, `value`, should be a `JSON.stringified()`',
'object of the new values you\'d like to persist.',
'Example: `setPreferences (value: JSON.stringify({ lastOpened: Date.now() }), "local")`',
].join(' '),
args: {
value: nonNull(stringArg()),
type: nonNull(arg({
type: PreferencesTypeEnum,
})),
},
resolve: async (_, { value, type }, ctx) => {
await ctx.actions.localSettings.setPreferences(value, type)
return {}
},
})
t.field('openDirectoryInIDE', {
description: 'Open a path in preferred IDE',
type: 'Boolean',
args: {
path: nonNull(stringArg()),
},
resolve: (_, args, ctx) => {
ctx.actions.project.openDirectoryInIDE(args.path)
return true
},
})
t.field('openInFinder', {
description: 'Open a path in the local file explorer',
type: 'Boolean',
args: {
path: nonNull(stringArg()),
},
resolve: (_, args, ctx) => {
ctx.actions.electron.showItemInFolder(args.path)
return true
},
})
t.field('openFileInIDE', {
description: 'Open a file on specified line and column in preferred IDE',
type: 'Boolean',
args: {
input: nonNull(arg({
type: FileDetailsInput,
})),
},
resolve: (_, args, ctx) => {
ctx.actions.file.openFile(
args.input.filePath,
args.input.line || 1,
args.input.column || 1,
)
return true
},
})
t.field('migrateRenameSpecs', {
description: 'While migrating to 10+ renames files to match the new .cy pattern',
type: Query,
args: {
skip: booleanArg(),
before: list(nonNull(stringArg({
description: 'specs to move - current name',
}))),
after: list(nonNull(stringArg({
description: 'specs to move - current name',
}))),
},
resolve: async (_, { skip, before, after }, ctx) => {
if (!skip && before && after) {
await ctx.actions.migration.renameSpecFiles(before, after)
}
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('migrateRenameSpecsFolder', {
description: 'When the user decides to skip specs rename',
type: Query,
resolve: async (_, args, ctx) => {
await ctx.actions.migration.renameSpecsFolder()
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('migrateSkipManualRename', {
description: 'While migrating to 10+ skip manual rename step',
type: Query,
resolve: async (_, args, ctx) => {
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('migrateCloseManualRenameWatcher', {
description: 'While migrating to 10+ skip manual rename step',
type: 'Boolean',
resolve: async (_, args, ctx) => {
await ctx.actions.migration.closeManualRenameWatcher()
return true
},
})
t.field('finishedRenamingComponentSpecs', {
description: 'user has finished migration component specs - move to next step',
type: Query,
resolve: async (_, args, ctx) => {
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('migrateRenameSupport', {
description: 'While migrating to 10+ launch renaming of support file',
type: Query,
resolve: async (_, args, ctx) => {
await ctx.actions.migration.renameSupportFile()
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('migrateConfigFile', {
description: 'Transforms cypress.json file into cypress.config.js file',
type: Query,
resolve: async (_, args, ctx) => {
await ctx.actions.migration.createConfigFile()
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('migrateComponentTesting', {
description: 'Merges the component testing config in cypress.config.{js,ts}',
type: Query,
resolve: async (_, args, ctx) => {
await ctx.actions.migration.nextStep()
return {}
},
})
t.field('setProjectIdInConfigFile', {
description: 'Set the projectId field in the config file of the current project',
type: Query,
args: {
projectId: nonNull(stringArg()),
},
resolve: async (_, args, ctx) => {
try {
await ctx.actions.project.setProjectIdInConfigFile(args.projectId)
} catch {
// We were unable to set the project id, the error isn't useful
// to show the user here, because they're prompted to update the id manually
return null
}
// Wait for the project config to be reloaded
await ctx.lifecycleManager.refreshLifecycle()
return {}
},
})
t.field('closeBrowser', {
description: 'Close active browser',
type: 'Boolean',
resolve: async (source, args, ctx) => {
await ctx.actions.browser.closeBrowser()
return true
},
})
t.field('switchTestingTypeAndRelaunch', {
description: 'Switch Testing type and relaunch browser',
type: 'Boolean',
args: {
testingType: nonNull(arg({ type: TestingTypeEnum })),
},
resolve: async (source, args, ctx) => {
await ctx.actions.project.switchTestingTypesAndRelaunch(args.testingType)
return true
},
})
t.field('dismissWarning', {
type: Query,
args: {
id: nonNull(idArg({})),
},
description: `Dismisses a warning displayed by the frontend`,
resolve: (source, args, ctx) => {
ctx.actions.error.clearWarning(args.id)
return {}
},
})
t.field('pingBaseUrl', {
type: Query,
description: 'Ping configured Base URL',
resolve: async (source, args, ctx) => {
await ctx.actions.project.pingBaseUrl()
return {}
},
})
t.field('refreshCloudViewer', {
type: Query,
description: 'Clears the cloudViewer cache to refresh the organizations and projects',
resolve: async (source, args, ctx) => {
await ctx.cloud.invalidate('Query', 'cloudViewer')
return {}
},
})
t.field('purgeCloudSpecByPathCache', {
type: 'Boolean',
args: {
projectSlug: nonNull(stringArg({})),
specPaths: nonNull(list(nonNull(stringArg({})))),
},
description: 'Removes the cache entries for specified cloudSpecByPath query records',
resolve: async (source, args, ctx) => {
const { projectSlug, specPaths } = args
debug('Purging %d `cloudSpecByPath` cache records for project %s: %o', specPaths.length, projectSlug, specPaths)
for (let specPath of specPaths) {
await ctx.cloud.invalidate('Query', 'cloudSpecByPath', { projectSlug, specPath })
}
return true
},
})
t.field('refetchRemote', {
type: Query,
description: 'Signal that we are explicitly refetching remote data and should not use the server cache',
resolve: () => {
return {
requestPolicy: 'network-only',
} as const
},
})
t.field('determineCohort', {
type: Cohort,
description: 'Determine the cohort based on the given configuration. This will either return the cached cohort for a given name or choose a new one and store it.',
args: {
cohortConfig: nonNull(CohortInput),
},
resolve: async (source, args, ctx) => {
return ctx.actions.cohorts.determineCohort(args.cohortConfig.name, args.cohortConfig.cohorts, args.cohortConfig.weights || undefined)
},
})
t.field('recordEvent', {
type: 'Boolean',
description: 'Dispatch an event to Cypress Cloud to be recorded. Events are completely anonymous and are only used to identify aggregate usage patterns across all Cypress users.',
args: {
campaign: nonNull(stringArg({})),
messageId: nonNull(stringArg({})),
medium: nonNull(stringArg({})),
cohort: stringArg({}),
},
resolve: (source, args, ctx) => {
return ctx.actions.eventCollector.recordEvent({
campaign: args.campaign,
messageId: args.messageId,
medium: args.medium,
cohort: args.cohort || undefined,
})
},
})
t.boolean('_clearCloudCache', {
description: 'Internal use only, clears the cloud cache',
resolve: (source, args, ctx) => {
ctx.cloud.reset()
return true
},
})
t.json('_showUrqlCache', {
description: 'Internal use only, clears the cloud cache',
resolve: async (source, args, ctx) => {
const { data } = await ctx.cloud.getCache()
return data
},
})
t.boolean('setRunAllSpecs', {
description: 'List of specs to run for the "Run All Specs" Feature',
args: {
runAllSpecs: nonNull(list(nonNull(stringArg()))),
},
resolve: (source, args, ctx) => {
ctx.project.setRunAllSpecs(args.runAllSpecs)
return true
},
})
},
})