Ocelot-Social/src/middleware/idMiddleware.js
Robert Schäfer aa07a2a616 Refactor Id Middleware
I found a way to cleanly request additional attributes in our
middleware. We can use this pattern if we e.g. require the author of
posts and comments to check if the user is the author and therefore
authorized to update or delete the post.

CC @mattwr18 @appinteractive @tirokk
2019-02-21 10:50:41 +01:00

25 lines
826 B
JavaScript

import cloneDeep from 'lodash/cloneDeep'
const includeId = async (resolve, root, args, context, resolveInfo) => {
// Keeping the graphql resolveInfo untouched ensures that we don't add the
// following attributes to the result set returned to the graphQL client.
// We only want to pass these attributes to our resolver for internal
// purposes e.g. authorization.
const copy = cloneDeep(resolveInfo)
copy.fieldNodes[0].selectionSet.selections.unshift({
kind: 'Field',
name: { kind: 'Name', value: 'id' }
})
return resolve(root, args, context, copy)
}
export default {
Query: (resolve, root, args, context, info) => {
return includeId(resolve, root, args, context, info)
},
Mutation: (resolve, root, args, context, info) => {
return includeId(resolve, root, args, context, info)
}
}