[英]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.我最近刚刚开始使用 Apollo,并希望通过新的 GraphQL 层公开一些现有的 REST API。
These REST services are already fully (Request and Response data structures) covered by an extensive set of custom Typescript type definitions.这些 REST 服务已经完全(请求和响应数据结构)包含在一组广泛的自定义 Typescript 类型定义中。
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.我希望找到一种方法来重用这些现有类型,并且也许在顶部添加一些额外的Query
类型,能够生成 GraphQL 模式而不是复制代码。
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.找了很多工具可以从GraphQL生成Typescript的定义,但是反方向好像很难。
type-graphql only works if all custom types are class
es. type-graphql仅在所有自定义类型均为class
es 时才有效。 That doesn't work for me;那对我不起作用; I don't have a single class but a myriad of simple type
s here.我这里没有一个class 而是无数个简单type
。
ts2graphql hasn't been updated in a year and I can't even get the example code to work. ts2graphql已经一年没有更新了,我什至无法让示例代码运行。
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).据我所知,没有办法从 typescript 代码完全自动生成 graphql 模式,因为 typescript 更具表现力,只能描述 graphql 实体,但不能描述查询、订阅和突变(不使用一些神奇命名的通用类型)。
I found not so popular project ts-graphql that may be helpful for you in semi-automatic describing graphql schema in typescript.我发现不太受欢迎的项目ts-graphql可能对您在 typescript 中半自动描述 graphql 模式有帮助。
What may be better much way to synchronize typescript and graphql is to generate TS types and interfaces from graphql schema.同步 typescript 和 graphql 的更好方法是从 graphql 模式生成 TS 类型和接口。 Popular projects like graphql-code-generator.com or Apollo server could be used for that approach. graphql-code-generator.com或Apollo 服务器等热门项目可用于该方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.