Skip to content

Commit

Permalink
fix: graphql upload relations returning null (#6233)
Browse files Browse the repository at this point in the history
  • Loading branch information
JarrodMFlesch committed May 6, 2024
1 parent 3cb3c1a commit cac52da
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/payload/src/graphql/schema/buildObjectType.ts
Expand Up @@ -609,6 +609,7 @@ function buildObjectType({
const locale = args.locale || context.req.locale
const fallbackLocale = args.fallbackLocale || context.req.fallbackLocale
const id = value
const draft = args.draft ?? context.req.query?.draft

if (id) {
const relatedDocument = await context.req.payloadDataLoader.load(
Expand All @@ -622,6 +623,7 @@ function buildObjectType({
fallbackLocale,
false,
false,
Boolean(draft),
]),
)

Expand Down
16 changes: 16 additions & 0 deletions test/collections-graphql/config.ts
Expand Up @@ -358,11 +358,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) => {
Expand Down
38 changes: 38 additions & 0 deletions test/collections-graphql/int.spec.ts
@@ -1,8 +1,10 @@
import { GraphQLClient } from 'graphql-request'
import path from 'path'

import type { Post } from './payload-types'

import payload from '../../packages/payload/src'
import getFileByPath from '../../packages/payload/src/uploads/getFileByPath'
import { mapAsync } from '../../packages/payload/src/utilities/mapAsync'
import { initPayloadTest } from '../helpers/configHelpers'
import { idToString } from '../helpers/idToString'
Expand Down Expand Up @@ -945,6 +947,42 @@ describe('collections-graphql', () => {
expect(queriedDoc.title).toEqual('2')
expect(queriedDoc.relationToSelf.title).toEqual('1')
})

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 response = (await client.request(query)) as any

const queriedDoc = response.CyclicalRelationship
expect(queriedDoc.media.id).toEqual(mediaDoc.id)
expect(queriedDoc.media.title).toEqual('example')
})
})
})

Expand Down

0 comments on commit cac52da

Please sign in to comment.