Generate Javascript static types from GraphQL: TypeScript and Flow

David Mráz
Nov 21, 2018 · 8 min read
Image for post
Image for post

Introduction

Model example of manual static typing for GraphQL queries

{ "data": {   "subscriptions": [{      "id": "02b7d240-0d44-11ea-bbff-1b2383f1b30b",      "email": "david@atheros.ai"     }]  }}

How would we achieve that with GraphQL?

Image for post
Image for post

Generating types on the frontend with GraphQL CodeGen

git clone git@github.com:atherosai/next-react-graphql-apollo-hooks.git
npm i
npm run dev

GraphQL CodeGen yaml file

schema: 'server/schema/typeDefs.ts'documents: 'components/**/*.graphql'generates:generated/typescript-operations.ts:- typescript-operations- typescript
graphql-codegen
npm run gen:schema

TypeScript output and how to use it in your React components

Generating typed-safe resolvers on your server with GraphQLCodeGen

schema: 'server/schema/typeDefs.ts'documents: 'components/**/*.graphql'generates:__generated__/typescript-operations.ts:- typescript-operations- typescriptserver/__generated__/resolver-types.ts:- typescript- typescript-resolvers
npm run gen:schema

How to take it even further?

Summary


Atheros

Everything you need to know about building AI products.

David Mráz

Written by

Founder of https://atheros.ai | Artificial Intelligence | Machine Learning | JavaScript, Python, C++ | Business

Atheros

Atheros

Everything you need to know about building AI products.

David Mráz

Written by

Founder of https://atheros.ai | Artificial Intelligence | Machine Learning | JavaScript, Python, C++ | Business

Atheros

Atheros

Everything you need to know about building AI products.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store