|
| 1 | +import { cursorToId, idToCursor, dotObject } from '../utils'; |
| 2 | +import { projectionCollection, projectionFixRenamed } from './projection'; |
| 3 | +import { |
| 4 | + getArgsFromOpts, |
| 5 | + getRenamedMongooseFields, |
| 6 | +} from './commons'; |
| 7 | +import { connectionArgs } from 'graphql-relay'; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Helper to get an empty connection. |
| 12 | + */ |
| 13 | +export function emptyConnection() { |
| 14 | + return { |
| 15 | + count: 0, |
| 16 | + edges: [], |
| 17 | + pageInfo: { |
| 18 | + startCursor: null, |
| 19 | + endCursor: null, |
| 20 | + hasPreviousPage: false, |
| 21 | + hasNextPage: false, |
| 22 | + }, |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | +export function getIdFromCursor(cursor) { |
| 28 | + if (cursor === undefined || cursor === null) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + return cursorToId(cursor); |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +export function getByConnectionResolver(typeName, resolverOpts = {}) { |
| 37 | + const mongooseModel = Storage.MongooseModels.get(typeName); |
| 38 | + |
| 39 | + if (!mongooseModel) { |
| 40 | + if (process.env.NODE_ENV !== 'production') { |
| 41 | + console.log(`graphql-mongoose warn: could not find model '${typeName}' ` |
| 42 | + + `for getByConnectionResolver. You should call populateModels(${typeName}).`); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const args = { |
| 47 | + ...connectionArgs, |
| 48 | + ...getArgsFromOpts(typeName, resolverOpts), |
| 49 | + }; |
| 50 | + |
| 51 | + const renamedFields = getRenamedMongooseFields(mongooseModel); |
| 52 | + const projectionFn = renamedFields |
| 53 | + ? projectionFixRenamed.bind(this, renamedFields) |
| 54 | + : projectionCollection; |
| 55 | + |
| 56 | + const resolve = async function(root, queryArgs = {}, context, info) { |
| 57 | + const { before, after, first, last, sort = { _id: 1 }, filter } = queryArgs; |
| 58 | + |
| 59 | + const begin = getIdFromCursor(after); |
| 60 | + const end = getIdFromCursor(before); |
| 61 | + |
| 62 | + const skip = (first - last) || 0; |
| 63 | + const limit = last || first; |
| 64 | + |
| 65 | + const selector = Object.assign({}, filter ? dotObject(filter) : null); |
| 66 | + if (begin) { |
| 67 | + selector._id = {}; |
| 68 | + selector._id.$gt = begin; |
| 69 | + } |
| 70 | + |
| 71 | + if (end) { |
| 72 | + selector._id = {}; |
| 73 | + selector._id.$lt = end; |
| 74 | + } |
| 75 | + |
| 76 | + if (mongooseModel) { |
| 77 | + const projFields = projectionFn(info); |
| 78 | + |
| 79 | + let count = 0; |
| 80 | + if (projFields.count) { |
| 81 | + count = await mongoose.getCount(mongooseModel, selector); |
| 82 | + } |
| 83 | + |
| 84 | + const result = await mongoose.getList( |
| 85 | + mongooseModel, |
| 86 | + selector, |
| 87 | + { limit, skip, sort }, |
| 88 | + projFields |
| 89 | + ); |
| 90 | + |
| 91 | + if (result.length === 0) { |
| 92 | + return emptyConnection(); |
| 93 | + } |
| 94 | + |
| 95 | + const edges = result.map((value) => ({ |
| 96 | + cursor: idToCursor(value._id), |
| 97 | + node: value, |
| 98 | + })); |
| 99 | + |
| 100 | + return { |
| 101 | + count, |
| 102 | + edges, |
| 103 | + pageInfo: { |
| 104 | + startCursor: edges[0].cursor, |
| 105 | + endCursor: edges[edges.length - 1].cursor, |
| 106 | + hasPreviousPage: skip !== 0 || !!begin, |
| 107 | + hasNextPage: result.length === limit, |
| 108 | + }, |
| 109 | + }; |
| 110 | + } |
| 111 | + |
| 112 | + return null; |
| 113 | + }; |
| 114 | + |
| 115 | + return { args, resolve }; |
| 116 | +} |
0 commit comments