Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 521
John Goofy Avatar asked Nov 01 '20 23:11

John Goofy


People also ask

How does GraphQL CodeGen work?

What is Graphql-Code Generator (CodeGen)? GraphQL-CodeGen is a CLI that can generate code from your GraphQL operations and schema. In React projects using typescript operations, it is very helpful to map GraphQL schema into interfaces. GraphQL can also generate hooks from operations.

Are your TypeScript types in sync with your GraphQL schema?

If you are using both TypeScript and GraphQL in your projects, you may have found yourself trying to keep your TypeScript types in sync with your GraphQL schema. If you have been doing this manually until now, then this article will be for you.

How to create a GraphQL schema in JavaScript?

The GraphQL implementation in JavaScript comes with the GraphQLSchema class which can be used as follows to define the schema. TypeGraphQL is a quite popular solution for creating GraphQL schemas integrated with TypeScript. It depends on decorators, so make sure you have experimentalDecorators turned on in your config file if you wish to use it.

What is typegraphql and how to use it?

TypeGraphQL allows you to build a schema using TypeScript classes and decorators. It's just syntactic sugar because under the hood TypeGraphQL will still generate regular GraphQL code. We will see the code generated later - for now, let's create the schema.

What is the difference between typescript and nodeJS GraphQL?

GraphQL handles the types for the GraphQL schemas, and TypeScript sets the types on the GraphQL resolvers. However, because you are handling multiple languages, building strongly-typed APIs using Node.js, GraphQL, and TypeScript can be a challenge to maintain.


Video Answer


1 Answers

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.

like image 166
Mikhail Sereniti Avatar answered Oct 19 '22 06:10

Mikhail Sereniti