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

shotor/ts-gql-sdk-gen

Repository files navigation

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('https://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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published