Genql translate typed code into graphql queries, enabling you to get auto completion and validation for your graphql queries.
To use genql you first need to generate a sdk client
First install the required package from npm
1npm init -y2npm i -D @genql/cli # cli to generate the client code3npm i @genql/runtime graphql # runtime dependencies
@genql/cli
is a dev dependency because it is only required to generate the client, @genql/runtime
instead is a direct dependency of the generated code
Then run the genql command to generate the client inside a directory
1genql --schema ./schema.graphql --output ./generated2# or using a graphql api url3genql --endpoint https://countries.trevorblades.com --output ./generated -H 'Authorization: Bearer myToken'
The generated files expose a function createClient
, this creates a client you can use to send requests
1// example client generated with genql2const { createClient } = require('./generated')34const client = createClient({5url: 'https://countries.trevorblades.com',6headers: {7'Some-Header': 'hello',8},9})10client11.query({12countries: {13name: true,14code: true,15},16})17.then(console.log)
Read more about the graphql client methods in the usage section