import createQueryResolverStub , { QueryResolverCallback , } from "./createQueryResolverStub" ; import { Resolver , Resolvers , TestResolvers } from "./createTestRenderer" ; type ValueOrCallbackRecursive < T > = { [ P in keyof T ] ? : ValueOrCallbackRecursive < T [ P ] > | ( ( ) => void ) ; } ; type ResolverResult < T extends Resolver < any , any > > = T extends Resolver < any , infer R > ? R : never ; type OverwriteQueryResolverTemplate < T extends Resolvers = any > = { [ P in keyof Required < T > [ "Query" ] ] : ValueOrCallbackRecursive < ResolverResult < Required < T > [ "Query" ] [ P ] > > ; } ; param original param overwrite function overwriteRecursive ( original , overwrite ) { let ret = original ; Object . keys ( overwrite ) . forEach ( ( k ) => { ret = { ... original , [ k ] : ( ... args ) => { const resolve = ( ) => typeof original [ k ] === "function" ? original [ k ] ( ... args ) : original [ k ] ; if ( overwrite [ k ] ) { if ( typeof overwrite [ k ] === "function" ) { const result = overwrite [ k ] ( ... args ) ; return result !== undefined ? result : resolve ( ) ; } if ( overwrite [ k ] instanceof Object && ! Array . isArray ( overwrite [ k ] ) ) { return overwriteRecursive ( resolve ( ) , overwrite [ k ] ) ; } return overwrite [ k ] ; } return resolve ( ) ; } , } ; } ) ; return ret ; } param callback export function createQueryResolverOverwrite < T extends Resolver < any , any > > ( callback ) { return createQueryResolverStub ( callback ) ; } /**
 * overwriteQueryResolver allows you to conveniently overwrite the Query resolvers,
 * even if you just want to overwrite a deeply nested field.
 *
 * When your overwrite resolver returns no value, it will fallback to the
 * original resolver.
 *
 * Example usage:
 * ```ts
 * {
 *   resolver: overwriteQueryResolver<GQLResolver>(params.resolvers || {}, {
 *     story: {
 *       comments: createQueryResolverOverwrite<StoryToCommentsResolver>(
 *         ({ variables }) => {
 *           if (variables.tag === "FEATURED") {
 *             return storyWithNoComments.comments;
 *           }
 *           return;
 *         }
 *       ),
 *     },
 *   }),
 * }
 * ```
 */ export default function overwriteQueryResolver < T extends Resolvers < any , any > > ( original , overwriteQuery ) { return { ... original , Query : overwriteRecursive ( original . Query || { } , overwriteQuery ) , } ; }