John Goofy
John Goofy

Reputation: 998

Generate GraphQL schema from TypeScript?

I have just recently started with Apollo and would like to expose some existing REST APIs through a new GraphQL layer.

These REST services are already fully (Request and Response data structures) covered by an extensive set of custom Typescript type definitions.

I was hoping to find a way to re-use these existing types, and, maybe with a few additional Query types on top, be able to generate a GraphQL schema instead of duplicating the code.

Surprisingly this appears to be a rather rare use case? I found many tools to generate Typescript definitions from GraphQL, but the reverse direction seems to be difficult.

type-graphql only works if all custom types are classes. That doesn't work for me; I don't have a single class but a myriad of simple types here.

ts2graphql hasn't been updated in a year and I can't even get the example code to work.

What else could I try?

Upvotes: 23

Views: 7745

Answers (2)

Tobi
Tobi

Reputation: 1621

Pothos may be able to help here. It's focused on building a GraphQL schema in Typescript without the class requirement of type-graphql. While you still need to define the GraphQL schema manually, you should be able to leverage your existing types.

Upvotes: -1

Michael Sereniti
Michael Sereniti

Reputation: 616

As far as I know there is no way to fully automatically generate graphql schema from typescript code cause typescript much more expressive and can describe only graphql entities but not queries, subscriptions and mutations (without using some magically named generic types).

I found not so popular project ts-graphql that may be helpful for you in semi-automatic describing graphql schema in typescript.

What may be better much way to synchronize typescript and graphql is to generate TS types and interfaces from graphql schema. Popular projects like graphql-code-generator.com or Apollo server could be used for that approach.

Upvotes: 3

Related Questions