graphql-request
Minimal GraphQL client supporting Node and browsers for scripts or simple apps
Highlights
- Most simple & lightweight GraphQL client
- Promise-based API (works with
async
/await
) - ESM native package (CJS build is included for now as well, but will eventually be removed)
- First class TypeScript support
- Including
TypedDocumentNode
- Including
- Isomorphic (works in both Nodejs and Browsers)
Install
npm add graphql-request graphql
Quick Start
Send a GraphQL document using a static request function:
import { request, gql } from 'graphql-request'
const document = gql`
{
company {
ceo
}
}
`
await request('https://api.spacex.land/graphql/', document)
The function can be passed a configuration object for more complex cases:
await request({
url,
document,
variables,
requestHeaders,
})
A class is available for constructing your own instances:
import { GraphQLClient } from 'graphql-request'
const document = gql`
{
company {
ceo
}
}
`
const client = new GraphQLClient(endpoint)
await request('https://api.spacex.land/graphql/', document)
Examples
- Request:
- GraphQL:
- Configuration:
- Community
- TypeScript
- Other:
Node Version Support
We only (officially) support versions of Nodejs of the following status:
- Current
- LTS
- Maintenance and end of life not yet reached
So for example on May 1 2023 that would mean these versions: 16, 18, 19.
Any issue that exists solely for an unsupported version of Nodejs will be rejected (not worked on).
Reference
Configuration
ErrorPolicy
By default GraphQLClient will throw when an error is received. However, sometimes you still want to resolve the (partial) data you received.
You can define errorPolicy
in the GraphQLClient
constructor.
const client = new GraphQLClient(endpoint, { errorPolicy: 'all' })
None (default)
Allow no errors at all. If you receive a GraphQL error the client will throw.
Ignore
Ignore incoming errors and resolve like no errors occurred
All
Return both the errors and data, only works with rawRequest
.
Knowledge Base
Why was the file upload feature taken away? Will it return?
In this issue we decided to make this library more stable and maintainable. In principal the feature is still in scope of this library and will make a return when we find time to do the feature right.
graphql
?
Why do I have to install graphql-request
uses methods exposed by the graphql
package to handle some internal logic. On top of that, for TypeScript users, some types are used from the graphql
package to provide better typings.
gql
template exported by graphql-request
?
Do I need to wrap my GraphQL documents inside the No. It is there for convenience so that you can get the tooling support like prettier formatting and IDE syntax highlighting. You can use gql
from graphql-tag
if you need it for some reason too.
graphql-request
apart from other clients like Apollo, Relay, etc.?
What sets 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.