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,9 +201,10 @@ export function getDefaultMutations (options) {
return mutations; return mutations;
} }
const registerCollectionCallbacks = typeName => { const registerCollectionCallbacks = (typeName, options) => {
typeName = typeName.toLowerCase(); typeName = typeName.toLowerCase();
if (options.create) {
registerCallback({ registerCallback({
name: `${typeName}.create.validate`, name: `${typeName}.create.validate`,
iterator: { document: 'The document being inserted' }, iterator: { document: 'The document being inserted' },
@ -219,7 +220,7 @@ const registerCollectionCallbacks = typeName => {
registerCallback({ registerCallback({
name: `${typeName}.create.before`, name: `${typeName}.create.before`,
iterator: { document: 'The document being inserted' }, iterator: { document: 'The document being inserted' },
properties: [ { currentUser: 'The current user' }], properties: [{ currentUser: 'The current user' }],
runs: 'sync', runs: 'sync',
returns: 'document', returns: 'document',
description: `Perform operations on a new document before it's inserted in the database.`, description: `Perform operations on a new document before it's inserted in the database.`,
@ -243,7 +244,8 @@ const registerCollectionCallbacks = typeName => {
returns: null, returns: null,
description: `Perform operations on a new document after it's inserted in the database asynchronously.`, description: `Perform operations on a new document after it's inserted in the database asynchronously.`,
}); });
}
if (options.update) {
registerCallback({ registerCallback({
name: `${typeName}.update.validate`, name: `${typeName}.update.validate`,
iterator: { data: 'The client data' }, iterator: { data: 'The client data' },
@ -258,7 +260,7 @@ const registerCollectionCallbacks = typeName => {
}); });
registerCallback({ registerCallback({
name: `${typeName}.update.before`, name: `${typeName}.update.before`,
iterator: { data: 'The client data'}, iterator: { data: 'The client data' },
properties: [ properties: [
{ document: 'The document being edited' }, { document: 'The document being edited' },
{ currentUser: 'The current user' }, { currentUser: 'The current user' },
@ -269,7 +271,7 @@ const registerCollectionCallbacks = typeName => {
}); });
registerCallback({ registerCallback({
name: `${typeName}.update.after`, name: `${typeName}.update.after`,
iterator: { newDocument: 'The document after the update'}, iterator: { newDocument: 'The document after the update' },
properties: [ properties: [
{ document: 'The document being edited' }, { document: 'The document being edited' },
{ currentUser: 'The current user' }, { currentUser: 'The current user' },
@ -290,7 +292,8 @@ const registerCollectionCallbacks = typeName => {
returns: null, returns: null,
description: `Perform operations on a document after it's updated in the database asynchronously.`, description: `Perform operations on a document after it's updated in the database asynchronously.`,
}); });
}
if (options.delete) {
registerCallback({ registerCallback({
name: `${typeName}.delete.validate`, name: `${typeName}.delete.validate`,
iterator: { document: 'The document being removed' }, iterator: { document: 'The document being removed' },
@ -321,4 +324,5 @@ const registerCollectionCallbacks = typeName => {
returns: null, returns: null,
description: `Perform operations on a document after it's removed from the database asynchronously.`, 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);
/** /**