I’m using graphql-codegen to generate apoollo mutations and queries into separate files (I noticed tree shaking does not work at all with a single generated graphql file).
This is where I’m at in my config, I can generate but the files have to be colocated which I don’t want (to make updating all the imports easier):
import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = {
schema: {
"http://localhost:8080/v1/graphql": {
headers: {
"x-hasura-admin-secret": "poop is poop",
secret: "poop is poopity poop",
},
},
},
documents: [
"./**/*.graphql",
"./**/*.ts",
"./**/*.tsx",
"!./node_modules/**",
"!./generated/*.ts",
"!./**/*.generated.*.ts",
],
generates: {
"generated/types.ts": {
plugins: [
{
typescript: {
noExport: true,
},
},
{
"typescript-operations": {
noExport: true,
},
},
],
},
"generated/": {
preset: "near-operation-file",
presetConfig: {
extension: ".generated.tsx",
baseTypesPath: "types.ts", // can we avoid having this?
},
plugins: ["typescript-operations", "typescript-react-apollo"],
},
"generated/graphql-types.d.ts": {
plugins: [
{
typescript: {
noExport: true,
},
},
{
"typescript-operations": {
noExport: true,
},
},
],
config: {
enumsAsTypes: true,
},
},
"generated/graphql-frags.ts": {
plugins: [
{
"fragment-matcher": {
apolloClientVersion: 3,
module: "es2015",
useExplicitTyping: false,
},
},
],
config: {
skipTypename: true,
enumsAsTypes: true, // to ensure enums are not generated
},
},
"generated/graphql-enums.ts": {
plugins: ["typescript"],
config: {
onlyEnums: true,
},
},
"generated/graphql-sdk.ts": {
config: {
rawRequest: true,
enumsAsTypes: true, // to ensure enums are not generated
},
plugins: ["typescript-graphql-request"],
},
},
};
export default config;
I also don’t want to do a baseTYpesPath in near-operation-file because "generated/graphql-types.d.ts"
does exactly what that does without needing to import a giant types file into each individual code gen’d file. In fact if I comment out the ‘import * as types from ‘types” in the generated files and remove the Types.[type] from each usage it compiles and runs fine which for me feels like a cleaner solution that won’t bog down the typoescript transpiling or accidentally break tree shaking.
Any thoughts on how to accomplish the two above?