Skip to content

Commit 67a7358

Browse files
authored
fix(plugin-import-export): export with draft true (#11762)
### What? - GraphQL was broken because of an error with the enum for the drafts input which cannot be 'true'. - Selecting Draft was not doing anything as it wasn't being passed through to the find arguments. ### Why? This was causing any graphql calls to error. ### How? - Changed draft options to Yes/No instead of True/False - Correctly pass the drafts arg to `draft` Fixes #
1 parent e83f452 commit 67a7358

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

packages/plugin-import-export/src/export/createExport.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { getSelect } from './getSelect.js'
1010

1111
type Export = {
1212
collectionSlug: string
13+
drafts?: 'no' | 'yes'
1314
exportsCollection: string
1415
fields?: string[]
1516
format: 'csv' | 'json'
@@ -41,6 +42,7 @@ export const createExport = async (args: CreateExportArgs) => {
4142
id,
4243
name: nameArg,
4344
collectionSlug,
45+
drafts,
4446
exportsCollection,
4547
fields,
4648
format,
@@ -64,11 +66,12 @@ export const createExport = async (args: CreateExportArgs) => {
6466
const findArgs = {
6567
collection: collectionSlug,
6668
depth: 0,
69+
draft: drafts === 'yes',
6770
limit: 100,
6871
locale,
6972
overrideAccess: false,
7073
page: 0,
71-
select: fields ? getSelect(fields) : undefined,
74+
select: Array.isArray(fields) && fields.length > 0 ? getSelect(fields) : undefined,
7275
sort,
7376
user,
7477
where,

packages/plugin-import-export/src/export/getFields.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ export const getFields = (config: Config): Field[] => {
9797
},
9898
width: '33%',
9999
},
100-
defaultValue: 'true',
100+
defaultValue: 'yes',
101101
label: 'Drafts',
102102
options: [
103103
{
104-
label: 'True',
105-
value: 'true',
104+
label: 'Yes',
105+
value: 'yes',
106106
},
107107
{
108-
label: 'False',
109-
value: 'false',
108+
label: 'No',
109+
value: 'no',
110110
},
111111
],
112112
},

test/_community/payload-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export type SupportedTimezones =
5454
| 'Asia/Singapore'
5555
| 'Asia/Tokyo'
5656
| 'Asia/Seoul'
57+
| 'Australia/Brisbane'
5758
| 'Australia/Sydney'
5859
| 'Pacific/Guam'
5960
| 'Pacific/Noumea'

test/plugin-import-export/int.spec.ts

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ import type { CollectionSlug, Payload } from 'payload'
33
import path from 'path'
44
import { fileURLToPath } from 'url'
55

6+
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
7+
68
import { devUser } from '../credentials.js'
79
import { initPayloadInt } from '../helpers/initPayloadInt.js'
810
import { readCSV, readJSON } from './helpers.js'
911
import { richTextData } from './seed/richTextData.js'
1012

1113
let payload: Payload
14+
let restClient: NextRESTClient
1215
let user: any
1316

1417
const filename = fileURLToPath(import.meta.url)
1518
const dirname = path.dirname(filename)
1619

1720
describe('@payloadcms/plugin-import-export', () => {
1821
beforeAll(async () => {
19-
;({ payload } = (await initPayloadInt(dirname)) as { payload: Payload })
22+
;({ payload, restClient } = await initPayloadInt(dirname))
2023
user = await payload.login({
2124
collection: 'users',
2225
data: {
@@ -32,6 +35,25 @@ describe('@payloadcms/plugin-import-export', () => {
3235
}
3336
})
3437

38+
describe('graphql', () => {
39+
it('should not break graphql', async () => {
40+
const query = `query {
41+
__schema {
42+
queryType {
43+
name
44+
}
45+
}
46+
}`
47+
const response = await restClient
48+
.GRAPHQL_POST({
49+
body: JSON.stringify({ query }),
50+
})
51+
.then((res) => res.json())
52+
53+
expect(response.error).toBeUndefined()
54+
})
55+
})
56+
3557
describe('exports', () => {
3658
it('should create a file for collection csv from defined fields', async () => {
3759
let doc = await payload.create({
@@ -66,6 +88,53 @@ describe('@payloadcms/plugin-import-export', () => {
6688
expect(data[0].updatedAt).toBeDefined()
6789
})
6890

91+
it('should create a file for collection csv with draft data', async () => {
92+
const draftPage = await payload.create({
93+
collection: 'pages',
94+
user,
95+
data: {
96+
title: 'Draft Page',
97+
_status: 'published',
98+
},
99+
})
100+
101+
await payload.update({
102+
collection: 'pages',
103+
id: draftPage.id,
104+
data: {
105+
title: 'Draft Page Updated',
106+
_status: 'draft',
107+
},
108+
})
109+
110+
let doc = await payload.create({
111+
collection: 'exports',
112+
user,
113+
data: {
114+
collectionSlug: 'pages',
115+
fields: ['id', 'title', '_status'],
116+
locale: 'en',
117+
format: 'csv',
118+
where: {
119+
title: { contains: 'Draft ' },
120+
},
121+
},
122+
})
123+
124+
doc = await payload.findByID({
125+
collection: 'exports',
126+
id: doc.id,
127+
})
128+
129+
expect(doc.filename).toBeDefined()
130+
const expectedPath = path.join(dirname, './uploads', doc.filename as string)
131+
const data = await readCSV(expectedPath)
132+
133+
expect(data[0].id).toBeDefined()
134+
expect(data[0].title).toStrictEqual('Draft Page Updated')
135+
expect(data[0]._status).toStrictEqual('draft')
136+
})
137+
69138
it('should create a file for collection csv from one locale', async () => {
70139
let doc = await payload.create({
71140
collection: 'exports',

0 commit comments

Comments
 (0)