graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Possibility of passing an expression for the mappers config

Open evsar3 opened this issue 5 years ago • 3 comments

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.

evsar3 avatar Feb 23 '21 16:02 evsar3

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

dotansimha avatar Mar 07 '21 15:03 dotansimha

With mapperPatternExclude, yes, looks good.

kamilkisiela avatar Mar 08 '21 08:03 kamilkisiela

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 :) )

janmeier avatar Aug 05 '22 07:08 janmeier