UNPKG

graphql-compose-mongoose

Version:

Plugin for `graphql-compose` which derive a graphql types from a mongoose model.

29 lines (27 loc) 972 B
'use strict'; exports.__esModule = true; exports.projectionHelper = projectionHelper; function projectionHelper(resolveParams) { // eslint-disable-line const projection = resolveParams.projection; if (projection) { // if projection has '*' key, then omit field projection (fetch all fields from database) if (projection['*']) { return; } const flatProjection = {}; Object.keys(projection).forEach(key => { const val = projection[key]; if (val && (val.$meta || val.$slice || val.$elemMatch)) { // pass MongoDB projection operators https://docs.mongodb.com/v3.2/reference/operator/projection/meta/ flatProjection[key] = val; } else { // if not projection operator, then flatten projection flatProjection[key] = !!val; } }); if (Object.keys(flatProjection).length > 0) { resolveParams.query = resolveParams.query.select(flatProjection); // eslint-disable-line } } }