From 6e116a76fde4dd6c24834d1429aeeccd13e0ec1a Mon Sep 17 00:00:00 2001 From: Jarrod Flesch <30633324+JarrodMFlesch@users.noreply.github.com> Date: Tue, 14 May 2024 14:05:58 -0400 Subject: [PATCH] fix(graphql): threads through correct draft value for upload relations (#6235) --- .../graphql/src/schema/buildObjectType.ts | 5 ++- test/collections-graphql/config.ts | 16 ++++++++ test/collections-graphql/int.spec.ts | 39 +++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/packages/graphql/src/schema/buildObjectType.ts b/packages/graphql/src/schema/buildObjectType.ts index 68441c775..bab168076 100644 --- a/packages/graphql/src/schema/buildObjectType.ts +++ b/packages/graphql/src/schema/buildObjectType.ts @@ -361,7 +361,7 @@ function buildObjectType({ const locale = args.locale || context.req.locale const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale let relatedCollectionSlug = field.relationTo - const draft = args.draft ?? context.req.query?.draft + const draft = Boolean(args.draft ?? context.req.query?.draft) if (hasManyValues) { const results = [] @@ -630,6 +630,7 @@ function buildObjectType({ const locale = args.locale || context.req.locale const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale const id = value + const draft = Boolean(args.draft ?? context.req.query?.draft) if (id) { const relatedDocument = await context.req.payloadDataLoader.load( @@ -638,7 +639,7 @@ function buildObjectType({ currentDepth: 0, depth: 0, docID: id, - draft: false, + draft, fallbackLocale, locale, overrideAccess: false, diff --git a/test/collections-graphql/config.ts b/test/collections-graphql/config.ts index 8f343b00e..7e791593b 100644 --- a/test/collections-graphql/config.ts +++ b/test/collections-graphql/config.ts @@ -351,11 +351,27 @@ export default buildConfigWithDefaults({ type: 'relationship', relationTo: 'cyclical-relationship', }, + { + type: 'upload', + name: 'media', + relationTo: 'media', + }, ], versions: { drafts: true, }, }, + { + slug: 'media', + access: openAccess, + upload: true, + fields: [ + { + name: 'title', + type: 'text', + }, + ], + }, ], graphQL: { queries: (GraphQL) => { diff --git a/test/collections-graphql/int.spec.ts b/test/collections-graphql/int.spec.ts index 7513ef5ae..e0eeedaca 100644 --- a/test/collections-graphql/int.spec.ts +++ b/test/collections-graphql/int.spec.ts @@ -1,5 +1,7 @@ import type { Payload } from 'payload' +import path from 'path' +import { getFileByPath } from 'payload/uploads' import { mapAsync } from 'payload/utilities' import type { NextRESTClient } from '../helpers/NextRESTClient.js' @@ -1074,6 +1076,43 @@ describe('collections-graphql', () => { expect(queriedDoc2.relationToSelf.title).toEqual(draftValue) }) + it('should query upload enabled docs', async () => { + const file = await getFileByPath(path.resolve(__dirname, '../uploads/test-image.jpg')) + + const mediaDoc = await payload.create({ + collection: 'media', + file, + data: { + title: 'example', + }, + }) + + // doc with upload relation + const newDoc = await payload.create({ + collection: 'cyclical-relationship', + data: { + media: mediaDoc.id, + }, + }) + + const query = `{ + CyclicalRelationship(id: ${typeof newDoc.id === 'number' ? newDoc.id : `"${newDoc.id}"`}) { + media { + id + title + } + } + }` + const res = await restClient + .GRAPHQL_POST({ + body: JSON.stringify({ query }), + }) + .then((res) => res.json()) + const queriedDoc = res.data.CyclicalRelationship + expect(queriedDoc.media.id).toEqual(mediaDoc.id) + expect(queriedDoc.media.title).toEqual('example') + }) + describe('Error Handler', () => { it('should return have an array of errors when making a bad request', async () => { const query = `query {