Skip to content

nordsimon/graphql-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 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

Initialize the client

var client = require('graphql-client')({
  url: 'http://your-host/graphql',
  headers: {
    Authorization: 'Bearer ' + token
  }
})

Use the promise API

WARNING: Make sure the Promise API is polyfilled for older browsers, you can use es6-promise

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

client.query(`
query search ($query: String, $from: Int, $limit: Int) {
  search(query: $query, from: $from, limit: $limit) {
    took,
    totalHits,
    hits {
      name
    }
  }
}`, variables, function(req, res) {
  if(res.status === 401) {
    throw new Error('Not authorized')
  }
})
.then(function(body) {
  console.log(body)
})
.catch(function(err) {
  console.log(err.message)
})

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published