fix(graphql): respect draft: true when querying joins (#11869)
The same as https://github.com/payloadcms/payload/pull/11763 but also for GraphQL. The previous fix was working only for the Local API and REST API due to a different method for querying joins in GraphQL.
This commit is contained in:
@@ -379,6 +379,8 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
const { limit, page, sort, where } = args
|
||||
const { req } = context
|
||||
|
||||
const draft = Boolean(args.draft ?? context.req.query?.draft)
|
||||
|
||||
const fullWhere = combineQueries(where, {
|
||||
[field.on]: { equals: parent._id ?? parent.id },
|
||||
})
|
||||
@@ -390,6 +392,7 @@ export const fieldToSchemaMap: FieldToSchemaMap = {
|
||||
return await req.payload.find({
|
||||
collection,
|
||||
depth: 0,
|
||||
draft,
|
||||
fallbackLocale: req.fallbackLocale,
|
||||
limit,
|
||||
locale: req.locale,
|
||||
|
||||
@@ -5,6 +5,11 @@ export const versionsSlug = 'versions'
|
||||
export const Versions: CollectionConfig = {
|
||||
slug: versionsSlug,
|
||||
fields: [
|
||||
{
|
||||
name: 'title',
|
||||
type: 'text',
|
||||
required: true,
|
||||
},
|
||||
{
|
||||
name: 'category',
|
||||
relationTo: 'categories',
|
||||
|
||||
@@ -569,7 +569,7 @@ describe('Joins Field', () => {
|
||||
|
||||
const version = await payload.create({
|
||||
collection: 'versions',
|
||||
data: { categoryVersion: category.id },
|
||||
data: { title: 'version', categoryVersion: category.id },
|
||||
})
|
||||
|
||||
const res = await payload.find({ collection: 'categories-versions', draft: false })
|
||||
@@ -582,7 +582,7 @@ describe('Joins Field', () => {
|
||||
|
||||
const version = await payload.create({
|
||||
collection: 'versions',
|
||||
data: { categoryVersions: [category.id] },
|
||||
data: { title: 'version', categoryVersions: [category.id] },
|
||||
})
|
||||
|
||||
const res = await payload.find({ collection: 'categories-versions', draft: false })
|
||||
@@ -595,7 +595,7 @@ describe('Joins Field', () => {
|
||||
|
||||
const version = await payload.create({
|
||||
collection: 'versions',
|
||||
data: { categoryVersion: category.id },
|
||||
data: { title: 'version', categoryVersion: category.id },
|
||||
})
|
||||
|
||||
const res = await payload.find({
|
||||
@@ -615,7 +615,14 @@ describe('Joins Field', () => {
|
||||
|
||||
const version = await payload.create({
|
||||
collection: 'versions',
|
||||
data: { _status: 'draft', categoryVersion: category.id },
|
||||
data: { title: 'original-title', _status: 'draft', categoryVersion: category.id },
|
||||
draft: true,
|
||||
})
|
||||
|
||||
await payload.update({
|
||||
collection: 'versions',
|
||||
id: version.id,
|
||||
data: { title: 'updated-title' },
|
||||
draft: true,
|
||||
})
|
||||
|
||||
@@ -625,6 +632,7 @@ describe('Joins Field', () => {
|
||||
})
|
||||
|
||||
expect(res.docs[0].relatedVersions.docs[0].id).toBe(version.id)
|
||||
expect(res.docs[0].relatedVersions.docs[0].title).toBe('updated-title')
|
||||
})
|
||||
|
||||
it('should populate joins when versions on both sides draft true payload.db.queryDrafts', async () => {
|
||||
@@ -632,7 +640,7 @@ describe('Joins Field', () => {
|
||||
|
||||
const version = await payload.create({
|
||||
collection: 'versions',
|
||||
data: { categoryVersions: [category.id] },
|
||||
data: { categoryVersions: [category.id], title: 'version' },
|
||||
})
|
||||
|
||||
const res = await payload.find({
|
||||
@@ -932,6 +940,52 @@ describe('Joins Field', () => {
|
||||
)
|
||||
})
|
||||
|
||||
it('should populate joins with hasMany when on both sides documents are in draft', async () => {
|
||||
const category = await payload.create({
|
||||
collection: 'categories-versions',
|
||||
data: { _status: 'draft' },
|
||||
draft: true,
|
||||
})
|
||||
|
||||
const version = await payload.create({
|
||||
collection: 'versions',
|
||||
data: { _status: 'draft', title: 'original-title', categoryVersion: category.id },
|
||||
draft: true,
|
||||
})
|
||||
|
||||
await payload.update({
|
||||
collection: 'versions',
|
||||
draft: true,
|
||||
id: version.id,
|
||||
data: { title: 'updated-title' },
|
||||
})
|
||||
|
||||
const query = `query {
|
||||
CategoriesVersions(draft: true) {
|
||||
docs {
|
||||
relatedVersions(
|
||||
limit: 1
|
||||
) {
|
||||
docs {
|
||||
id,
|
||||
title
|
||||
}
|
||||
hasNextPage
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
||||
const res = await restClient
|
||||
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
|
||||
.then((res) => res.json())
|
||||
|
||||
expect(res.data.CategoriesVersions.docs[0].relatedVersions.docs[0].id).toBe(version.id)
|
||||
expect(res.data.CategoriesVersions.docs[0].relatedVersions.docs[0].title).toBe(
|
||||
'updated-title',
|
||||
)
|
||||
})
|
||||
|
||||
it('should have simple paginate for joins inside groups', async () => {
|
||||
const queryWithLimit = `query {
|
||||
Categories(where: {
|
||||
|
||||
@@ -54,6 +54,7 @@ export type SupportedTimezones =
|
||||
| 'Asia/Singapore'
|
||||
| 'Asia/Tokyo'
|
||||
| 'Asia/Seoul'
|
||||
| 'Australia/Brisbane'
|
||||
| 'Australia/Sydney'
|
||||
| 'Pacific/Guam'
|
||||
| 'Pacific/Noumea'
|
||||
@@ -464,6 +465,7 @@ export interface Singular {
|
||||
*/
|
||||
export interface Version {
|
||||
id: string;
|
||||
title: string;
|
||||
category?: (string | null) | Category;
|
||||
categoryVersion?: (string | null) | CategoriesVersion;
|
||||
categoryVersions?: (string | CategoriesVersion)[] | null;
|
||||
@@ -999,6 +1001,7 @@ export interface UploadsSelect<T extends boolean = true> {
|
||||
* via the `definition` "versions_select".
|
||||
*/
|
||||
export interface VersionsSelect<T extends boolean = true> {
|
||||
title?: T;
|
||||
category?: T;
|
||||
categoryVersion?: T;
|
||||
categoryVersions?: T;
|
||||
@@ -1235,4 +1238,3 @@ declare module 'payload' {
|
||||
// @ts-ignore
|
||||
export interface GeneratedTypes extends Config {}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user