@gql2ts/from-query

generate typescript interfaces from a graphql schema and query

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
1936Mar 25, 2020May 23, 2016Minified + gzip package size for @gql2ts/from-query in KB

Readme

@gql2ts/from-query

This package is used to generate types/interfaces from a GraphQL Schema and a query.

Objective

Pass a GraphQL type schema to generate a TypeScript interface:

Input:

type Query {
  thing: String!
  anotherThing: Boolean!
}

Output:

interface IQuery {
  thing: string;
  anotherThing: boolean;
}

Basic Usage

import fromQuery from '@gql2ts/from-query';
import { schema, query } from 'my-graphql-stuff';
import * as fs from 'fs';

const typescriptDefinitions = fromQuery(schema, query);

const allDefinitions = typescriptDefinitions.map(({ result }) => result).join('\n');

fs.writeFile('definition.d.ts', allDefinitions);

Advanced Usage

import fromQuery from '@gql2ts/from-query';
import { schema, query } from 'my-graphql-stuff';
// see `IOptions` in `types.ts` for options
import * as options from './customFormatters';
import * as fs from 'fs';

const myCustomTypes = {
  Date: 'string',
  Map: 'object',
}

const typescriptDefinitions = fromQuery(schema, query, myCustomTypes, options);

const allDefinitions = typescriptDefinitions.map(({ result }) => result).join('\n');

fs.writeFile('definition.d.ts', allDefinitions);

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.