Skip to content
 
 

Latest commit

 

History

30 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple GraphQL Client

Small http client based on the fetch api. Uses isomorphic-fetch for wider support

If query fails, errors are thrown with messages and query highlight for easy debug

Install

npm install graphql-client -S

How To

var client = require('graphql-client')({
  url: 'http://your-host/graphql'
})
  // Before request hook
  .on('request', (req) => {
    // Do whatever you want with `Request` instance, e.g. add JWT auth header
    if (authenticated) {
      req.headers.set('Authentication', 'Bearer ' + token)
    }
  })
  // On response hook. Access `Response` instance before parsing it's body
  .on('response', (res) => {
    ...
  })
  // After response is parsed as JSON
  .on('data', (data) => {
    console.log('GraphQL response:', data)
  })


var query = `
  query search ($query: String, $from: Int, $limit: Int) {
  search(query: $query, from: $from, limit: $limit) {
    took,
    totalHits,
    hits {
      name
    }
  }
}`

var variables = {
  query: "Search Query",
  limit: 100,
  from: 200
}

client.query(query, variables).then(function(body) {
  console.log(body)
})
.catch(function(err) {
  console.log(err.message)
})

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages