fix: #2685, graphql querying relationships with custom id
This commit is contained in:
@@ -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,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -389,8 +389,6 @@ function buildObjectType({
|
||||
}
|
||||
|
||||
if (id) {
|
||||
id = id.toString();
|
||||
|
||||
const relatedDocument = await context.req.payloadDataLoader.load(JSON.stringify([
|
||||
relatedCollectionSlug,
|
||||
id,
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user