README
gql-req
✨ Minimal and ultra Lightweight GraphQL client (lighter then graphql-request
⚡️) that supports Node and browsers for scripts or simple apps 🚀.
This is an attempt to revive graphql-request
❤️.
Features
- 🦄 Inspired from
graphql-request
(with almost identical interface). - 🔥 Most simple and lightweight GraphQL client (Lighter then
graphql-request
). - ⚖️ Tiny Bundle: 1.515 kB (gzip)
- 📌 Promise-based API (works with
async
/await
). - ✨ Isomorphic through
Axios
. - 🎯 Supports queries through
POST
andGET
. - 🎉 Typescript support.
Limitation
- 🚧 No front-end integration.
- 🚧 No Cache System.
- 🚧 No Subscription aka WS (WebSocket) support.
Installation
# npm
npm install gql-req
## yarn
yarn add gql-req
API
GraphQLClient
— class for create re-useable client.request
— funcs for plain request.
Options
The Options type adopts the AxiosRequestConfig type but with few additions and changes.
method
— http method used to connect with graphql server ('GET'/'POST', 'POST' as default).headers
— key-value object that definite the headers.pureDataResponse
— Unlike graphql-request, I have found rawRequest method and Request
method are same, for that this option was created. if this option istrue
, the result is equal
toRequest
result (false
as default).
Usage
const { request, GraphQLClient } = require('gql-req');
// Run GraphQL queries/mutations using a static function (plain request)
request(endpoint, { query, variables }).then(data => console.log(data));
// ... or create a GraphQL client instance to send requests (re-useable client)
// ... you can also update the header by use `setHeaders` method
// setHeaders(key: string, value: string)
// setHeaders(headers: Headers) with Headers = { [key:string]: string }
const client = new GraphQLClient(endpoint, { headers: {} });
client.request(query, variables).then(data => console.log(data));
You can play around with it on this sandbox example codesandbox.io/gql-req.
FAQ
gql-req
over graphql-request?
Why Lighter by more then 2 kB, support queries under 'GET' request.
gql-req
, Apollo and Relay?
What's the difference between like graphql-request
, gql-req
is perfect for small scripts or simple apps.
Compared to GraphQL clients like Apollo or Relay, gql-req
doesn't have a built-in cache and has no integrations for frontend frameworks. The goal is to keep the package and API as minimal as possible.
So what about Lokka?
Lokka is great but it still requires a lot of setup code to be able to send a simple GraphQL query. gql-req
does less work compared to Lokka but is a lot simpler to use.