Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to add prefix/suffix to all generated type names #3440

Open
1 task done
jtomaszewski opened this issue Jan 14, 2025 · 2 comments
Open
1 task done

Allow to add prefix/suffix to all generated type names #3440

jtomaszewski opened this issue Jan 14, 2025 · 2 comments
Labels

Comments

@jtomaszewski
Copy link

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

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

If I have a User database entity, a User GQL schema object, and perhaps even some more User interfaces, it's easy to mistake one with another.

When a file relies on both (e.g. a user resolver that fetches User db entity in order to resolve with a User GQL schema object), import aliases need to be used such as Import { User as UserSchema } from '...', which often need to be written manually by the developer, and are prone to typos and lack of consistency in the codebase.

Describe the solution you'd like

Could we make the generated type names configurable? For example, we could add a typeName?(name: string): string option to the GraphQLDefinitionsFactory:

const definitionsFactory = new GraphQLDefinitionsFactory();
definitionsFactory.generate({
  typePaths: ['./src/**/*.graphql'],
  path: join(process.cwd(), 'src/api/schema.d.ts'),
  typeName: (name: string) => `${name}Schema` // new option!
});

This would result in following schema be created:

export interface UserSchema {
    id: string;
    name: string;
}

export interface IQuery {
    users(): UserSchema[] | Promise<UserSchema[]>;
}

instead of this one:

export interface User {
    id: string;
    name: string;
}

export interface IQuery {
    users(): User[] | Promise<User[]>;
}

@kamilmysliwiec I'd be happy to create a PR for this, just let me know if you'd be open to merging it.

Teachability, documentation, adoption, migration strategy

No response

What is the motivation / use case for changing the behavior?

I like to give a prefix or suffix to all classnames in my repository, e.g.:

  • suffix all resolvers with *Resolver (something that is suggested by nestjs docs already),
  • suffix all db entity classes with *Entity
  • suffix all input/DTOs with *Input or *Dto or *InputDto
  • suffix all schema objects with *Schema

However, currently I'm using schema-first approach, and thus TS type definitions of GQL schema interfaces are generated automatically, and the generated type names can't be customized.

@kamilmysliwiec
Copy link
Member

Feel free to create a PR for this feature :)

@synerp
Copy link

synerp commented Jan 16, 2025

I solve this problem with nx libraries. It lets you define the import path for different types of imports.
nx g @nx/nest:lib libs/my-nest-lib --publishable --importPath=@my-workspace/my-nest-lib
Have a look here: https://nx.dev/nx-api/nest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants