Skip to content

Commit

Permalink
feat: create content model resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Apr 29, 2020
1 parent f1f77a7 commit d0f1ea0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ErrorResponse, NotFoundResponse, Response } from "@webiny/graphql";

export default async (root: any, args: {[key: string]: any}, context: {[key: string]: any}) => {
const { CmsContentModel } = context.models;

const sourceRev = await CmsContentModel.findById(args.revision);
if (!sourceRev) {
return new NotFoundResponse(`Revision with id "${args.revision}" was not found!`);
}

const newRevision = new CmsContentModel();

try {
newRevision.populate({
title: sourceRev.title,
group: await sourceRev.group,
modelId: sourceRev.modelId,
settings: sourceRev.settings,
layout: sourceRev.layout,
fields: sourceRev.fields,
parent: sourceRev.parent
});
await newRevision.save();
} catch (e) {
return new ErrorResponse({
code: e.code,
message: e.message,
data: e.data
});
}
return new Response(newRevision);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { ListResponse, requiresTotalCount } from "@webiny/graphql";

export const listContentModels = async ({ context, args, info }) => {
const { CmsContentModel } = context.models;
const { limit = 10, after, before, sort = null, parent = null } = args;

const query: any = {
latestVersion: true
};

if (parent) {
query.parent = parent;
}

let search = null;
if (args.search) {
search = {
query: args.search,
fields: ["title"],
operator: "or"
};
}

return await CmsContentModel.find({
sort,
limit,
after,
before,
search,
query,
totalCount: requiresTotalCount(info)
});
};

export default async (root: any, args: Object, context: Object, info) => {
const list = await listContentModels({ args, context, info });
return new ListResponse(list, list.getMeta());
};

0 comments on commit d0f1ea0

Please sign in to comment.