@gql2ts/from-query

v2.0.0-4
generate typescript interfaces from a graphql schema and query
gql2ts graphql typescript

@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);
npm i @gql2ts/from-query

Metadata

  • ISC
  • Whatever
  • Brett Jurgens
  • released 3/13/2020

Downloads