new behaviour: single resolver returns null if documentId is not defined

This commit is contained in:
Eric Burel 2018-10-29 18:41:17 +01:00
parent 4dda9919fe
commit 10603d000f
2 changed files with 6 additions and 2 deletions

View file

@ -49,7 +49,6 @@
"intl": "^1.2.4", "intl": "^1.2.4",
"intl-locales-supported": "^1.0.0", "intl-locales-supported": "^1.0.0",
"juice": "^1.11.0", "juice": "^1.11.0",
"later": "^1.2.0",
"lodash": "^4.17.10", "lodash": "^4.17.10",
"mailchimp": "^1.1.6", "mailchimp": "^1.1.6",
"marked": "^0.3.9", "marked": "^0.3.9",

View file

@ -110,12 +110,17 @@ export function getDefaultResolvers(options) {
// use Dataloader if doc is selected by documentId/_id // use Dataloader if doc is selected by documentId/_id
const documentId = selector.documentId || selector._id; const documentId = selector.documentId || selector._id;
const doc = documentId ? await collection.loader.load(documentId) : await Connectors.get(collection, selector); // documentId can be undefined, this is NOT a failure case
// for example it allows form to have a "edit" and "new" mode withtout
// needing to swap the withSingle hoc
if (!documentId) return { result: null };
const doc = await collection.loader.load(documentId);
if (!doc) { if (!doc) {
if (allowNull) { if (allowNull) {
return { result: null }; return { result: null };
} else { } else {
// if documentId is provided but no document is found, this is usually a failure case
const MissingDocumentError = createError('app.missing_document', { message: 'app.missing_document' }); const MissingDocumentError = createError('app.missing_document', { message: 'app.missing_document' });
throw new MissingDocumentError({ data: { documentId, selector } }); throw new MissingDocumentError({ data: { documentId, selector } });
} }