简体   繁体   中英

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 class es. That doesn't work for me; I don't have a single class but a myriad of simple type s 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM