@@ -12,6 +12,8 @@ import { idToString } from '../helpers/idToString.js'
12
12
import { initPayloadInt } from '../helpers/initPayloadInt.js'
13
13
import { errorOnHookSlug , pointSlug , relationSlug , slug } from './config.js'
14
14
15
+ const formatID = ( id : number | string ) => ( typeof id === 'number' ? id : `"${ id } "` )
16
+
15
17
const title = 'title'
16
18
17
19
let restClient : NextRESTClient
@@ -1010,6 +1012,99 @@ describe('collections-graphql', () => {
1010
1012
const queriedDoc = res . data . CyclicalRelationships . docs [ 0 ]
1011
1013
expect ( queriedDoc . title ) . toEqual ( queriedDoc . relationToSelf . title )
1012
1014
} )
1015
+
1016
+ it ( 'should still query hasMany relationships when some document was deleted' , async ( ) => {
1017
+ const relation_1_draft = await payload . create ( {
1018
+ collection : 'relation' ,
1019
+ data : { _status : 'draft' , name : 'relation_1_draft' } ,
1020
+ draft : true ,
1021
+ } )
1022
+
1023
+ const relation_2 = await payload . create ( {
1024
+ collection : 'relation' ,
1025
+ data : { name : 'relation_2' , _status : 'published' } ,
1026
+ } )
1027
+
1028
+ await payload . create ( {
1029
+ collection : 'posts' ,
1030
+ draft : true ,
1031
+ data : {
1032
+ _status : 'draft' ,
1033
+ title : 'post with relations in draft' ,
1034
+ relationHasManyField : [ relation_1_draft . id , relation_2 . id ] ,
1035
+ } ,
1036
+ } )
1037
+
1038
+ await payload . delete ( { collection : 'relation' , id : relation_1_draft . id } )
1039
+
1040
+ const query = `query {
1041
+ Posts(draft:true,where: { title: { equals: "post with relations in draft" }}) {
1042
+ docs {
1043
+ id
1044
+ title
1045
+ relationHasManyField {
1046
+ id,
1047
+ name
1048
+ }
1049
+ }
1050
+ totalDocs
1051
+ }
1052
+ }`
1053
+
1054
+ const res = await restClient
1055
+ . GRAPHQL_POST ( { body : JSON . stringify ( { query } ) } )
1056
+ . then ( ( res ) => res . json ( ) )
1057
+
1058
+ const queriedDoc = res . data . Posts . docs [ 0 ]
1059
+ expect ( queriedDoc . title ) . toBe ( 'post with relations in draft' )
1060
+
1061
+ expect ( queriedDoc . relationHasManyField [ 0 ] . id ) . toBe ( relation_2 . id )
1062
+ } )
1063
+
1064
+ it ( 'should still query hasMany relationships when user doesnt have access to some document' , async ( ) => {
1065
+ const relation_1_draft = await payload . create ( {
1066
+ collection : 'relation' ,
1067
+ data : { name : 'restricted' } ,
1068
+ } )
1069
+
1070
+ const relation_2 = await payload . create ( {
1071
+ collection : 'relation' ,
1072
+ data : { name : 'relation_2' } ,
1073
+ } )
1074
+
1075
+ await payload . create ( {
1076
+ collection : 'posts' ,
1077
+ draft : true ,
1078
+ data : {
1079
+ _status : 'draft' ,
1080
+ title : 'post with relation restricted' ,
1081
+ relationHasManyField : [ relation_1_draft . id , relation_2 . id ] ,
1082
+ } ,
1083
+ } )
1084
+
1085
+ const query = `query {
1086
+ Posts(draft:true,where: { title: { equals: "post with relation restricted" }}) {
1087
+ docs {
1088
+ id
1089
+ title
1090
+ relationHasManyField {
1091
+ id,
1092
+ name
1093
+ }
1094
+ }
1095
+ totalDocs
1096
+ }
1097
+ }`
1098
+
1099
+ const res = await restClient
1100
+ . GRAPHQL_POST ( { body : JSON . stringify ( { query } ) } )
1101
+ . then ( ( res ) => res . json ( ) )
1102
+
1103
+ const queriedDoc = res . data . Posts . docs [ 0 ]
1104
+ expect ( queriedDoc . title ) . toBe ( 'post with relation restricted' )
1105
+
1106
+ expect ( queriedDoc . relationHasManyField [ 0 ] . id ) . toBe ( relation_2 . id )
1107
+ } )
1013
1108
} )
1014
1109
} )
1015
1110
@@ -1106,7 +1201,7 @@ describe('collections-graphql', () => {
1106
1201
} )
1107
1202
1108
1203
const query = `{
1109
- CyclicalRelationship(id: ${ typeof newDoc . id === 'number' ? newDoc . id : `" ${ newDoc . id } "` } ) {
1204
+ CyclicalRelationship(id: ${ formatID ( newDoc . id ) } ) {
1110
1205
media {
1111
1206
id
1112
1207
title
0 commit comments