Skip to content

Commit

Permalink
Support access: false in items API and schema type printer (#5096)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Mar 11, 2021
1 parent e2edaaf commit b7ce464
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-timers-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': patch
---

Updated items API to handle static `false` access control.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ function getTypeNodeForType(type: GraphQLType): TypeNode {

export function getCoerceAndValidateArgumentsFnForGraphQLField(
schema: GraphQLSchema,
field: GraphQLField<any, any>
field?: GraphQLField<any, any>
) {
if (!field) return;
const variableDefintions: VariableDefinitionNode[] = [];

for (const arg of field.args) {
Expand Down
11 changes: 10 additions & 1 deletion packages-next/keystone/src/lib/itemAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function itemAPIForList(
const listKey = list.key;
return {
findOne({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.findOne) throw new Error('You do not have access to this resource');
const args = getArgs.findOne(rawArgs) as { where: { id: string } };
if (resolveFields) {
return getItem({ listKey, context, returnFields: resolveFields, itemId: args.where.id });
Expand All @@ -51,6 +52,7 @@ export function itemAPIForList(
}
},
findMany({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.findMany) throw new Error('You do not have access to this resource');
const args = getArgs.findMany(rawArgs);
if (resolveFields) {
return getItems({ listKey, context, returnFields: resolveFields, ...args });
Expand All @@ -59,10 +61,12 @@ export function itemAPIForList(
}
},
async count(rawArgs) {
const args = getArgs.count(rawArgs);
if (!getArgs.count) throw new Error('You do not have access to this resource');
const args = getArgs.count(rawArgs!);
return (await list.listQueryMeta(args, context)).getCount();
},
createOne({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.createOne) throw new Error('You do not have access to this resource');
const { data } = getArgs.createOne(rawArgs);
if (resolveFields) {
return createItem({ listKey, context, returnFields: resolveFields, item: data });
Expand All @@ -71,6 +75,7 @@ export function itemAPIForList(
}
},
createMany({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.createMany) throw new Error('You do not have access to this resource');
const { data } = getArgs.createMany(rawArgs);
if (resolveFields) {
return createItems({ listKey, context, returnFields: resolveFields, items: data });
Expand All @@ -79,6 +84,7 @@ export function itemAPIForList(
}
},
updateOne({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.updateOne) throw new Error('You do not have access to this resource');
const { id, data } = getArgs.updateOne(rawArgs);
if (resolveFields) {
return updateItem({ listKey, context, returnFields: resolveFields, item: { id, data } });
Expand All @@ -87,6 +93,7 @@ export function itemAPIForList(
}
},
updateMany({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.updateMany) throw new Error('You do not have access to this resource');
const { data } = getArgs.updateMany(rawArgs);
if (resolveFields) {
return updateItems({ listKey, context, returnFields: resolveFields, items: data });
Expand All @@ -95,6 +102,7 @@ export function itemAPIForList(
}
},
deleteOne({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.deleteOne) throw new Error('You do not have access to this resource');
const { id } = getArgs.deleteOne(rawArgs);
if (resolveFields) {
return deleteItem({ listKey, context, returnFields: resolveFields, itemId: id });
Expand All @@ -103,6 +111,7 @@ export function itemAPIForList(
}
},
deleteMany({ resolveFields = 'id', ...rawArgs }) {
if (!getArgs.deleteMany) throw new Error('You do not have access to this resource');
const { ids } = getArgs.deleteMany(rawArgs);
if (resolveFields) {
return deleteItems({ listKey, context, returnFields: resolveFields, items: ids });
Expand Down
7 changes: 6 additions & 1 deletion packages-next/keystone/src/lib/schema-type-printer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export function printGeneratedTypes(

const { gqlNames } = list;
let listTypeInfoName = `${listKey}ListTypeInfo`;
const listQuery = queryNodeFieldsByName[gqlNames.listQueryName];
printedTypes += `
export type ${listTypeInfoName} = {
key: ${JSON.stringify(listKey)};
Expand All @@ -146,7 +147,11 @@ export type ${listTypeInfoName} = {
update: ${gqlNames.updateInputName};
};
args: {
listQuery: ${printArgs(queryNodeFieldsByName[gqlNames.listQueryName].arguments!)}
listQuery: ${
listQuery
? printArgs(listQuery.arguments!)
: 'import("@keystone-next/types").BaseGeneratedListTypes["args"]["listQuery"]'
}
};
};
Expand Down

1 comment on commit b7ce464

@vercel
Copy link

@vercel vercel bot commented on b7ce464 Mar 11, 2021

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.