diff --git a/src/collections/dataloader.ts b/src/collections/dataloader.ts index 4906b83a00..67259e0fb7 100644 --- a/src/collections/dataloader.ts +++ b/src/collections/dataloader.ts @@ -54,12 +54,16 @@ const batchAndLoadDocs = (req: PayloadRequest): BatchLoadFn 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, ], }; } diff --git a/src/graphql/schema/buildObjectType.ts b/src/graphql/schema/buildObjectType.ts index 805701be25..5fb6dee3ac 100644 --- a/src/graphql/schema/buildObjectType.ts +++ b/src/graphql/schema/buildObjectType.ts @@ -389,8 +389,6 @@ function buildObjectType({ } if (id) { - id = id.toString(); - const relatedDocument = await context.req.payloadDataLoader.load(JSON.stringify([ relatedCollectionSlug, id, diff --git a/test/collections-graphql/config.ts b/test/collections-graphql/config.ts index b425cb5c72..842fccc334 100644 --- a/test/collections-graphql/config.ts +++ b/test/collections-graphql/config.ts @@ -64,6 +64,11 @@ export default buildConfig({ type: 'relationship', relationTo: relationSlug, }, + { + name: 'relationToCustomID', + type: 'relationship', + relationTo: 'custom-ids', + }, // Relation hasMany { name: 'relationHasManyField', @@ -86,10 +91,26 @@ export default buildConfig({ }, ], }, + { + slug: 'custom-ids', + access: { + read: () => true, + }, + fields: [ + { + name: 'id', + type: 'number', + }, + { + name: 'title', + type: 'text', + }, + ], + }, collectionWithName(relationSlug), collectionWithName('dummy'), ], - onInit: async (payload) => { + onInit: async payload => { await payload.create({ collection: 'users', data: { @@ -98,12 +119,29 @@ export default buildConfig({ }, }); + await payload.create({ + collection: 'custom-ids', + data: { + id: 1, + title: 'hello', + }, + }); + + await payload.create({ + collection: slug, + data: { + title: 'has custom ID relation', + relationToCustomID: 1, + }, + }); + await payload.create({ collection: slug, data: { title: 'post1', }, }); + await payload.create({ collection: slug, data: { diff --git a/test/collections-graphql/int.spec.ts b/test/collections-graphql/int.spec.ts index 4dbffcaabb..600f761aed 100644 --- a/test/collections-graphql/int.spec.ts +++ b/test/collections-graphql/int.spec.ts @@ -364,6 +364,29 @@ describe('collections-graphql', () => { expect(docs).toContainEqual(expect.objectContaining({ id: specialPost.id })); }); }); + + describe('relationships', () => { + it('should query on relationships with custom IDs', async () => { + const query = `query { + Posts(where: { title: { equals: "has custom ID relation" }}) { + docs { + id + title + relationToCustomID { + id + } + } + totalDocs + } + }`; + + const response = await client.request(query); + const { docs, totalDocs } = response.Posts; + + expect(totalDocs).toStrictEqual(1); + expect(docs[0].relationToCustomID.id).toStrictEqual(1); + }); + }); }); describe('Error Handler', () => {