'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?
Solution 1:[1]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mikhail Sereniti |