apollographql / apollo-tooling

✏️ Apollo CLI for client tooling (Mostly replaced by Rover)
https://apollographql.com
MIT License
3.04k stars 469 forks source link

apollo client:codegen Add custom header to output #1043

Open rautenrieth-da opened 5 years ago

rautenrieth-da commented 5 years ago

With --passthroughCustomScalars, the TypeScript codegen will generate files with unknown types for custom scalars, resulting in output that does not compile.

To get my definition of those scalars into the codegen output, I am using --customScalarsPrefix=CustomScalars. and then using a shell script to add the following code at the top of the codegen output:

import * as CustomScalars from './CustomScalars'

To avoid this postprocessing step, can we add an option to prepend arbitrary content to the codegen output?

gaelollivier commented 5 years ago

Same here, it seems the --passthroughCustomScalars is unusable with typescript without an additional post-processing command.

In our case, a custom header would also be useful to add a eslint-disable, since we use eslint instead of tslint

sarfata commented 5 years ago

This article https://www.leighhalliday.com/generating-types-apollo suggests using a global.d.ts file to define the custom scalars type.

It works but it's still not ideal because I do not see a way to automatically do a conversion.

For example: type GQLDateTime = string works (because I am converting dates to ISO strings on the server, but type GQLDateTime = Date does not work.

I would like to define custom scalars and have their constructor or a deserialization function called when the data is received from the server.

RobertStigsson commented 4 years ago

In the back-end there exists a nice utility (https://www.npmjs.com/package/graphql-schema-typescript) which has:

customScalarType: {
    DateTime: "Date",
    MomentDateTime: "Moment"
},
importStatements: {
   `import { Moment } from "moment";`
}

Which makes the development easier and more type secure. Is there anyway to implement something similar in apollo-tooling?