dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.84k stars 1.33k forks source link

Possibility of passing an expression for the mappers config #5594

Open evsar3 opened 3 years ago

evsar3 commented 3 years ago

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

Not related to a problem. I'm using Mongoose and in order it to work properly I have to map the Mongoose Document interface of my models to the generated resolvers Types. The case it that the map list is getting quite big, once I have to manually add every new Mongoose model to the map list.

Describe the solution you'd like A pattern that can be set for the mappers to resolve would be very handy. Example:

overwrite: true
schema: "./src/typeDefs/*.graphql"
documents: null
generates:
  src/lib/generated/graphql.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
    config:
      contextType: ../graphql#Context
      useIndexSignature: true
      mapperPattern: ../mongoose/models/{{TypeName}}Model#{{TypeName}}Document

The above expression would resolve for each Type of the schema and produce a result like: (Let's assume TypeName as equal to User) ../mongoose/models/UserModel#UserDocument

For the expressions that could not be resolved to a real reference, the default behavior can be assumed. The maps explicitly set on mappers option, overrides the pattern one.

An additional option mapperPatternExclude as a list of Types names, can be used to exclude some Types to be parsed by the pattern expression.

Describe alternatives you've considered A plugin that implements such behavior.

Additional context Imagine a project with dozens or even hundreds of Types that have to be mapped.

dotansimha commented 3 years ago

This could totally work. @kamilkisiela what do you think?

kamilkisiela commented 3 years ago

With mapperPatternExclude, yes, looks good.

janmeier commented 2 years ago

I would be interested in implementing this feature, but I would like a pointer or two before getting started.

I'm thinking it would make sense to add the check for mapperPattern after the check for mappers, around here https://github.com/dotansimha/graphql-code-generator/blob/cf408868c119c6e2e6b7b79c845353da08547420/packages/plugins/other/visitor-plugin-common/src/base-resolvers-visitor.ts#L556-L559

Would that make sense?

(If you would rather discuss this over an actual PR, let me know and I will open one so we can discuss an actual implementation :) )