diff --git a/packages/utils/src/resolve-additional-resolvers.ts b/packages/utils/src/resolve-additional-resolvers.ts index 189f721b1c71a..512db01912932 100644 --- a/packages/utils/src/resolve-additional-resolvers.ts +++ b/packages/utils/src/resolve-additional-resolvers.ts @@ -264,16 +264,9 @@ export function resolveAdditionalResolversWithoutImport( } const resolverData = { root, args, context, info, env: process.env }; const targetArgs: any = {}; - for (const argPath in additionalResolver.sourceArgs) { - dset( - targetArgs, - argPath, - stringInterpolator.parse( - additionalResolver.sourceArgs[argPath].toString(), - resolverData, - ), - ); - } + + deeplySetArgs(resolverData, { targetArgs }, 'targetArgs', additionalResolver.sourceArgs) + const options: any = { ...baseOptions, root, @@ -321,148 +314,22 @@ export function resolveAdditionalResolvers( return resolvers; } else { - const baseOptions: any = {}; - if (additionalResolver.result) { - baseOptions.valuesFromResults = generateValuesFromResults(additionalResolver.result); - } - if ('pubsubTopic' in additionalResolver) { - return { - [additionalResolver.targetTypeName]: { - [additionalResolver.targetFieldName]: { - subscribe: withFilter( - (root, args, context, info) => { - const resolverData = { root, args, context, info, env: process.env }; - const topic = stringInterpolator.parse( - additionalResolver.pubsubTopic, - resolverData, - ); - return pubsub.asyncIterator(topic) as AsyncIterableIterator; - }, - (root, args, context, info) => { - return additionalResolver.filterBy - ? // eslint-disable-next-line no-new-func - new Function(`return ${additionalResolver.filterBy}`)() - : true; - }, - ), - resolve: (payload: any) => { - if (baseOptions.valuesFromResults) { - return baseOptions.valuesFromResults(payload); - } - return payload; - }, - }, - }, - }; - } else if ('keysArg' in additionalResolver) { - return { - [additionalResolver.targetTypeName]: { - [additionalResolver.targetFieldName]: { - selectionSet: - additionalResolver.requiredSelectionSet || `{ ${additionalResolver.keyField} }`, - resolve: async (root: any, args: any, context: any, info: any) => { - if (!baseOptions.selectionSet) { - baseOptions.selectionSet = generateSelectionSetFactory( - info.schema, - additionalResolver, - ); - } - const resolverData = { root, args, context, info, env: process.env }; - const targetArgs: any = {}; - for (const argPath in additionalResolver.additionalArgs || {}) { - dset( - targetArgs, - argPath, - stringInterpolator.parse( - additionalResolver.additionalArgs[argPath], - resolverData, - ), - ); - } - const options: any = { - ...baseOptions, - root, - context, - info, - argsFromKeys: (keys: string[]) => { - const args: any = {}; - dset(args, additionalResolver.keysArg, keys); - Object.assign(args, targetArgs); - return args; - }, - key: lodashGet(root, additionalResolver.keyField), - }; - return context[additionalResolver.sourceName][additionalResolver.sourceTypeName][ - additionalResolver.sourceFieldName - ](options); - }, - }, - }, - }; - } else if ('targetTypeName' in additionalResolver) { - return { - [additionalResolver.targetTypeName]: { - [additionalResolver.targetFieldName]: { - selectionSet: additionalResolver.requiredSelectionSet, - resolve: (root: any, args: any, context: any, info: GraphQLResolveInfo) => { - // Assert source exists - if (!context[additionalResolver.sourceName]) { - throw new Error(`No source found named "${additionalResolver.sourceName}"`); - } - if (!context[additionalResolver.sourceName][additionalResolver.sourceTypeName]) { - throw new Error( - `No root type found named "${additionalResolver.sourceTypeName}" exists in the source ${additionalResolver.sourceName}\n` + - `It should be one of the following; ${Object.keys( - context[additionalResolver.sourceName], - ).join(',')})}}`, - ); - } - if ( - !context[additionalResolver.sourceName][additionalResolver.sourceTypeName][ - additionalResolver.sourceFieldName - ] - ) { - throw new Error( - `No field named "${additionalResolver.sourceFieldName}" exists in the type ${additionalResolver.sourceTypeName} from the source ${additionalResolver.sourceName}`, - ); - } - - if (!baseOptions.selectionSet) { - baseOptions.selectionSet = generateSelectionSetFactory( - info.schema, - additionalResolver, - ); - } - const resolverData = { root, args, context, info, env: process.env }; - const targetArgs: any = {}; - for (const argPath in additionalResolver.sourceArgs) { - dset( - targetArgs, - argPath, - stringInterpolator.parse( - additionalResolver.sourceArgs[argPath].toString(), - resolverData, - ), - ); - } - const options: any = { - ...baseOptions, - root, - args: targetArgs, - context, - info, - }; - return context[additionalResolver.sourceName][additionalResolver.sourceTypeName][ - additionalResolver.sourceFieldName - ](options); - }, - }, - }, - }; - } else { - return additionalResolver; - } + return resolveAdditionalResolversWithoutImport(additionalResolver, pubsub); } }), ); } + + +function deeplySetArgs(resolverData: any, args: object, path: string, value: any) { + if (typeof value === 'string') { + dset(args, path, stringInterpolator.parse( + value.toString(), + resolverData, + )) + } else { + for (const key in value) { + deeplySetArgs(resolverData, args, `${path}.${key}`, value[key]) + } + } +}