# graphql-to-ts

> This module is intended for people writing code generation tools and is not meant to be used directly. If you're looking to quickly convert GraphQL to TypeScript, use the companion project graphql-to-ts-cli.

Latest version **0.0.27** (published 2020-05-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install graphql-to-ts
pnpm add graphql-to-ts
yarn add graphql-to-ts
bun add graphql-to-ts
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.27 |
| Published | 2020-05-02 |
| First published | 2018-08-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 71.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jeswin Kumar |
| Maintainers | jeswin |

## Links

- npm: https://www.npmjs.com/package/graphql-to-ts
- npm.io page: https://npm.io/package/graphql-to-ts

## Dependencies (7)

- [graphql](https://npm.io/package/graphql.md) ^0.13.2
- [prettier](https://npm.io/package/prettier.md) ^1.14.3
- [@types/node](https://npm.io/package/@types/node.md) ^10.7.1
- [change-case](https://npm.io/package/change-case.md) ^3.0.2
- [graphql-tag](https://npm.io/package/graphql-tag.md) ^2.9.2
- [@types/prettier](https://npm.io/package/@types/prettier.md) ^1.13.2
- [@types/change-case](https://npm.io/package/@types/change-case.md) ^2.3.1

## Recent versions

- 0.0.27 (latest) — 2020-05-02
- 0.0.26 — 2018-09-27
- 0.0.25 — 2018-09-27
- 0.0.24 — 2018-09-27
- 0.0.23 — 2018-09-26
- 0.0.22 — 2018-09-26
- 0.0.21 — 2018-09-26
- 0.0.20 — 2018-09-26
- 0.0.19 — 2018-09-24
- 0.0.18 — 2018-09-23
- 0.0.15 — 2018-09-20
- 0.0.14 — 2018-09-20
- 0.0.13 — 2018-09-19
- 0.0.12 — 2018-09-05
- 0.0.10 — 2018-09-05
- … 9 more at https://npm.io/package/graphql-to-ts/versions

## README

# graphql-to-ts

This module is intended for people writing code generation tools and is not meant to be used directly. If you're looking to quickly convert GraphQL to TypeScript, use the companion project graphql-to-ts-cli.

## Installation

```bash
npm install graphql-to-ts
```

For CLI tools

```bash
npm install graphql-to-ts
```

## Limitations

Fragments and polymorphism are not supported yet. Pull requests welcome.

## API

The best way to learn the API will be by looking at the tests. They are sufficiently easy to follow.

### getTypes()

Converts various GraphQL types (such as types, enums, input types) to TypeScript interfaces.

```js
const schema = `
  type PermissionDTO {
    resource: String!
    allow: Boolean!
  }

  type ScuttlespaceUser {
    about: String
    domain: String
    enabled: Boolean!
    externalId: String!
    pub: String!
    rowid: ID!
    username: String!
    permissions: [PermissionDTO]
  }
`;

const result = getTypes(schema));

// Returns the following.

result = {
  interfaces: [
    {
      name: "IPermissionDTO",
      graphqlType: "PermissionDTO",
      extension: false,
      fields: [
        {
          name: "resource",
          type: { kind: "Scalar", type: "string", nullable: false }
        },
        {
          name: "allow",
          type: { kind: "Scalar", type: "boolean", nullable: false }
        }
      ],
      index: 0,
      gql: "type PermissionDTO {\n  resource: String!\n  allow: Boolean!\n}"
    },
    {
      name: "IScuttlespaceUser",
      graphqlType: "ScuttlespaceUser",
      extension: false,
      fields: [
        {
          name: "about",
          type: { kind: "Scalar", type: "string", nullable: true }
        },
        {
          name: "domain",
          type: { kind: "Scalar", type: "string", nullable: true }
        },
        {
          name: "enabled",
          type: { kind: "Scalar", type: "boolean", nullable: false }
        },
        {
          name: "externalId",
          type: { kind: "Scalar", type: "string", nullable: false }
        },
        {
          name: "pub",
          type: { kind: "Scalar", type: "string", nullable: false }
        },
        {
          name: "rowid",
          type: { kind: "Scalar", type: "string", nullable: false }
        },
        {
          name: "username",
          type: { kind: "Scalar", type: "string", nullable: false }
        },
        {
          name: "permissions",
          type: {
            kind: "List",
            type: { kind: "Scalar", type: "IPermissionDTO", nullable: true },
            nullable: true
          }
        }
      ],
      index: 1,
      gql:
        "type ScuttlespaceUser {\n  about: String\n  domain: String\n  enabled: Boolean!\n  externalId: String!\n  pub: String!\n  rowid: ID!\n  username: String!\n  permissions: [PermissionDTO]\n}"
    }
  ],
  enums: []
};
```

## getQueries()

Converts GraphQL queries into TypeScript interfaces.

```js
const schema = `--see previous--`;
const queries = `
  mutation AddUser($nameArg: String, $locationArg: String!) {
    addUser(name: $nameArg, location: $locationArg)
  }
`;

const result = lib.getQueries(queries, schema);

// Returns the following.

result = {
  queries: [],
  mutations: [
    {
      index: 0,
      name: "AddUser",
      selections: [
        {
          name: "addUser",
          arguments: [
            { name: "name", value: "nameArg" },
            { name: "location", value: "locationArg" }
          ],
          type: {
            kind: "Scalar",
            nullable: false,
            type: "boolean"
          }
        }
      ],
      variables: [
        {
          name: "nameArg",
          type: { kind: "Scalar", type: "string", nullable: true }
        },
        {
          name: "locationArg",
          type: { kind: "Scalar", type: "string", nullable: false }
        }
      ],
      gql:
        "mutation AddUser($nameArg: String, $locationArg: String!) {\n  addUser(name: $nameArg, location: $locationArg)\n}"
    }
  ]
};
```

---
_Source: https://npm.io/package/graphql-to-ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
