0.0.27 • Published 4 years ago

graphql-to-ts v0.0.27

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

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

npm install graphql-to-ts

For CLI tools

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.

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.

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}"
    }
  ]
};
0.0.27

4 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago