({
target: targetMaybeThunk,
before,
after,
where,
orderBy: orderByEnum,
ignoreArgs
})
| 121 | } |
| 122 | |
| 123 | export function createConnectionResolver({ |
| 124 | target: targetMaybeThunk, |
| 125 | before, |
| 126 | after, |
| 127 | where, |
| 128 | orderBy: orderByEnum, |
| 129 | ignoreArgs |
| 130 | }) { |
| 131 | before = before || ((options) => options); |
| 132 | after = after || ((result) => result); |
| 133 | |
| 134 | let orderByAttribute = function (orderAttr, {source, args, context, info}) { |
| 135 | return typeof orderAttr === 'function' ? orderAttr(source, args, context, info) : orderAttr; |
| 136 | }; |
| 137 | |
| 138 | let orderByDirection = function (orderDirection, args) { |
| 139 | if (args.last) { |
| 140 | return orderDirection.indexOf('ASC') >= 0 |
| 141 | ? orderDirection.replace('ASC', 'DESC') |
| 142 | : orderDirection.replace('DESC', 'ASC'); |
| 143 | } |
| 144 | return orderDirection; |
| 145 | }; |
| 146 | |
| 147 | /** |
| 148 | * Creates a cursor given a item returned from the Database |
| 149 | * @param {Object} item sequelize row |
| 150 | * @param {Integer} index the index of this item within the results, 0 indexed |
| 151 | * @return {String} The Base64 encoded cursor string |
| 152 | */ |
| 153 | let toCursor = function (item, index) { |
| 154 | const model = getModelOfInstance(item); |
| 155 | const id = model ? |
| 156 | typeof model.primaryKeyAttribute === 'string' ? item[model.primaryKeyAttribute] : null : |
| 157 | item[Object.keys(item)[0]]; |
| 158 | return base64(JSON.stringify([id, index])); |
| 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Decode a cursor into its component parts |
| 163 | * @param {String} cursor Base64 encoded cursor |
| 164 | * @return {Object} Object containing ID and index |
| 165 | */ |
| 166 | let fromCursor = function (cursor) { |
| 167 | let [id, index] = JSON.parse(unbase64(cursor)); |
| 168 | |
| 169 | return { |
| 170 | id, |
| 171 | index |
| 172 | }; |
| 173 | }; |
| 174 | |
| 175 | let argsToWhere = function (args) { |
| 176 | let result = {}; |
| 177 | |
| 178 | if (where === undefined) return result; |
| 179 | |
| 180 | _.each(args, (value, key) => { |
no test coverage detected