-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create content model resolvers
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
...api-headless-cms/src/handler/plugins/graphql/contentModel/resolvers/createRevisionFrom.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
38 changes: 38 additions & 0 deletions
38
.../api-headless-cms/src/handler/plugins/graphql/contentModel/resolvers/listContentModels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}; |