wikijs-fork/server/graph/resolvers/page.js

142 lines
3.7 KiB
JavaScript
Raw Normal View History

2018-05-20 22:50:51 +00:00
const graphHelper = require('../../helpers/graph')
/* global WIKI */
module.exports = {
Query: {
async pages() { return {} }
},
Mutation: {
async pages() { return {} }
},
PageQuery: {
2018-11-25 06:28:20 +00:00
async history(obj, args, context, info) {
return WIKI.models.pageHistory.getHistory({
pageId: args.id,
offsetPage: args.offsetPage || 0,
offsetSize: args.offsetSize || 100
2018-11-25 06:28:20 +00:00
})
2019-03-09 05:51:02 +00:00
},
async search (obj, args, context) {
if (WIKI.data.searchEngine) {
return WIKI.data.searchEngine.query(args.query, args)
} else {
return {
results: [],
suggestions: [],
totalHits: 0
}
}
},
async list (obj, args, context, info) {
return WIKI.models.pages.query().column([
'id',
'path',
{ locale: 'localeCode' },
'title',
'description',
'isPublished',
'isPrivate',
'privateNS',
'contentType',
'createdAt',
'updatedAt'
]).modify(queryBuilder => {
if (args.limit) {
queryBuilder.limit(args.limit)
}
const orderDir = args.orderByDirection === 'DESC' ? 'desc' : 'asc'
switch (args.orderBy) {
case 'CREATED':
queryBuilder.orderBy('createdAt', orderDir)
break
case 'PATH':
queryBuilder.orderBy('path', orderDir)
break
case 'TITLE':
queryBuilder.orderBy('title', orderDir)
break
case 'UPDATED':
queryBuilder.orderBy('updatedAt', orderDir)
break
default:
queryBuilder.orderBy('id', orderDir)
break
}
})
2019-07-02 05:48:19 +00:00
},
async single (obj, args, context, info) {
let page = await WIKI.models.pages.getPageFromDb(args.id)
if (page) {
return {
...page,
locale: page.localeCode,
editor: page.editorKey
}
} else {
throw new WIKI.Error.PageNotFound()
}
},
async tags (obj, args, context, info) {
return WIKI.models.tags.query().orderBy('tag', 'asc')
}
2018-05-20 22:50:51 +00:00
},
PageMutation: {
2018-07-22 04:29:39 +00:00
async create(obj, args, context) {
const page = await WIKI.models.pages.createPage({
...args,
2018-07-22 04:29:39 +00:00
authorId: context.req.user.id
2018-05-20 22:50:51 +00:00
})
return {
2018-07-22 04:29:39 +00:00
responseResult: graphHelper.generateSuccess('Page created successfully.'),
page
2018-05-20 22:50:51 +00:00
}
},
2019-01-26 23:35:56 +00:00
async delete(obj, args, context) {
await WIKI.models.pages.deletePage({
...args,
authorId: context.req.user.id
})
2018-05-20 22:50:51 +00:00
return {
2018-07-22 04:29:39 +00:00
responseResult: graphHelper.generateSuccess('Page has been deleted.')
2018-05-20 22:50:51 +00:00
}
},
async update(obj, args, context) {
const page = await WIKI.models.pages.updatePage({
...args,
authorId: context.req.user.id
})
2018-05-20 22:50:51 +00:00
return {
responseResult: graphHelper.generateSuccess('Page has been updated.'),
page
2018-05-20 22:50:51 +00:00
}
2019-07-06 21:06:42 +00:00
},
async flushCache(obj, args, context) {
try {
await WIKI.models.pages.flushCache()
return {
responseResult: graphHelper.generateSuccess('Pages Cache has been flushed successfully.')
}
} catch (err) {
return graphHelper.generateError(err)
}
},
async migrateToLocale(obj, args, context) {
try {
const count = await WIKI.models.pages.migrateToLocale(args)
return {
responseResult: graphHelper.generateSuccess('Migrated content to target locale successfully.'),
count
}
} catch (err) {
return graphHelper.generateError(err)
}
2018-05-20 22:50:51 +00:00
}
},
Page: {
// comments(pg) {
// return pg.$relatedQuery('comments')
// }
}
}