Skip to content

Commit

Permalink
feat: allow to customize crud resolvers
Browse files Browse the repository at this point in the history
(no type checking yet)
  • Loading branch information
macrozone committed Aug 6, 2020
1 parent 309dfdf commit 004fa68
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions packages/backend/src/addCrudResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,48 @@ declare global {
}
}

export default (resourceName: string, { printSecurityWarning = true } = {}) => {
const DEFAULT_MANY = {
filtering: true,
pagination: true,
ordering: true,
};

type Customize = {
// no good types here :-(
one?: (c: {}) => any;
many?: (c: typeof DEFAULT_MANY) => any;
createOne?: (c: {}) => any;
updateOne?: (c: {}) => any;
updateMany?: (c: {}) => any;
upsertOne?: (c: {}) => any;
deleteOne?: (c: {}) => any;
deleteMany?: (c: {}) => any;
};

type Options = {
/**
* whether to display a warning to secure the mutations and resolvers (defaults to true)
*/
printSecurityWarning?: boolean;
customize?: Customize;
};
const addCrudResolvers = (
resourceName: string,
{ printSecurityWarning = true, customize }: Options = {},
) => {
const typeName = upperFirst(resourceName);

const queryName = lowerFirst(resourceName);
const queryAllName = pluralize(queryName);
const queryCountName = `${pluralize(queryName)}Count`;

const mutations = [
`createOne${typeName}`,
`updateOne${typeName}`,
`updateMany${typeName}`,
`upsertOne${typeName}`,
`deleteOne${typeName}`,
`deleteMany${typeName}`,
{ name: `createOne${typeName}`, type: "createOne" },
{ name: `updateOne${typeName}`, type: "updateOne" },
{ name: `updateMany${typeName}`, type: "updateMany" },
{ name: `upsertOne${typeName}`, type: "upsertOne" },
{ name: `deleteOne${typeName}`, type: "deleteOne" },
{ name: `deleteMany${typeName}`, type: "deleteMany" },
];
if (process.env.NODE_ENV === "development" && printSecurityWarning) {
const queries = [queryName, queryAllName, queryCountName];
Expand All @@ -33,7 +61,7 @@ export default (resourceName: string, { printSecurityWarning = true } = {}) => {
);

console.info(`Queries: ${queries.join(" ")}`);
console.info(`Mutations: ${mutations.join(" ")}`);
console.info(`Mutations: ${mutations.map((m) => m.name).join(" ")}`);

console.info(
"☝ please make sure to restirct unauthorized access to these queries using graphq-shield",
Expand All @@ -44,12 +72,9 @@ export default (resourceName: string, { printSecurityWarning = true } = {}) => {
Query: extendType({
type: "Query",
definition(t) {
t.crud[queryName]();
t.crud[queryAllName]({
filtering: true,
pagination: true,
ordering: true,
});
t.crud[queryName](customize?.one?.({}) ?? {});

t.crud[queryAllName](customize?.many?.(DEFAULT_MANY) ?? DEFAULT_MANY);
t.int(queryCountName, {
args: {
where: arg({
Expand Down Expand Up @@ -83,8 +108,12 @@ export default (resourceName: string, { printSecurityWarning = true } = {}) => {
Mutation: extendType({
type: "Mutation",
definition(t) {
mutations.forEach((mutation) => t.crud[mutation](null));
mutations.forEach((mutation) =>
t.crud[mutation.name](customize?.[mutation.type]?.({}) ?? {}),
);
},
}),
};
};

export default addCrudResolvers;

0 comments on commit 004fa68

Please sign in to comment.