npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lana-commerce/typescript-codegen

v4.0.0

Published

Typescript code generator for GraphQL

Downloads

10

Readme

Lana Typescript Codegen

A CLI tool for generating typescript code based on graphql fragments/operations. It does not support all GraphQL features, in particular support for subscriptions is missing.

How it works?

The tool works on a target directory. It scans the directory for *.graphql files, then based on fragments/operations defined in those files it generates a bunch of typescript files. The resulting directory structure looks like this (in addition to *.graphql files):

<target directory>
├── fragments
│   └── <fragment-name>.ts
├── operations
│   └── <operation-name>.ts
└── types.ts
  • types.ts - Contains all type definitions, tool doesn't use the .d.ts file, because all type definitions are scoped to that file.
  • fragments/<fragment-name>.ts - Contains a definition for a single graphql fragment as a default exported string. Example:
    export default "fragment MenuShort on StorefrontMenu {\n  id\n  name\n}\n";
  • operations/<operation-name>.ts - Contains a definition for a sngle graphql operation as a default exported string, but with a type overload. The file includes and prepends all used fragments as well. Example:
    import MenuShort from "../fragments/MenuShort";
    import { GetMenuQueryMeta } from "../types";
    export default ((MenuShort +
      "query GetMenu($shopID: String!, $id: String!) {\n  storefrontMenus(shop_id: $shopID, ids: [$id]) {\n    ...MenuShort\n  }\n}") as unknown) as GetMenuQueryMeta;

This structure makes it webpack friendly. You only include what you use, and every fragment body is included exactly once.

The types.ts file mentioned above in addition to fragment type definitions contains meta types for operation definitions of the following form:

export interface GetMenuQuery {
  storefrontMenus: Array<MenuShortFragment> | null;
}

export interface GetMenuQueryVariables {
  id: string;
  shopID: string;
}

export interface GetMenuQueryMeta {
  __opType: GetMenuQuery;
  __opVariablesType: GetMenuQueryVariables;
  __tag: "graphql-operation";
}

The operation itself is type casted to its *Meta type. This allows you to build request handlers which are aware of both: the result of the operation and the variables the operation expects as input.

Usage

  1. Install the tool locally or globally:

    # local installation:
    npm install -D @lana-commerce/typescript-codegen
    
    # global installation:
    npm install -g @lana-commerce/typescript-codegen
  2. Create a directory with some *.graphql files in it.

    mkdir -p src/graphql

    Write down some graphql fragments/queries to src/graphql/all.graphql. It's up to you how you want to organize your graphql files. It could be all.graphql or fragments.graphql/operations.graphql or something else. The tool will load all graphql files.

    fragment MenuShort on StorefrontMenu {
      id
      name
    }
    
    query GetMenu($shopID: String!, $id: String!) {
      storefrontMenus(shop_id: $shopID, ids: [$id]) {
        ...MenuShort
      }
    }
  3. Run the tool! (example assumes global installation and proper PATH configuration)

    lana-commerce-typescript-codegen src/graphql

    You should see something like this:

    [14:06:01.051] loading graphql file "src/graphql/all.graphql"
    [14:06:01.055] fetching schema via introspection query from "https://api.lana.dev/storefront.json"
    [14:06:01.534] writing data to file "src/graphql/types.ts"
    [14:06:01.535] writing data to file "src/graphql/fragments/MenuShort.ts"
    [14:06:01.535] writing data to file "src/graphql/operations/GetMenuQuery.ts"

Include fragments from other packages

To boost data reuse even further, the tool allows you to import fragment definitions from other packages.

TODO: how to include packages from other files?