graphql-request
graphql-request
📡 Minimal GraphQL client supporting Node and browsers for scripts or simple apps
Features
- Most simple and lightweight GraphQL client
- Promise-based API (works with
async
/await
) - Typescript support (Flow coming soon)
Install
npm install graphql-request
Quickstart
Send a GraphQL query with a single line of code. ▶️ Try it out.
const query = `{ Movie(title: "Inception") { releaseDate actors { name } }}`
Usage
// Run GraphQL queries/mutations using a static function // ... or create a GraphQL client instance to send requestsconst client = endpoint headers: {} client
Examples
Authentication via HTTP header
const client = 'my-endpoint' headers: Authorization: 'Bearer my-jwt-token' const query = `{ Movie(title: "Inception") { releaseDate actors { name } }}` client
Passing more options to fetch
const client = 'my-endpoint' credentials: 'include' mode: 'cors' const query = `{ Movie(title: "Inception") { releaseDate actors { name } }}` client
Using variables
const query = `query getMovie($title: String!) { Movie(title: $title) { releaseDate actors { name } }}` const variables = title: 'Inception'
Error handling
const wrongQuery = `{ some random stuff}`
require
instead of import
Using const request = const query = `{ Movie(title: "Inception") { releaseDate actors { name } }}`
node
Cookie support for npm install fetch-cookie/node-fetch
// use this instead for cookie supportglobal'fetch' = const client = 'my-endpoint' const query = `{ Movie(title: "Inception") { releaseDate actors { name } }}` client
Receiving a raw response
The request
method will return the data
or errors
key from the response.
If you need to access the extensions
key you can use the rawRequest
method:
const query = `{ Movie(title: "Inception") { releaseDate actors { name } }}`
More examples coming soon...
- Fragments
- Using
graphql-tag
- Typed Typescript return values
FAQ
graphql-request
, Apollo and Relay?
What's the difference between graphql-request
is the most minimal and simplest to use GraphQL client. It's perfect for small scripts or simple apps.
Compared to GraphQL clients like Apollo or Relay, graphql-request
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. graphql-request
does less work compared to Lokka but is a lot simpler to use.
Join our Slack community if you run into issues or have questions. We love talking to you!