简体   繁体   English

从 TypeScript 生成 GraphQL 架构?

[英]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.comApollo 服务器等热门项目可用于该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 GraphQL 模式生成 GraphQL 操作 - How to generate GraphQL operations from GraphQL schema 如果我不使用放大,如何从 AppSync GraphQL 架构生成 Typescript 定义? - How to generate Typescript definitions from AppSync GraphQL schema if I am not using amplify? 覆盖 GraphQL Schema 生成的 Typescript 类型 - Override from GraphQL Schema generated Typescript types 从打字稿生成 JSON 模式 - Generate JSON schema from typescript 如何在没有操作的情况下使用 graphql schema.json 为 graphql 类型生成打字稿接口? - How to generate typescript interfaces for graphql types using a graphql schema.json without operations? 从 Typescript 类型/接口生成 Joi 模式 - Generate Joi Schema from Typescript types/interfaces 如何从 Typescript 模式创建 GraphQL 类型 SDK? - How to create a GraphQL typed SDK from a Typescript schema object? 如何从 Swagger 模式生成基本的 TypeScript 接口? - How to generate basic TypeScript interfaces from Swagger schema? 如何使用 apollo client:codegen 从 schema.graphql 中提取打字稿接口 - How to extract a typescript interfaces from a schema.graphql with apollo client:codegen graphql codegen typescript-如何仅生成类型 - graphql codegen typescript - how to generate types only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM