1

I'm using graphql-codegen to generate types for my graphql server and also using the same schema to generate types for my DB(which is not exactly the same) using mongo plugin for codegen

I've configured codegen to generate to each type with the name <typeName> an appropriate type for the DB schema with the name <typeName>DB, so for the type Person the appropriate type in the DB will be PersonDB.

I need to map these changes in order for codegen to generate a safe graphql resolvers, I can do it with a static dictionary but I prefer doing it automatically by mapping the names.

so, is it possible, using typescript, getting the name of a type name for a generic type T passed to generic typescript function?

here's an example for implementation, when the only missing piece is the getName generic.

// generated.ts
// automatically generated
type Person = {
  name: string;
};
type PersonDB = {
  name: string;
  _id: string;
};

// mapper.ts
import * as Types from "./generated";
// Types["Person"] => Person Type

// type mapper<T> = ...
// type m = mapper<Person>
// type of m should be PersonDB

//possible implementation
type getName<T> = any // todo:how?
type mapper<T> = Types[`${getName<T>}DB`];

an alternative automated process would also be very appreciated.

relevant but not the same:

Eliav Louski
  • 3,593
  • 2
  • 28
  • 52

0 Answers0