Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Latest commit

 

History

History
82 lines (58 loc) · 1.34 KB

README.md

File metadata and controls

82 lines (58 loc) · 1.34 KB

ts-gql-sdk-gen

Generate a complete GraphQL SDK from schema, generating all queries/mutations/subscriptions along the way.

Basically hacks the following two great packages together:

Usage

Install

yarn global add @shotor/tsgqlsdk

# or install locally
yarn add -D @shotor/tsgqlsdk

Run

Given schema file

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

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

Run the program

tsgqlsdk /path/to/schema.graphql /path/to/destination

In the destination folder you will find the following folder structure

- /queries
- /mutations
- /subscriptions
- gql-sdk.ts

Make sure graphql-request is installed in your project.

yarn add graphql-request

Then import and use the gql-sdk.ts and use fully typed API:

import { GraphQLClient } from 'graphql-request'
import { getSdk } from '/path/to/destination/gql-sdk.ts'

const sdk = getSdk(new GraphQLClient('http://example.org/graphql'))

desribe('test user', () => {
  it('gets user', async () => {
    const ID = 300
    const result = await sdk.user({
      id: ID,
    })

    expect(result.id).toBe(ID)
  })
})

License

MIT