Git Product home page Git Product logo

ts-alias-transformer's Introduction

ts-alias-transformer

TypeScript AST transformer to resolve type aliases into fully formed interfaces

Blog post with more context.

Usage

  • npm install -g ts-alias-transformer
  • ts-alias-transformer -m [path to model root] -o [generated output]

Problem I'm trying to solve

I am using GraphQL at work and would like to add some type-safety around resolvers. Type safety in GQL is a bit weird to conceptualize, because resolvers are pretty dynamic by nature. Luckily I came across graphqlgen, which solved a lot of these problems I was having with its concept of models.

At work we run gRPC microservices, and GQL mostly serves as a nice fanout layer for our UI consumers. We already publish TypeScript interfaces that match our published proto contracts. I wanted to consume these types in graphqlgen, but ran into some issues due to type export support and with the way our TypeScript interfaces published (heavily namespaced, lots of references). Additionally, because graphqlgen is using babel-parser to do it's introspection and generation, it's quite limited as far as working with imported types (leads to parsing hell).

This repo is a mixture of me solving my narrow problem for work (and hopefully by extension graphqlgen), as well as demonstrating some of the power of the TypeScript compiler API. Utilizing the Checker API and a custom transformer, I was able to solve my problem without too much hassle.

Problem Visualized

// my_protos in node_modules
export namespace protos {
  export namespace user {
    export interface User {
      username: string;
      info: UserInfo;
    }
    
    export interface UserInfo {
      firstName: string;
      lastName: string;
    }
  }
  
  export namespace todo {
    export interface Todo {
      createdBy: protos.user.User;
      text: string;
    }
  }
}


// src/models.ts <-- models for graphqlgen (models returned from Query resolvers)
import { protos } from 'my_protos';

export type User = protos.user.User;
export type Todo = protos.todo.Todo;

// Run my program here pointing at src/models file

// generated/output.ts
export interface User {
  username: string;
  info: {
    firstName: string;
    lastName: string;
  }
}

export interface Todo {
  createdBy: {
    username: string;
    info: {
      firstName: string;
      lastName: string;
    }
  },
  text: string;
}

ts-alias-transformer's People

Contributors

ksaldana1 avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.