Commit 7c266683 authored by Jure's avatar Jure

fix(server): correctly address missing records

parent 3d499241
Pipeline #13091 passed with stages
in 12 minutes and 44 seconds
const { ref, lit } = require('objection')
const { NotFoundError } = require('@pubsweet/errors')
const notFoundError = (property, value, className) =>
new NotFoundError(`Object not found: ${className} with ${property} ${value}`)
// create a function which creates a new entity and performs authorization checks
const createCreator = (entityName, EntityModel) => async (
input,
......@@ -140,6 +145,10 @@ const fetchOneCreator = (entityName, EntityModel) => async (
const entity = await ctx.loaders[entityName].load(id)
if (!entity) {
throw notFoundError('id', id, entityName)
}
const outputFilter = await ctx.helpers.canKnowAbout(ctx.user, entity)
return outputFilter(entity)
}
......
......@@ -16,7 +16,12 @@ if (
}
const defaultLoader = model =>
new DataLoader(ids => model.query().whereIn('id', ids))
new DataLoader(async ids => {
const results = await model.query().whereIn('id', ids)
// We map over ids so that the DataLoader API is always matched,
// i.e. array of keys in, array of results out (even if some records are not found)
return ids.map(id => results.find(result => result.id === id))
})
module.exports = () => {
const loaders = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment