Name: graphql-request
Owner: Prisma
Description: ? Minimal GraphQL client supporting Node and browsers for scripts or simple apps
Created: 2017-05-10 20:05:07.0
Updated: 2018-05-24 15:06:12.0
Pushed: 2018-05-18 21:52:14.0
Size: 251
Language: TypeScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
? Minimal GraphQL client supporting Node and browsers for scripts or simple apps
async
/ await
)install graphql-request
Send a GraphQL query with a single line of code. ?? Try it out.
rt { request } from 'graphql-request'
t query = `{
vie(title: "Inception") {
releaseDate
actors {
name
}
est('https://api.graph.cool/simple/v1/movies', query).then(data => console.log(data))
rt { request, GraphQLClient } from 'graphql-request'
un GraphQL queries/mutations using a static function
est(endpoint, query, variables).then(data => console.log(data))
.. or create a GraphQL client instance to send requests
t client = new GraphQLClient(endpoint, { headers: {} })
nt.request(query, variables).then(data => console.log(data))
rt { GraphQLClient } from 'graphql-request'
t client = new GraphQLClient('my-endpoint', {
aders: {
Authorization: 'Bearer my-jwt-token',
t query = `{
vie(title: "Inception") {
releaseDate
actors {
name
}
nt.request(query).then(data => console.log(data))
rt { GraphQLClient } from 'graphql-request'
t client = new GraphQLClient('my-endpoint', {
dentials: 'include',
e: 'cors'
t query = `{
vie(title: "Inception") {
releaseDate
actors {
name
}
nt.request(query).then(data => console.log(data))
rt { request } from 'graphql-request'
t query = `query getMovie($title: String!) {
vie(title: $title) {
releaseDate
actors {
name
}
t variables = {
tle: 'Inception',
est('my-endpoint', query, variables).then(data => console.log(data))
rt { request } from 'graphql-request'
t wrongQuery = `{
me random stuff
est('my-endpoint', query)
hen(data => console.log(data))
atch(err => {
console.log(err.response.errors) // GraphQL response errors
console.log(err.response.data) // Response data if available
require
instead of import
t { request } = require('graphql-request')
t query = `{
vie(title: "Inception") {
releaseDate
actors {
name
}
est('my-endpoint', query).then(data => console.log(data))
node
install fetch-cookie/node-fetch
s
rt { GraphQLClient } from 'graphql-request'
se this instead for cookie support
al['fetch'] = require('fetch-cookie/node-fetch')(require('node-fetch'))
t client = new GraphQLClient('my-endpoint')
t query = `{
vie(title: "Inception") {
releaseDate
actors {
name
}
nt.request(query).then(data => console.log(data))
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:
rt { rawRequest } from 'graphql-request'
t query = `{
vie(title: "Inception") {
releaseDate
actors {
name
}
equest('my-endpoint', query).then(({data, extensions}) => console.log(data, extensions))
graphql-tag
graphql-request
, Apollo and Relay?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.
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!