dotansimha / graphql-code-generator-community

MIT License
112 stars 143 forks source link

[typescript-rtk-query] allow for automatic transform response in codegen #266

Open DominicGBauer opened 1 year ago

DominicGBauer commented 1 year ago

Describe the solution you'd like

I would like the codegen to automatically generate transformResponse function when api endpoint is generated e.g.

    Feed: build.query<FeedQuery, FeedQueryVariables>({
      query: (variables) => ({ document: FeedDocument, variables })
      transformResponse: (response: FeedQuery) => response
    }),

The reason is that I end up manually adding this every time because the api will send something like data.applications instead of applications

ferrero1987 commented 1 year ago

you have to create a new file and use enhanceEndpoints! to this you add transformResponse

import { api as generatedApi } from '../Feed.generated'
export const feedApi = generatedApi.enhanceEndpoints({
    endpoints: {
      FeedDocument: {
        transformResponse: (response: FeedQuery) => response
      }
    }
  })

export const { useFeedDocument } = feedApi