From 2f209e3e9b84548448046478778ab4dfa9eaf4b0 Mon Sep 17 00:00:00 2001 From: Elliot DeNolf Date: Mon, 22 May 2023 16:14:28 -0400 Subject: [PATCH 1/2] chore: recreate issue in test dir --- test/collections-graphql/config.ts | 29 +++++++++++++++++++++++++++- test/collections-graphql/int.spec.ts | 23 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/test/collections-graphql/config.ts b/test/collections-graphql/config.ts index b425cb5c72..ae6d08a2ff 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,23 @@ export default buildConfig({ }, ], }, + { + slug: 'custom-ids', + 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,10 +116,19 @@ export default buildConfig({ }, }); + await payload.create({ + collection: 'custom-ids', + data: { + id: 1, + title: 'hello', + }, + }); + await payload.create({ collection: slug, data: { title: 'post1', + relationToCustomID: 1, }, }); await payload.create({ diff --git a/test/collections-graphql/int.spec.ts b/test/collections-graphql/int.spec.ts index 4dbffcaabb..af2b5b5ed6 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: "post1" }}) { + 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', () => { From 9bb54703423b3f0fdb242a5e63f322d346323b06 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 22 May 2023 16:40:24 -0400 Subject: [PATCH 2/2] fix: #2685, graphql querying relationships with custom id --- src/collections/dataloader.ts | 8 ++++++-- src/graphql/schema/buildObjectType.ts | 2 -- test/collections-graphql/config.ts | 13 ++++++++++++- test/collections-graphql/int.spec.ts | 16 ++++++++-------- 4 files changed, 26 insertions(+), 13 deletions(-) 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 ae6d08a2ff..842fccc334 100644 --- a/test/collections-graphql/config.ts +++ b/test/collections-graphql/config.ts @@ -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: { diff --git a/test/collections-graphql/int.spec.ts b/test/collections-graphql/int.spec.ts index af2b5b5ed6..600f761aed 100644 --- a/test/collections-graphql/int.spec.ts +++ b/test/collections-graphql/int.spec.ts @@ -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;