Currently, I am utilizing the graphql-code-generator to automatically generate TypeScript types from my GraphQL schema. Within GraphQL, it is possible to define custom scalar types that result in specific type mappings, as seen below in the following code snippet:
export type Scalars = {
String: string;
Boolean: boolean;
NodeDescription: any;
};
export type CodeResource = {
ast: Scalars["NodeDescription"];
};
The standard GraphQL scalar types like String and Boolean are mapped to their corresponding TypeScript types, while custom scalar types such as NodeDescription are assigned the general TypeScript type of any.
In my Angular client project, there already exists a type named NodeDescription located at ../app/shared/syntaxtree which I prefer to use instead of the default any type generated by graphql-code-generator for NodeDescription. Is there a way to override the NodeDescription field within the Scalars type? Ultimately, I aim to have the ast field in the CodeResource type reflect the NodeDescription type rather than any.
One approach I considered was overriding the Scalars["NodeDescription"] type with NodeDescription. To achieve this, I attempted to import all types into a new file and perform the necessary overrides:
import {Scalars} from "./graphql";
import {NodeDescription} from "../app/shared/syntaxtree";
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
type Scalar = Overwrite<Scalars, {NodeDescription: NodeDescription}>
While this method was somewhat successful, the ast field within the CodeResource type remained typed as any. Another possibility involves using a carefully crafted bash script with sed commands to directly edit the generated file contents to incorporate the desired changes:
import {NodeDescription} from "../app/shared/syntaxtree";
export type Scalars = {
String: string;
Boolean: boolean;
NodeDescription: NodeDescription;
};
However, before proceeding with the bash script modification, I would appreciate insights on whether there exists a more elegant TypeScript-based solution for overriding the auto-generated types. Thank you for any guidance provided.