prismagraphql/graphql-request

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

Homepage:

Size: 251

Language: TypeScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

graphql-request

CircleCI npm version

? Minimal GraphQL client supporting Node and browsers for scripts or simple apps

Features
Install
install graphql-request
Quickstart

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))
Usage
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))
Examples
Authentication via HTTP header
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))
Passing more options to fetch
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))
Using variables
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))
Error handling
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

Using 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))
Cookie support for 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))
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:

rt { rawRequest } from 'graphql-request'

t query = `{
vie(title: "Inception") {
releaseDate
actors {
  name
}



equest('my-endpoint', query).then(({data, extensions}) => console.log(data, extensions))
More examples coming soon…
FAQ
What's the difference between 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.

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.

Help & Community Slack Status

Join our Slack community if you run into issues or have questions. We love talking to you!

Prisma


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.