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 endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const graphQLClient = endpoint headers: authorization: 'Bearer MY_TOKEN' const query = /* GraphQL */ ` { Movie(title: "Inception") { releaseDate actors { name } } } ` const data = await graphQLClient console}
Passing more options to fetch
{ const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const graphQLClient = endpoint credentials: 'include' mode: 'cors' const query = /* GraphQL */ ` { Movie(title: "Inception") { releaseDate actors { name } } } ` const data = await graphQLClient console}
Using variables
{ const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const query = /* GraphQL */ ` query getMovie($title: String!) { Movie(title: $title) { releaseDate actors { name } } } ` const variables = title: 'Inception' const data = await console}
Error handling
{ const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const query = /* GraphQL */ ` { Movie(title: "Inception") { releaseDate actors { fullname # "Cannot query field 'fullname' on type 'Actor'. Did you mean 'name'?" } } } ` try const data = await console catch error console process }
require
instead of import
Using const request = { const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const query = /* GraphQL */ ` { Movie(title: "Inception") { releaseDate actors { name } } } ` const data = await console}
node
Cookie support for npm install fetch-cookie
{ const endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const graphQLClient = endpoint headers: authorization: 'Bearer MY_TOKEN' const query = /* GraphQL */ ` { Movie(title: "Inception") { releaseDate actors { name } } } ` const data = await graphQLClient console}
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 endpoint = 'https://api.graph.cool/simple/v1/cixos23120m0n0173veiiwrjr' const query = /* GraphQL */ ` { Movie(title: "Inception") { releaseDate actors { name } } } ` const data errors extensions headers status = await console}
More examples coming soon...
- Fragments
- Using
graphql-tag
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!