passes depth to all afterRead performFieldOperations

This commit is contained in:
James
2020-07-20 12:44:29 -04:00
parent dd8f22db2a
commit 0bfc10d55c
17 changed files with 55 additions and 69 deletions

View File

@@ -9,8 +9,8 @@ module.exports = {
{
name: 'author',
label: 'Author',
type: 'text',
maxLength: 100,
type: 'relationship',
relationTo: 'public-users',
required: true,
},
{

View File

@@ -15,5 +15,20 @@ module.exports = {
maxLength: 100,
required: true,
},
{
name: 'relationship',
label: 'Test Relationship',
type: 'relationship',
relationTo: 'localized-posts',
hasMany: true,
required: true,
},
{
name: 'singleRelationship',
label: 'Test Single Relationship',
type: 'relationship',
relationTo: 'localized-posts',
required: true,
},
],
};

View File

@@ -70,6 +70,7 @@ module.exports = {
graphQL: '/graphql',
graphQLPlayground: '/graphql-playground',
},
defaultDepth: 2,
compression: {},
paths: {
scss: path.resolve(__dirname, 'client/scss/overrides.scss'),

View File

@@ -4,6 +4,7 @@ const performFieldOperations = require('../../fields/performFieldOperations');
const register = async (args) => {
const {
depth,
overrideAccess,
config,
collection: {
@@ -81,6 +82,7 @@ const register = async (args) => {
hook: 'afterRead',
operationName: 'read',
req,
depth,
});
// /////////////////////////////////////

View File

@@ -6,6 +6,7 @@ const performFieldOperations = require('../../fields/performFieldOperations');
const update = async (args) => {
const {
depth,
config,
collection: {
Model,
@@ -115,6 +116,7 @@ const update = async (args) => {
hook: 'afterRead',
operationName: 'read',
req,
depth,
});
// /////////////////////////////////////

View File

@@ -1,5 +1,4 @@
const paginate = require('mongoose-paginate-v2');
const autopopulate = require('mongoose-autopopulate');
const buildQueryPlugin = require('../mongoose/buildQuery');
const localizationPlugin = require('../localization/plugin');
const buildSchema = require('../mongoose/buildSchema');
@@ -8,8 +7,7 @@ const buildCollectionSchema = (collection, config, schemaOptions = {}) => {
const schema = buildSchema(collection.fields, { timestamps: collection.timestamps, ...schemaOptions });
schema.plugin(paginate)
.plugin(buildQueryPlugin)
.plugin(autopopulate);
.plugin(buildQueryPlugin);
if (config.localization) {
schema.plugin(localizationPlugin, config.localization);

View File

@@ -2,8 +2,11 @@ const fs = require('fs');
const { NotFound, Forbidden, ErrorDeletingFile } = require('../../errors');
const executeAccess = require('../../auth/executeAccess');
const performFieldOperations = require('../../fields/performFieldOperations');
const deleteQuery = async (args) => {
const {
depth,
collection: {
Model,
config: collectionConfig,
@@ -14,6 +17,7 @@ const deleteQuery = async (args) => {
locale,
fallbackLocale,
},
config,
} = args;
// /////////////////////////////////////
@@ -90,7 +94,19 @@ const deleteQuery = async (args) => {
}
// /////////////////////////////////////
// 4. Execute after collection hook
// 6. Execute field-level hooks and access
// /////////////////////////////////////
result = await performFieldOperations(config, collectionConfig, {
data: result,
hook: 'afterRead',
operationName: 'read',
req,
depth,
});
// /////////////////////////////////////
// 7. Execute after collection hook
// /////////////////////////////////////
await collectionConfig.hooks.afterDelete.reduce(async (priorHook, hook) => {
@@ -100,7 +116,7 @@ const deleteQuery = async (args) => {
}, Promise.resolve());
// /////////////////////////////////////
// 5. Return results
// 8. Return results
// /////////////////////////////////////
return result;

View File

@@ -6,8 +6,8 @@ const find = async (args) => {
where,
page,
limit,
depth,
config,
depth,
collection: {
Model,
config: collectionConfig,
@@ -16,7 +16,6 @@ const find = async (args) => {
req: {
locale,
fallbackLocale,
payloadAPI,
},
} = args;
@@ -74,26 +73,8 @@ const find = async (args) => {
limit: limit || 10,
sort,
collation: sort ? { locale: 'en' } : {}, // case-insensitive sort in MongoDB
options: {
autopopulate: false,
},
};
// Only allow depth override within REST.
// If allowed in GraphQL, it would break resolvers
// as a full object will be returned instead of an ID string
if (payloadAPI === 'REST') {
if (depth && depth !== '0') {
optionsToExecute.options.autopopulate = {
maxDepth: parseInt(depth, 10),
};
} else {
optionsToExecute.options.autopopulate = {
maxDepth: 2,
};
}
}
let result = await Model.paginate(query, optionsToExecute);
// /////////////////////////////////////
@@ -110,6 +91,7 @@ const find = async (args) => {
const data = doc.toJSON({ virtuals: true });
return performFieldOperations(config, collectionConfig, {
depth,
data,
req,
hook: 'afterRead',

View File

@@ -15,7 +15,6 @@ const findByID = async (args) => {
req: {
locale,
fallbackLocale,
payloadAPI,
},
} = args;
@@ -54,26 +53,7 @@ const findByID = async (args) => {
// 3. Perform database operation
// /////////////////////////////////////
const queryOptionsToExecute = {
autopopulate: false,
};
// Only allow depth override within REST.
// If allowed in GraphQL, it would break resolvers
// as a full object will be returned instead of an ID string
if (payloadAPI === 'REST') {
if (depth && depth !== '0') {
queryOptionsToExecute.autopopulate = {
maxDepth: parseInt(depth, 10),
};
} else {
queryOptionsToExecute.autopopulate = {
maxDepth: 2,
};
}
}
let result = await Model.findOne(query, {}, queryOptionsToExecute);
let result = await Model.findOne(query, {});
if (!result && !hasWhereAccess) throw new NotFound();
if (!result && hasWhereAccess) throw new Forbidden();
@@ -89,6 +69,7 @@ const findByID = async (args) => {
// /////////////////////////////////////
result = await performFieldOperations(config, collectionConfig, {
depth,
req,
data: result,
hook: 'afterRead',

View File

@@ -12,6 +12,7 @@ const resizeAndSave = require('../../uploads/imageResizer');
const update = async (args) => {
const {
config,
depth,
collection: {
Model,
config: collectionConfig,
@@ -158,6 +159,7 @@ const update = async (args) => {
hook: 'afterRead',
operationName: 'read',
req,
depth,
});
// /////////////////////////////////////

View File

@@ -9,6 +9,7 @@ const createHandler = (config) => async (req, res, next) => {
collection: req.collection,
config,
data: req.body,
depth: req.query.depth,
});
return res.status(httpStatus.CREATED).json({

View File

@@ -9,6 +9,7 @@ const deleteHandler = (config) => async (req, res, next) => {
collection: req.collection,
config,
id: req.params.id,
depth: req.query.depth,
});
if (!doc) {

View File

@@ -10,6 +10,7 @@ const updateHandler = (config) => async (req, res, next) => {
config,
id: req.params.id,
data: req.body,
depth: req.query.depth,
});
return res.status(httpStatus.OK).json({

View File

@@ -1,5 +1,4 @@
const mongoose = require('mongoose');
const autopopulate = require('mongoose-autopopulate');
const mongooseHidden = require('mongoose-hidden')();
const buildSchema = require('../mongoose/buildSchema');
const localizationPlugin = require('../localization/plugin');
@@ -12,7 +11,6 @@ const buildModel = (config) => {
globalsSchema.plugin(localizationPlugin, config.localization);
}
globalsSchema.plugin(autopopulate);
globalsSchema.plugin(mongooseHidden);
const Globals = mongoose.model('globals', globalsSchema);
@@ -24,7 +22,6 @@ const buildModel = (config) => {
globalSchema.plugin(localizationPlugin, config.localization);
}
globalSchema.plugin(autopopulate);
globalSchema.plugin(mongooseHidden);
Globals.discriminator(globalConfig.slug, globalSchema);

View File

@@ -8,7 +8,6 @@ const findOne = async (args) => {
Model,
req,
req: {
payloadAPI,
locale,
fallbackLocale,
},
@@ -32,23 +31,6 @@ const findOne = async (args) => {
// 3. Perform database operation
// /////////////////////////////////////
const queryOptionsToExecute = {
options: {},
};
// Only allow depth override within REST.
// If allowed in GraphQL, it would break resolvers
// as a full object will be returned instead of an ID string
if (payloadAPI === 'REST') {
if (depth && depth !== '0') {
queryOptionsToExecute.options.autopopulate = {
maxDepth: parseInt(depth, 10),
};
} else {
queryOptionsToExecute.options.autopopulate = false;
}
}
let doc = await Model.findOne({ globalType: slug });
if (!doc) {
@@ -71,6 +53,7 @@ const findOne = async (args) => {
hook: 'afterRead',
operationName: 'read',
req,
depth,
});
// /////////////////////////////////////

View File

@@ -14,6 +14,7 @@ const update = async (args) => {
locale,
fallbackLocale,
},
depth,
} = args;
// /////////////////////////////////////
@@ -91,6 +92,7 @@ const update = async (args) => {
hook: 'afterRead',
operationName: 'read',
req,
depth,
});
// /////////////////////////////////////

View File

@@ -5,6 +5,8 @@ const sanitizeGlobals = require('../globals/sanitize');
const sanitizeConfig = (config) => {
const sanitizedConfig = { ...config };
if (sanitizedConfig.defaultDepth === undefined) sanitizedConfig.defaultDepth = 2;
sanitizedConfig.collections = sanitizedConfig.collections.map((collection) => sanitizeCollection(sanitizedConfig.collections, collection));
if (sanitizedConfig.globals) {