Merge pull request #2028 from OrigenStudio/feature/defaults-resolvers-mutations-options

Minor default resolvers and mutations improvements
This commit is contained in:
Sacha Greif 2018-08-11 12:26:20 +09:00 committed by GitHub
commit 772d3f47ef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 131 additions and 125 deletions

View file

@ -16,17 +16,17 @@ export function getDefaultMutations (options) {
if (typeof arguments[0] === 'object') { if (typeof arguments[0] === 'object') {
// new single-argument API // new single-argument API
typeName = arguments[0].typeName; typeName = arguments[0].typeName;
collectionName = getCollectionName(typeName); // TODO: find more reliable way to get type name from collection name? collectionName = arguments[0].collectionName || getCollectionName(typeName);
mutationOptions = arguments[0].options || defaultOptions; mutationOptions = { ...defaultOptions, ...arguments[0].options };
} else { } else {
// OpenCRUD backwards compatibility // OpenCRUD backwards compatibility
collectionName = arguments[0]; collectionName = arguments[0];
typeName = getTypeName(collectionName); typeName = getTypeName(collectionName);
mutationOptions = arguments[1] || defaultOptions; mutationOptions = { ...defaultOptions, ...arguments[1] };
} }
// register callbacks for documentation purposes // register callbacks for documentation purposes
registerCollectionCallbacks(typeName); registerCollectionCallbacks(typeName, mutationOptions);
const mutations = {}; const mutations = {};
@ -201,124 +201,128 @@ export function getDefaultMutations (options) {
return mutations; return mutations;
} }
const registerCollectionCallbacks = typeName => { const registerCollectionCallbacks = (typeName, options) => {
typeName = typeName.toLowerCase(); typeName = typeName.toLowerCase();
registerCallback({ if (options.create) {
name: `${typeName}.create.validate`, registerCallback({
iterator: { document: 'The document being inserted' }, name: `${typeName}.create.validate`,
properties: [ iterator: { document: 'The document being inserted' },
{ document: 'The document being inserted' }, properties: [
{ currentUser: 'The current user' }, { document: 'The document being inserted' },
{ validationErrors: 'An object that can be used to accumulate validation errors' }, { currentUser: 'The current user' },
], { validationErrors: 'An object that can be used to accumulate validation errors' },
runs: 'sync', ],
returns: 'document', runs: 'sync',
description: `Validate a document before insertion (can be skipped when inserting directly on server).`, returns: 'document',
}); description: `Validate a document before insertion (can be skipped when inserting directly on server).`,
registerCallback({ });
name: `${typeName}.create.before`, registerCallback({
iterator: { document: 'The document being inserted' }, name: `${typeName}.create.before`,
properties: [ { currentUser: 'The current user' }], iterator: { document: 'The document being inserted' },
runs: 'sync', properties: [{ currentUser: 'The current user' }],
returns: 'document', runs: 'sync',
description: `Perform operations on a new document before it's inserted in the database.`, returns: 'document',
}); description: `Perform operations on a new document before it's inserted in the database.`,
registerCallback({ });
name: `${typeName}.create.after`, registerCallback({
iterator: { document: 'The document being inserted' }, name: `${typeName}.create.after`,
properties: [{ currentUser: 'The current user' }], iterator: { document: 'The document being inserted' },
runs: 'sync', properties: [{ currentUser: 'The current user' }],
returns: 'document', runs: 'sync',
description: `Perform operations on a new document after it's inserted in the database but *before* the mutation returns it.`, returns: 'document',
}); description: `Perform operations on a new document after it's inserted in the database but *before* the mutation returns it.`,
registerCallback({ });
name: `${typeName}.create.async`, registerCallback({
iterator: { document: 'The document being inserted' }, name: `${typeName}.create.async`,
properties: [ iterator: { document: 'The document being inserted' },
{ currentUser: 'The current user' }, properties: [
{ collection: 'The collection the document belongs to' }, { currentUser: 'The current user' },
], { collection: 'The collection the document belongs to' },
runs: 'async', ],
returns: null, runs: 'async',
description: `Perform operations on a new document after it's inserted in the database asynchronously.`, returns: null,
}); description: `Perform operations on a new document after it's inserted in the database asynchronously.`,
});
registerCallback({ }
name: `${typeName}.update.validate`, if (options.update) {
iterator: { data: 'The client data' }, registerCallback({
properties: [ name: `${typeName}.update.validate`,
{ document: 'The document being edited' }, iterator: { data: 'The client data' },
{ currentUser: 'The current user' }, properties: [
{ validationErrors: 'An object that can be used to accumulate validation errors' }, { document: 'The document being edited' },
], { currentUser: 'The current user' },
runs: 'sync', { validationErrors: 'An object that can be used to accumulate validation errors' },
returns: 'modifier', ],
description: `Validate a document before update (can be skipped when updating directly on server).`, runs: 'sync',
}); returns: 'modifier',
registerCallback({ description: `Validate a document before update (can be skipped when updating directly on server).`,
name: `${typeName}.update.before`, });
iterator: { data: 'The client data'}, registerCallback({
properties: [ name: `${typeName}.update.before`,
{ document: 'The document being edited' }, iterator: { data: 'The client data' },
{ currentUser: 'The current user' }, properties: [
], { document: 'The document being edited' },
runs: 'sync', { currentUser: 'The current user' },
returns: 'modifier', ],
description: `Perform operations on a document before it's updated in the database.`, runs: 'sync',
}); returns: 'modifier',
registerCallback({ description: `Perform operations on a document before it's updated in the database.`,
name: `${typeName}.update.after`, });
iterator: { newDocument: 'The document after the update'}, registerCallback({
properties: [ name: `${typeName}.update.after`,
{ document: 'The document being edited' }, iterator: { newDocument: 'The document after the update' },
{ currentUser: 'The current user' }, properties: [
], { document: 'The document being edited' },
runs: 'sync', { currentUser: 'The current user' },
returns: 'document', ],
description: `Perform operations on a document after it's updated in the database but *before* the mutation returns it.`, runs: 'sync',
}); returns: 'document',
registerCallback({ description: `Perform operations on a document after it's updated in the database but *before* the mutation returns it.`,
name: `${typeName}.update.async`, });
iterator: { newDocument: 'The document after the edit' }, registerCallback({
properties: [ name: `${typeName}.update.async`,
{ document: 'The document before the edit' }, iterator: { newDocument: 'The document after the edit' },
{ currentUser: 'The current user' }, properties: [
{ collection: 'The collection the document belongs to' }, { document: 'The document before the edit' },
], { currentUser: 'The current user' },
runs: 'async', { collection: 'The collection the document belongs to' },
returns: null, ],
description: `Perform operations on a document after it's updated in the database asynchronously.`, runs: 'async',
}); returns: null,
description: `Perform operations on a document after it's updated in the database asynchronously.`,
registerCallback({ });
name: `${typeName}.delete.validate`, }
iterator: { document: 'The document being removed' }, if (options.delete) {
properties: [ registerCallback({
{ currentUser: 'The current user' }, name: `${typeName}.delete.validate`,
{ validationErrors: 'An object that can be used to accumulate validation errors' }, iterator: { document: 'The document being removed' },
], properties: [
runs: 'sync', { currentUser: 'The current user' },
returns: 'document', { validationErrors: 'An object that can be used to accumulate validation errors' },
description: `Validate a document before removal (can be skipped when removing directly on server).`, ],
}); runs: 'sync',
registerCallback({ returns: 'document',
name: `${typeName}.delete.before`, description: `Validate a document before removal (can be skipped when removing directly on server).`,
iterator: { document: 'The document being removed' }, });
properties: [{ currentUser: 'The current user' }], registerCallback({
runs: 'sync', name: `${typeName}.delete.before`,
returns: null, iterator: { document: 'The document being removed' },
description: `Perform operations on a document before it's removed from the database.`, properties: [{ currentUser: 'The current user' }],
}); runs: 'sync',
registerCallback({ returns: null,
name: `${typeName}.delete.async`, description: `Perform operations on a document before it's removed from the database.`,
properties: [ });
{ document: 'The document being removed' }, registerCallback({
{ currentUser: 'The current user' }, name: `${typeName}.delete.async`,
{ collection: 'The collection the document belongs to' }, properties: [
], { document: 'The document being removed' },
runs: 'async', { currentUser: 'The current user' },
returns: null, { collection: 'The collection the document belongs to' },
description: `Perform operations on a document after it's removed from the database asynchronously.`, ],
}); runs: 'async',
returns: null,
description: `Perform operations on a document after it's removed from the database asynchronously.`,
});
}
}; };

