Skip to content

Commit 6e116a7

Browse files
fix(graphql): threads through correct draft value for upload relations (#6235)
1 parent f52607f commit 6e116a7

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

packages/graphql/src/schema/buildObjectType.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ function buildObjectType({
361361
const locale = args.locale || context.req.locale
362362
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale
363363
let relatedCollectionSlug = field.relationTo
364-
const draft = args.draft ?? context.req.query?.draft
364+
const draft = Boolean(args.draft ?? context.req.query?.draft)
365365

366366
if (hasManyValues) {
367367
const results = []
@@ -630,6 +630,7 @@ function buildObjectType({
630630
const locale = args.locale || context.req.locale
631631
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale
632632
const id = value
633+
const draft = Boolean(args.draft ?? context.req.query?.draft)
633634

634635
if (id) {
635636
const relatedDocument = await context.req.payloadDataLoader.load(
@@ -638,7 +639,7 @@ function buildObjectType({
638639
currentDepth: 0,
639640
depth: 0,
640641
docID: id,
641-
draft: false,
642+
draft,
642643
fallbackLocale,
643644
locale,
644645
overrideAccess: false,

test/collections-graphql/config.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,27 @@ export default buildConfigWithDefaults({
351351
type: 'relationship',
352352
relationTo: 'cyclical-relationship',
353353
},
354+
{
355+
type: 'upload',
356+
name: 'media',
357+
relationTo: 'media',
358+
},
354359
],
355360
versions: {
356361
drafts: true,
357362
},
358363
},
364+
{
365+
slug: 'media',
366+
access: openAccess,
367+
upload: true,
368+
fields: [
369+
{
370+
name: 'title',
371+
type: 'text',
372+
},
373+
],
374+
},
359375
],
360376
graphQL: {
361377
queries: (GraphQL) => {

test/collections-graphql/int.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { Payload } from 'payload'
22

3+
import path from 'path'
4+
import { getFileByPath } from 'payload/uploads'
35
import { mapAsync } from 'payload/utilities'
46

57
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
@@ -1074,6 +1076,43 @@ describe('collections-graphql', () => {
10741076
expect(queriedDoc2.relationToSelf.title).toEqual(draftValue)
10751077
})
10761078

1079+
it('should query upload enabled docs', async () => {
1080+
const file = await getFileByPath(path.resolve(__dirname, '../uploads/test-image.jpg'))
1081+
1082+
const mediaDoc = await payload.create({
1083+
collection: 'media',
1084+
file,
1085+
data: {
1086+
title: 'example',
1087+
},
1088+
})
1089+
1090+
// doc with upload relation
1091+
const newDoc = await payload.create({
1092+
collection: 'cyclical-relationship',
1093+
data: {
1094+
media: mediaDoc.id,
1095+
},
1096+
})
1097+
1098+
const query = `{
1099+
CyclicalRelationship(id: ${typeof newDoc.id === 'number' ? newDoc.id : `"${newDoc.id}"`}) {
1100+
media {
1101+
id
1102+
title
1103+
}
1104+
}
1105+
}`
1106+
const res = await restClient
1107+
.GRAPHQL_POST({
1108+
body: JSON.stringify({ query }),
1109+
})
1110+
.then((res) => res.json())
1111+
const queriedDoc = res.data.CyclicalRelationship
1112+
expect(queriedDoc.media.id).toEqual(mediaDoc.id)
1113+
expect(queriedDoc.media.title).toEqual('example')
1114+
})
1115+
10771116
describe('Error Handler', () => {
10781117
it('should return have an array of errors when making a bad request', async () => {
10791118
const query = `query {

0 commit comments

Comments
 (0)