genql

Type safe TypeScript client for any GraphQL API

README

genql


Read the quick start guide to generate your client and start writing queries.

You can stay up to date with the latest changes subscribing to the Genql changelog.

Features

-   ✅ Type completion & Type validation
-   🍃 No dependencies (not even graphql)
-   🚂 Works in browser, Node, Deno, Cloudflare workers, Bun and more

Example


First generate your client with the genql cli.

You can find other cli options here


  1. ```sh
  2. npm i -D @genql/cli # cli to generate the client code
  3. genql --schema ./schema.graphql --output ./generated
  4. ```

Then you can use your client as follow

  1. ```js
  2. import { createClient, everything } from './generated'
  3. const client = createClient()

  4. client
  5.     .query({
  6.         countries: {
  7.             // pass arguments to the query
  8.             __args: {
  9.                 filter: {
  10.                     currency: {
  11.                         eq: 'EUR',
  12.                     },
  13.                 },
  14.             },
  15.             name: true,
  16.             code: true,
  17.             nestedField: {
  18.                 // fetch all scalar fields
  19.                 __scalar: true,
  20.             },
  21.         },
  22.     })
  23.     .then(console.log)
  24. ```

The code above will fetch the graphql query below

  1. ```graphql
  2. query {
  3.     countries(filter: { currency: { eq: "EUR" } }) {
  4.         name
  5.         code
  6.         nestedField {
  7.             scalarField1
  8.             scalarField2
  9.         }
  10.     }
  11. }
  12. ```

Why


Genql has a lot of benefits over writing graphql queries by hand:

-   Writing queries is faster thanks to TypeScript auto completion
-   You can safely update your schema and be sure your queries are still valid
-   You can fetch all scalar fields in a type with __scalar: true
-   No graphql package dependency, no runtime parsing of queries
-   You have to generate the client only after your schema changes, not after every query change


Sponsors


[Notaku](https://notaku.so)