(targetMaybeThunk, options = {})
| 28 | } |
| 29 | |
| 30 | function resolverFactory(targetMaybeThunk, options = {}) { |
| 31 | assert( |
| 32 | typeof targetMaybeThunk === 'function' || checkIsModel(targetMaybeThunk) || checkIsAssociation(targetMaybeThunk), |
| 33 | 'resolverFactory should be called with a model, an association or a function (which resolves to a model or an association)' |
| 34 | ); |
| 35 | |
| 36 | const contextToOptions = _.assign({}, resolverFactory.contextToOptions, options.contextToOptions); |
| 37 | |
| 38 | assert(options.include === undefined, 'Include support has been removed in favor of dataloader batching'); |
| 39 | if (options.before === undefined) options.before = (options) => options; |
| 40 | if (options.after === undefined) options.after = (result) => result; |
| 41 | if (options.handleConnection === undefined) options.handleConnection = true; |
| 42 | |
| 43 | return async function (source, args, context, info) { |
| 44 | let target = typeof targetMaybeThunk === 'function' && !checkIsModel(targetMaybeThunk) ? |
| 45 | await Promise.resolve(targetMaybeThunk(source, args, context, info)) : targetMaybeThunk |
| 46 | , isModel = checkIsModel(target) |
| 47 | , isAssociation = checkIsAssociation(target) |
| 48 | , association = isAssociation && target |
| 49 | , model = isAssociation && target.target || isModel && target |
| 50 | , type = info.returnType |
| 51 | , list = options.list || |
| 52 | type instanceof GraphQLList || |
| 53 | type instanceof GraphQLNonNull && type.ofType instanceof GraphQLList; |
| 54 | |
| 55 | let targetAttributes = Object.keys(model.rawAttributes) |
| 56 | , findOptions = argsToFindOptions(args, targetAttributes); |
| 57 | |
| 58 | info = { |
| 59 | ...info, |
| 60 | type: type, |
| 61 | source: source, |
| 62 | target: target |
| 63 | }; |
| 64 | |
| 65 | context = context || {}; |
| 66 | |
| 67 | if (isConnection(type)) { |
| 68 | type = nodeType(type); |
| 69 | } |
| 70 | |
| 71 | type = type.ofType || type; |
| 72 | |
| 73 | findOptions.attributes = targetAttributes; |
| 74 | findOptions.logging = findOptions.logging || context.logging; |
| 75 | findOptions.graphqlContext = context; |
| 76 | |
| 77 | _.each(contextToOptions, (as, key) => { |
| 78 | findOptions[as] = context[key]; |
| 79 | }); |
| 80 | |
| 81 | return Promise.resolve(options.before(findOptions, args, context, info)).then(function (findOptions) { |
| 82 | if (args.where && !_.isEmpty(info.variableValues)) { |
| 83 | whereQueryVarsToValues(args.where, info.variableValues); |
| 84 | whereQueryVarsToValues(findOptions.where, info.variableValues); |
| 85 | } |
| 86 | |
| 87 | if (list && !findOptions.order) { |
nothing calls this directly
no test coverage detected