View file

@ -18,13 +18,13 @@ export function getDefaultResolvers(options) {
if (typeof arguments[0] === 'object') { if (typeof arguments[0] === 'object') {
// new single-argument API // new single-argument API
typeName = arguments[0].typeName; typeName = arguments[0].typeName;
collectionName = getCollectionName(typeName); // TODO: find more reliable way to get type name from collection name collectionName = arguments[0].collectionName || getCollectionName(typeName);
resolverOptions = arguments[0].options || defaultOptions; resolverOptions = { ...defaultOptions, ...arguments[0].options };
} else { } else {
// OpenCRUD backwards compatibility // OpenCRUD backwards compatibility
collectionName = arguments[0]; collectionName = arguments[0];
typeName = getTypeName(collectionName); typeName = getTypeName(collectionName);
resolverOptions = arguments[1] || defaultOptions; resolverOptions = { ...defaultOptions, ...arguments[1] };
} }
return { return {

View file

@ -20,8 +20,10 @@ export const Collections = [];
export const getCollection = name => Collections.find(({ options: { collectionName }}) => name === collectionName || name === collectionName.toLowerCase()); export const getCollection = name => Collections.find(({ options: { collectionName }}) => name === collectionName || name === collectionName.toLowerCase());
// TODO: find more reliable way to get collection name from type name?
export const getCollectionName = typeName => Utils.pluralize(typeName); export const getCollectionName = typeName => Utils.pluralize(typeName);
// TODO: find more reliable way to get type name from collection name?
export const getTypeName = collectionName => collectionName.slice(0,-1); export const getTypeName = collectionName => collectionName.slice(0,-1);
/** /**