Skip to content

Commit

Permalink
Make command to generate types from typed metadata in languages other…
Browse files Browse the repository at this point in the history
… than Typescript

PR-URL: hasura/graphql-engine-mono#8238
GitOrigin-RevId: 9a4730e0d311916c0d837586485382c58033c914
  • Loading branch information
m-Bilal authored and hasura-bot committed Mar 14, 2023
1 parent a119260 commit 69c8e8b
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
4 changes: 4 additions & 0 deletions metadata-api-types/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
go/
haskell/
rust/
kotlin/
60 changes: 55 additions & 5 deletions metadata-api-types/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ TYPESCRIPT_ROOT := typescript
TYPESCRIPT_SRC := ${TYPESCRIPT_ROOT}/src
PATCHES := $(wildcard ${TYPESCRIPT_ROOT}/patches/*.patch)

FILE_NAME := metadata.openapi

GO_ROOT := go
GO_FILE := ${FILE_NAME}.go
GO_TARGET := ${GO_ROOT}/${GO_FILE}

RUST_ROOT := rust
RUST_FILE := ${FILE_NAME}.rs
RUST_TARGET := ${RUST_ROOT}/${RUST_FILE}

HASKELL_ROOT := haskell
HASKELL_FILE := ${FILE_NAME}.hs
HASKELL_TARGET := ${HASKELL_ROOT}/${HASKELL_FILE}

KOTLIN_ROOT := kotlin
KOTLIN_FILE := ${FILE_NAME}.kt
KOTLIN_TARGET := ${KOTLIN_ROOT}/${KOTLIN_FILE}

# default target
.PHONY: help
## help: prints help message
Expand All @@ -13,18 +31,50 @@ help:
@sed -n 's/^##//p' ${MAKEFILE_LIST} | column -t -s ':' | sed -e 's/^/ /'

.PHONY: generate-types
## generate-types: Generate the TypeScript types for working with the Metadata API
generate-types: ${TYPESCRIPT_SRC}
## generate-types: Generate types for working with the Metadata API
generate-types: generate-typescript-types generate-go-types generate-rust-types generate-haskell-types generate-kotlin-types

.PHONY: generate-typescript-types
## generate-typescript-types: Generate the TypeScript types for working with the Metadata API
generate-typescript-types: ${TYPESCRIPT_SRC}

.PHONY: generate-go-types
## generate-go-types: Generate the Go types for working with the Metadata API
generate-go-types: ${GO_TARGET}

.PHONY: generate-rust-types
## generate-rust-types: Generate the Rust types for working with the Metadata API
generate-rust-types: ${RUST_TARGET}

.PHONY: generate-haskell-types
## generate-haskell-types: Generate the Haskell types for working with the Metadata API
generate-haskell-types: ${HASKELL_TARGET}

.PHONY: generate-kotlin-types
## generate-kotlin-types: Generate the Kotlin types for working with the Metadata API
generate-kotlin-types: ${KOTLIN_TARGET}

.PHONY: typecheck
## typecheck: Typechecks generated type definitions
typecheck: typecheck-metadata-api-types

${TYPESCRIPT_SRC}: ${SCHEMA_FILE} ${TYPESCRIPT_ROOT}/package.json ${TYPESCRIPT_ROOT}/package-lock.json ${PATCHES}
./scripts/generate-types.sh "${SCHEMA_FILE}"

.PHONY: typecheck-metadata-api-types
## typecheck-metadata-api-types: Typechecks the metadata-api-types
typecheck-metadata-api-types:
cd ${TYPESCRIPT_ROOT} && \
npm run typecheck

${TYPESCRIPT_SRC}: ${SCHEMA_FILE} ${TYPESCRIPT_ROOT}/package.json ${TYPESCRIPT_ROOT}/package-lock.json ${PATCHES}
./scripts/generate-typescript-types.sh "${SCHEMA_FILE}"

${GO_TARGET}: ${TYPESCRIPT_SRC}
./scripts/generate-types-for-lang.sh ${GO_ROOT} ${GO_FILE}

${RUST_TARGET}: ${TYPESCRIPT_SRC}
./scripts/generate-types-for-lang.sh ${RUST_ROOT} ${RUST_FILE}

${HASKELL_TARGET}: ${TYPESCRIPT_SRC}
./scripts/generate-types-for-lang.sh ${HASKELL_ROOT} ${HASKELL_FILE}

${KOTLIN_TARGET}: ${TYPESCRIPT_SRC}
./scripts/generate-types-for-lang.sh ${KOTLIN_ROOT} ${KOTLIN_FILE}
25 changes: 25 additions & 0 deletions metadata-api-types/scripts/generate-types-for-lang.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash
set -euo pipefail

PROJECT_ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." >/dev/null 2>&1 && pwd )" # ... https://stackoverflow.com/a/246128/176841

LANG="$1"
FILE="$2"
# We are generating types from already generated Typescript types.
# `index.js`` is the file that contains the Typescript types
INDEX_JS="${PROJECT_ROOT}/typescript/src/index.ts"

# Dir where generated types will be published.
DIR="${PROJECT_ROOT}/${LANG}"

if [ ! -d "${DIR}" ]; then
mkdir "${DIR}"
fi

# npm package used to generate types.
# Use `quicktype --help` to see a list of supported languages.
# Use `npm i -g quicktype` to install.
quicktype --lang "${LANG}" \
--out "${DIR}/${FILE}" \
--src-lang typescript \
--src "${INDEX_JS}" # add --quiet to silence output

0 comments on commit 69c8e8b

Please sign in to comment.