Skip to content

Instantly share code, notes, and snippets.

@Diullei
Created April 27, 2017 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Diullei/b63aa2f8aab3486590ab7bb8534d7c2d to your computer and use it in GitHub Desktop.
Save Diullei/b63aa2f8aab3486590ab7bb8534d7c2d to your computer and use it in GitHub Desktop.
Rudimentary graphql to typescript definition generator. Accepts a graphql schema file (not json, nor js) and converts it to typescript defs.
#!/usr/bin/env bash
cat "$1" \
| sed -r 's/^(\s*)#/\1\/\//' \
| sed -r 's/!//g' \
| sed -r 's/^input /export interface /g' \
| sed -r 's/^enum /export enum /g' \
| sed -r 's/^type /export interface /g' \
| sed -r 's/^union /export type /g'\
| sed -r 's/^schema /export interface schema /g' \
| sed -r 's/^(\s+[a-zA-Z0-9_-]+\s*)\([^\)]+\)(\s*:\s*)/\1\2/g' \
| sed -r 's/([:\=\|]\s*)\[ID!?\]!?($|\s+|\))/\1string[]\2/g' \
| sed -r 's/([:\=\|]\s*)ID!?($|\s+|\))/\1string\2/g' \
| sed -r 's/([:\=\|]\s*)\[String!?\]!?($|\s+|\))/\1string[]\2/g' \
| sed -r 's/([:\=\|]\s*)String!?($|\s+|\))/\1string\2/g' \
| sed -r 's/([:\=\|]\s*)\[Int!?\]!?($|\s+|\))/\1number[]\2/g' \
| sed -r 's/([:\=\|]\s*)Int!?($|\s+|\))/\1number\2/g' \
| sed -r 's/([:\=\|]\s*)\[Float!?\]!?($|\s+|\))/\1number[]\2/g' \
| sed -r 's/([:\=\|]\s*)Float!?($|\s+|\))/\1number\2/g' \
| sed -r 's/([:\=\|]\s*)\[Boolean!?\]!?($|\s+|\))/\1boolean[]\2/g' \
| sed -r 's/([:\=\|]\s*)Boolean!?($|\s+|\))/\1boolean\2/g' \
| sed -r 's/\[([a-zA-Z0-9_-]+)!?\]!?/\1[]/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment