Skip to content

Allow typescript-resolvers plugin to export resolver function alias type #7373

@smykhailov

Description

@smykhailov

Is your feature request related to a problem? Please describe.

Currently root resolver types exported the following way:

export type QueryResolvers<ContextType = IContext, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
  accounts?: Resolver<Maybe<ReadonlyArray<Maybe<ResolversTypes['Account']>>>, ParentType, ContextType>;
};

the accounts type Resolver<Maybe<ReadonlyArray<Maybe<ResolversTypes['Account']>>>, ParentType, ContextType>; is too long and complex to reuse it in TS files, for example, when I need to define resolver functions in the separate file and define its type:

export const accountsResolver: Resolver<Maybe<ReadonlyArray<Maybe<ResolversTypes['Account']>>>, ParentType, ContextType> = async (_, __, ctx) => {
  const res = ...

  return res.map(item => {
    return {
      id: item._id.toString(),
      name: item.name,
      balance: item.balance || 0,
    };
  });
};

Describe the solution you'd like

I propose to add additional optional config parameter, and in addition to this long construction generate alias for it:

export type AccountsResolverFn<ContextType = IContext, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = Resolver<Maybe<ReadonlyArray<Maybe<ResolversTypes['Account']>>>, ParentType, ContextType> | undefined;

with this change the code becomes much simpler:

export const accountsResolver: AccountsResolverFn = async (_, __, ctx) => {
  const res = ...

  return res.map(item => {
    return {
      id: item._id.toString(),
      name: item.name,
      balance: item.balance || 0,
    };
  });
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions