Skip to content
/ gql Public

A GraphQL client for Node.js (>=18) and the browser.

Notifications You must be signed in to change notification settings

coatl-labs/gql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@coatl/gql

A GraphQL client for Node.js and the browser.

Requirements

  • Node.js 18.0.0 or later (for Fetch API support)
  • Any browser with Fetch API support

Installation

# using npm
npm install @coatl/gql

# using yarn
yarn add @coatl/gql

# using pnpm
pnpm add @coatl/gql

Features

  • Queries and mutations
  • Variable interpolation
  • Full type safety
  • Fragments
  • Subscriptions
  • File uploads (multipart/form-data)
  • Directives
  • Custom scalars

Usage

Implementing a client

import { GQLClient } from '@coatl/gql'

export const client = new GQLClient('https://api.example.com/graphql', {
  headers: {
    Authorization: 'Bearer <token>'
  }
})

Queries

import { client } from './client'

interface User {
  id: string
  name: string
  email: string
}

const users = await client.query<User[]>(`
  users {
    id
    name
    email
  }
`)

console.log(users) // [...]

Mutations

import { client } from './client'

interface User {
  id: string
  name: string
  email: string
}

interface CreateUserInput {
  name: string
  email: string
}

const userExist = await client.query<User>(`
  # check if the user already exists
  user(email: $email) {
    id
  }
`)

if (userExist) {
  throw new Error('User already exists')
}

const variables = {
  name: 'John Doe',
  email: '[email protected]'
}

const user = await client.mutation<User, CreateUserInput>(
  `
  createUser(input: $input) {
    id
    name
    email
  }
`,
  variables
)