Skip to content

Instantly share code, notes, and snippets.

@acro5piano
Last active August 10, 2018 07:28
Show Gist options
  • Select an option

  • Save acro5piano/b1a44b9cf9d40fdcb679e906651ec752 to your computer and use it in GitHub Desktop.

Select an option

Save acro5piano/b1a44b9cf9d40fdcb679e906651ec752 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" \
| perl -pe 's/^(\s*)#/\1\/\//' \
| perl -pe 's/!//g' \
| perl -pe 's/^input /export interface /g' \
| perl -pe 's/^enum /export enum /g' \
| perl -pe 's/^type /export interface /g' \
| perl -pe 's/^union /export type /g'\
| perl -pe 's/^schema /export interface schema /g' \
| perl -pe 's/^(\s+[a-zA-Z0-9_-]+\s*)\([^\)]+\)(\s*:\s*)/\1\2/g' \
| perl -pe 's/([:\=\|]\s*)\[ID!?\]!?($|\s+|\))/\1string\[\]\2/g' \
| perl -pe 's/([:\=\|]\s*)ID!?($|\s+|\))/\1string\2/g' \
| perl -pe 's/([:\=\|]\s*)\[String!?\]!?($|\s+|\))/\1string\[\]\2/g' \
| perl -pe 's/([:\=\|]\s*)String!?($|\s+|\))/\1string\2/g' \
| perl -pe 's/([:\=\|]\s*)\[Int!?\]!?($|\s+|\))/\1number\[\]\2/g' \
| perl -pe 's/([:\=\|]\s*)Int!?($|\s+|\))/\1number\2/g' \
| perl -pe 's/([:\=\|]\s*)\[Float!?\]!?($|\s+|\))/\1number\[\]\2/g' \
| perl -pe 's/([:\=\|]\s*)Float!?($|\s+|\))/\1number\2/g' \
| perl -pe 's/([:\=\|]\s*)\[Boolean!?\]!?($|\s+|\))/\1boolean[\]\2/g' \
| perl -pe 's/([:\=\|]\s*)Boolean!?($|\s+|\))/\1boolean\2/g' \
| perl -pe 's/\[([a-zA-Z0-9_-]+)!?\]!?/\1\[\]/'
@acro5piano
Copy link
Copy Markdown
Author

Perl version, as for not GNU sed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment