/
graphql.js
154 lines (121 loc) 路 3.66 KB
/
graphql.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
/**
* Module dependencies
*/
// Public node modules.
const _ = require('lodash');
// Strapi helper for GraphQL.
const helpers = require('strapi/lib/configuration/hooks/graphql/helpers/');
const utils = require('./');
/**
* Utils functions for BookShelf
*/
module.exports = {
/**
* Get collection identity
*
* @return {String}
*/
getCollectionIdentity: collection => {
return _.capitalize(collection.forge().tableName);
},
/**
* Fetch one record
*
* @return {Object}
*/
fetch: (collectionIdentity, collection, criteria) => {
return collection.forge(criteria)
.fetch({withRelated: helpers.getAssociationsByIdentity(collectionIdentity)})
.then(data => _.isEmpty(data) ? data : data.toJSON());
},
/**
* Fetch all records
*
* @return {Array}
*/
fetchAll: (collectionIdentity, collection, criteria) => {
const filters = _.omit(helpers.handleFilters(criteria), value => {
return _.isUndefined(value) || _.isNumber(value) ? _.isNull(value) : _.isEmpty(value);
});
return collection.forge()
.query(filters)
.fetchAll({withRelated: helpers.getAssociationsByIdentity(collectionIdentity)})
.then(data => data.toJSON() || data);
},
/**
* Fetch latests records based on criteria
*
* @return {Array}
*/
fetchLatest: (collectionIdentity, collection, criteria) => {
const filters = _.omit(helpers.handleFilters(criteria), value => {
return _.isUndefined(value) || _.isNumber(value) ? _.isNull(value) : _.isEmpty(value);
});
// Handle filters
filters.orderBy = 'createdAt DESC';
filters.limit = filters.count;
delete filters.count;
return collection.forge(criteria)
.query(filters)
.fetchAll({withRelated: helpers.getAssociationsByIdentity(collectionIdentity)})
.then(data => data.toJSON() || data);
},
/**
* Fetch first records based on criteria
*
* @return {Array}
*/
fetchFirst: (collectionIdentity, collection, criteria) => {
const filters = _.omit(helpers.handleFilters(criteria), value => {
return _.isUndefined(value) || _.isNumber(value) ? _.isNull(value) : _.isEmpty(value);
});
// Handle filters
filters.orderBy = 'createdAt ASC';
filters.limit = filters.count;
delete filters.count;
return collection.forge(criteria)
.query(filters)
.fetchAll({withRelated: helpers.getAssociationsByIdentity(collectionIdentity)})
.then(data => data.toJSON() || data);
},
/**
* Create record
*
* @return {Object}
*/
create: (collectionIdentity, rootValue) => {
return strapi.services[collectionIdentity.toLowerCase()]
.add(rootValue.context.request.body)
.then(data => _.isFunction(_.get(data, 'toJSON')) ? data.toJSON() : data);
},
/**
* Update record
*
* @return {Object}
*/
update: (collectionIdentity, rootValue, args) => {
_.merge(args, rootValue.context.request.body);
const PK = utils.getPK(collectionIdentity.toLowerCase(), null, strapi.models);
return strapi.services[collectionIdentity.toLowerCase()]
.edit(_.set({}, PK, args[PK]), _.omit(args, PK))
.then(data => _.isFunction(_.get(data, 'toJSON')) ? data.toJSON() : data);
},
/**
* Delete record
*
* @return {Object}
*/
delete: (collectionIdentity, rootValue, args) => {
_.merge(args, rootValue.context.request.body);
return strapi.services[collectionIdentity.toLowerCase()]
.remove(args)
.then(data => _.isFunction(_.get(data, 'toJSON')) ? data.toJSON() : data);
},
/**
* Count records
*
* @return {Array}
*/
count: (collectionIdentity, collection) => collection.forge().count()
};