# gql-typescript-generator

> Generate queries from graphql schema, used for writing api test.

Latest version **10.0.3** (published 2020-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install gql-typescript-generator
pnpm add gql-typescript-generator
yarn add gql-typescript-generator
bun add gql-typescript-generator
```

Provides the command `gqlg`.

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 10.0.3 |
| Published | 2020-08-18 |
| First published | 2019-01-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 701.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Lukasz Gandecki lukasz@xolv.io |
| Maintainers | lgandecki |
| Keywords | graphql, query, generator |

## Links

- npm: https://www.npmjs.com/package/gql-typescript-generator
- Repository: https://github.com/TheBrainFamily/gql-typescript-generator
- Homepage: https://github.com/TheBrainFamily/gql-typescript-generator#readme
- Issues: https://github.com/TheBrainFamily/gql-typescript-generator/issues
- npm.io page: https://npm.io/package/gql-typescript-generator

## Dependencies (7)

- [graphql](https://npm.io/package/graphql.md) ^0.13.2
- [shelljs](https://npm.io/package/shelljs.md) ^0.8.4
- [commander](https://npm.io/package/commander.md) ^2.15.1
- [git-state](https://npm.io/package/git-state.md) ^4.1.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.0.12
- [graphql-tag](https://npm.io/package/graphql-tag.md) ^2.10.3
- [find-package-json](https://npm.io/package/find-package-json.md) ^1.2.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 10.0.3 (latest) — 2020-08-18
- 10.0.2 — 2020-08-17
- 10.0.1 — 2020-08-17
- 10.0.0 — 2020-08-17
- 9.0.5 — 2020-07-28
- 9.0.4 — 2020-07-28
- 9.0.3 — 2020-07-17
- 9.0.2 — 2020-07-17
- 9.0.1-preview-3 — 2020-07-17
- 9.0.1-preview — 2020-07-17
- 9.0.0-preview — 2020-07-15
- 8.0.3-preview — 2020-07-02
- 8.0.2-preview — 2020-06-26
- 8.0.1-preview — 2020-06-25
- 8.0.0-preview — 2020-06-24
- … 18 more at https://npm.io/package/gql-typescript-generator/versions

## README

# gql-generator

Generate queries from graphql schema, used for writing api test.

## Example
```gql
# Sample schema
type Query {
  user(id: Int!): User!
}

type User {
  id: Int!
  username: String!
  email: String!
  createdAt: String!
}
```

```gql
# Sample query generated
query user($id: Int!) {
  user(id: $id){
    id
    username
    email
    createdAt
  }
}
```

## Usage
```bash
# Install
npm install gql-generator -g

# see the usage
gqlg --help

# Generate sample queries from schema file
gqlg --schemaFilePath ./example/sampleTypeDef.graphql --destDirPath ./example/output --depthLimit 5
```

Now the queries generated from the [`sampleTypeDef.graphql`](./example/sampleTypeDef.graphql) can be found in the destDir: [`./example/output`](./example/output).

This tool generate 3 folders holding the queries: mutations, queries and subscriptions. And also `index.js` files to export the queries in each folder.

You can require the queries like this:

```js
// require all the queries
const queries = require('./example/output');
// require mutations only
const mutations = require('./example/output/mutations');

// sample content
console.log(queries.mutations.signup);
console.log(mutations.signup);
/*
mutation signup($username: String!, email: String!, password: String!){
  signup(username: $username, email: $email, password: $password){
    token
    user {
      id
      username
      email
      createdAt
    }
  }
}
*/

```

## Usage example

Say you have a graphql schema like this: 

```gql
type Mutation {
  signup(
    email: String!
    username: String!
    password: String!
  ): UserToken!
}

type UserToken {
  token: String!
  user: User!
}

type User {
  id: Int!
  username: String!
  email: String!
  createdAt: String!
}
```

Before this tool, you write graphql api test like this:

```js
const { GraphQLClient } = require('graphql-request');
require('should');

const host = 'http://localhost:8080/graphql';

test('signup', async () => {
  const gql = new GraphQLClient(host);
  const query = `mutation signup($username: String!, email: String!, password: String!){
    signup(username: $username, email: $email, password: $password){
      token
      user {
        id
        username
        email
        createdAt
      }
    }
  }`;

  const data = await gql.request(query, {
    username: 'tim',
    email: 'timqian92@qq.com',
    password: 'samplepass',
  });

  (typeof data.signup.token).should.equal('string');
);
```

As `gqlg` generated the queries for you, you don't need to write the query yourself, so your test will becomes:

```js
const { GraphQLClient } = require('graphql-request');
require('should');
const mutations = require('./example/output/mutations');

const host = 'http://localhost:8080/graphql';

test('signup', async () => {
  const gql = new GraphQLClient(host);

  const data = await gql.request(mutations.signup, {
    username: 'tim',
    email: 'timqian92@qq.com',
    password: 'samplepass',
  });

  (typeof data.signup.token).should.equal('string');
);
```

## Notice

As this tool is used for test, it expends all the fields in a query. And as we know, there might be recursive field in the query. So `gqlg` ignores the types which has been added in the parent queries already.


> [Donate with bitcoin](https://getcryptoo.github.io/)

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