@gql2ts/from-schema
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-4 • Public • Published

@gql2ts/from-schema

This package is used to generate a namespace for a GraphQL Schema with all possible interfaces/types included.

Objective

Pass a GraphQL type schema and query to generate a TypeScript interface:

Input:

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

Output:

interface GetThing {
  thing: string;
}

Basic Usage

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

const myNamespace = generateNamespace('MyGraphQL', schema);
fs.writeFile('mySchema.d.ts', myNamespace);

Advanced Usage

import { generateNamespace, ISchemaToInterfaceOptions } from '@gql2ts/from-schema';
import { schema } from 'my-graphql-stuff';
import { IFromQueryOptions } from '@gql2ts/types';
import * as fs from 'fs';

const options: Partial<ISchemaToInterfaceOptions> = {
  ignoredTypes: ['BadGraphType']
};

const overrides: Partial<IFromQueryOptions> = {
  generateInterfaceName: name => `IGraphQL${name}`
};

const myNamespace = generateNamespace('MyGraphQL', schema, options, overrides);
fs.writeFile('mySchema.d.ts', myNamespace);

Package Sidebar

Install

npm i @gql2ts/from-schema

Weekly Downloads

1,325

Version

2.0.0-4

License

ISC

Unpacked Size

22.7 kB

Total Files

5

Last publish

Collaborators

  • brandon.herman
  • brettjurgens