apollo-tooling icon indicating copy to clipboard operation
apollo-tooling copied to clipboard

Feature Request: Generate an `index.ts` file in codegen for typescript

Open kestred opened this issue 5 years ago • 5 comments

I'd like to make it easier to import many codegen'd query type definitions at one time.

Problem Statement

Currently the typescript generator will generate a different file for each query. Currently if a single file component uses multiple queries, it might import the queries like so:

import { GetUser } from "myqueries/GetUser";
import { GetFoo } from "myqueries/GetFoo";
import { GetBar } from "myqueries/GetBar";

Ideal Solution

Typescript codegen could also generate an index.ts file in the subdirectory that re-exports all of the queries.

It would be convenient to instead be able to do either:

// Import queries as namespace
import queries from "myqueries";
let something: queries.GetFoo = ...;

// Import specific queries in single import statement
import { GetUser, GetFoo } from "myqueries";

The generated index.ts file could look like:

export * from "./GetBar";
export * from "./GetFoo";
export * from "./GetUser";

or more conservatively

export { GetBar } from "./GetBar";
export { GetFoo } from "./GetFoo";
export { GetUser } from "./GetUser";

Affected Projects

As far as I know, only apollo-codegen-typescript would be affected.

kestred avatar Apr 01 '19 22:04 kestred

Could there be any possible name conflicts?

faurehu avatar Jul 16 '19 23:07 faurehu

This would be super usefull in my case.

Context: I'm using the generated types as a seperate package in a monorepo. I wish i could set up an index.ts with all the generated types as the main file in package.json.

antoniojps avatar Jan 24 '21 17:01 antoniojps

Does anyone have an idea on how to do this until it's implemented?

cglacet avatar Feb 14 '21 17:02 cglacet

@cglacet I used graphql-code-generator which allows you easily do this, they use apollo-tooling under the hood.

antoniojps avatar Mar 09 '21 01:03 antoniojps

@antoniojps Could you point me on how to create an index.ts file that exports * in all the *.ts in the same directory with graphql-code-generator?

ryankubanempire avatar Jul 28 '22 01:07 ryankubanempire