fix: #2685, graphql querying relationships with custom id

This commit is contained in:
James
2023-05-22 16:40:24 -04:00
parent 2f209e3e9b
commit 9bb5470342
4 changed files with 26 additions and 13 deletions

View File

@@ -54,12 +54,16 @@ const batchAndLoadDocs = (req: PayloadRequest): BatchLoadFn<string, TypeWithID>
const idField = payload.collections?.[collection].config.fields.find((field) => fieldAffectsData(field) && field.name === 'id');
if (isValidID(id, getIDType(idField))) {
let sanitizedID: string | number = id
if (idField?.type === 'number') sanitizedID = parseFloat(id)
if (isValidID(sanitizedID, getIDType(idField))) {
return {
...batches,
[batchKey]: [
...batches[batchKey] || [],
id,
sanitizedID,
],
};
}

View File

@@ -389,8 +389,6 @@ function buildObjectType({
}
if (id) {
id = id.toString();
const relatedDocument = await context.req.payloadDataLoader.load(JSON.stringify([
relatedCollectionSlug,
id,

View File

@@ -93,6 +93,9 @@ export default buildConfig({
},
{
slug: 'custom-ids',
access: {
read: () => true,
},
fields: [
{
name: 'id',
@@ -127,10 +130,18 @@ export default buildConfig({
await payload.create({
collection: slug,
data: {
title: 'post1',
title: 'has custom ID relation',
relationToCustomID: 1,
},
});
await payload.create({
collection: slug,
data: {
title: 'post1',
},
});
await payload.create({
collection: slug,
data: {

View File

@@ -368,17 +368,17 @@ describe('collections-graphql', () => {
describe('relationships', () => {
it('should query on relationships with custom IDs', async () => {
const query = `query {
Posts(where: { title: { equals: "post1" }}) {
docs {
id
title
relationToCustomID {
Posts(where: { title: { equals: "has custom ID relation" }}) {
docs {
id
title
relationToCustomID {
id
}
}
totalDocs
}
totalDocs
}
}`;
}`;
const response = await client.request(query);
const { docs, totalDocs } = response.Posts;