overblog / GraphQLBundle

This bundle provides tools to build a complete GraphQL API server in your Symfony App.
MIT License
783 stars 221 forks source link

[Feature Request] Generate TypeScript types automatically #1035

Closed martinszeltins closed 2 years ago

martinszeltins commented 2 years ago

When working with TypeScript on the frontend, it would be nice if it was possible to have TypeScript types automatically generated. Perhaps, it would be possible to add a Symfony command generate:typescript or something like that, which in turn would create a graphql.ts file with all TypeScript type definitions?

A workaround for me has been to take the generated schema.graphql and manually by hand convert it to TypeScript.

For example:

export interface User {
    __typename?: "User"
    username?: Maybe<Scalars["String"]>
}

export interface Address {
    __typename?: "Address"
    street?: Maybe<Scalars["String"]>
    houseNumber?: Maybe<Scalars["String"]>
    postalCode?: Maybe<Scalars["String"]>
    city?: Maybe<Scalars["String"]>
    country?: Maybe<Scalars["String"]>
    countryName?: Maybe<Scalars["String"]>
}
Vincz commented 2 years ago

It doesn't really make sense to generate the TS files from the backend.

You can do it with the frontend from the schema. You should check this: https://www.graphql-code-generator.com/

There is a bunch of awesome generators for Typescript and other GQL clients.

wesleylether commented 1 year ago

@Vincz @mcg-web I do use Attributes for Generating the GraphQL Queries and types. But then It would be perfect if those schemas can be generated for typescript use

Vincz commented 1 year ago

Hi @wesleylether. As state in my previous comment, you can easily generate your Typescript stuff based on your schema. Did you try this: https://the-guild.dev/graphql/codegen? You just need to dump your graphQL schema after everything is configured properly with attributes and stuff, and generate your Typescript types from this schema.

wesleylether commented 1 year ago

@Vincz Ahh my apologies. I did not know there was a dump graphQL schema option 😊 ! Thanks this is perfect indeed