Skip to content

ilearnio/graphql-client

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 97.9%
  • Makefile 2.1%