Skip to content

Commit

Permalink
Fixes Dataloader error when resolving relationships when access is `f…
Browse files Browse the repository at this point in the history
…alse` on related to-one item (#8216)
  • Loading branch information
Josh Calder committed Jan 3, 2023
1 parent b52713b commit 20bd505
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/shy-falcons-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': patch
---

Fixes Dataloader error when resolving relationships when access is `false` on related to-one item
4 changes: 2 additions & 2 deletions packages/core/src/lib/core/queries/output-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ async function fetchRelatedItems(
) {
const operationAccess = await getOperationAccess(foreignList, context, 'query');
if (!operationAccess) {
return [];
return toFetch.map(() => undefined);
}

const accessFilters = await getAccessFilters(foreignList, context, 'query');
if (accessFilters === false) {
return [];
return toFetch.map(() => undefined);
}

const resolvedWhere = await accessControlledFilter(
Expand Down
39 changes: 38 additions & 1 deletion tests/api-tests/relationships/crud/one-to-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { text, relationship } from '@keystone-6/core/fields';
import { list } from '@keystone-6/core';
import { setupTestRunner } from '@keystone-6/api-tests/test-runner';
import type { KeystoneContext } from '@keystone-6/core/types';
import { allowAll } from '@keystone-6/core/access';
import { allowAll, denyAll } from '@keystone-6/core/access';
import { apiTestConfig, ContextFromRunner } from '../../utils';

type IdType = any;
Expand Down Expand Up @@ -107,6 +107,27 @@ const runner = setupTestRunner({
}),
});

const runnerWithDeny = setupTestRunner({
config: apiTestConfig({
lists: {
Company: list({
access: denyAll,
fields: {
name: text(),
locations: relationship({ ref: 'Location.company', many: true }),
},
}),
Location: list({
access: allowAll,
fields: {
name: text(),
company: relationship({ ref: 'Company.locations' }),
},
}),
},
}),
});

describe(`One-to-many relationships`, () => {
describe('Read', () => {
test(
Expand Down Expand Up @@ -205,6 +226,22 @@ describe(`One-to-many relationships`, () => {
);
})
);

test(
'Deny access to related items returns null',
runnerWithDeny(async ({ context }) => {
await createReadData(context.sudo());
const companies = await context.query.Company.findMany({
query: 'id name',
});
expect(companies.toString()).toBe('');

const locations = await context.query.Location.findMany({
query: 'id name company { id name }',
});
expect(locations[0].company).toBeNull();
})
);
});

describe('Count', () => {
Expand Down

1 comment on commit 20bd505

@vercel
Copy link

@vercel vercel bot commented on 20bd505 Jan 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.