Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

graphql-client

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-client - npm Package Compare versions

Comparing version 1.0.4 to 1.1.0

33

index.js

@@ -34,2 +34,10 @@ function highlightQuery (query, errors) {

function GraphqlError(query, errors) {
var e = new Error(errors.map(function (e) { return e.message }).join('\n') + '\n' + highlightQuery(query, errors))
e.originalErrors = errors
return e
}
module.exports = function (params) {

@@ -39,8 +47,9 @@ require('isomorphic-fetch')

var headers = new Headers(params.headers)
headers.append('Content-Type', 'application/json')
return {
query: function (query, variables) {
var headers = new Headers()
headers.append('Content-Type', 'application/json')
query: function (query, variables, onResponse) {
return fetch(params.url, {
var req = new Request(params.url, {
method: 'POST',

@@ -53,9 +62,15 @@ body: JSON.stringify({

credentials: params.credentials
}).then(function (res) {
})
return fetch(req)
.then(function (res) {
onResponse && onResponse(req, res)
return res.json()
}).then(function (data) {
if (data.errors && data.errors.length) {
throw new Error(data.errors.map(function (e) { return e.message }).join('\n') + '\n' + highlightQuery(query, data.errors))
}).then(function (body) {
if (body.errors && body.errors.length) {
throw new GraphqlError(query, body.errors)
}
return data
return body
})

@@ -62,0 +77,0 @@ }

{
"name": "graphql-client",
"version": "1.0.4",
"version": "1.1.0",
"description": "",

@@ -17,4 +17,4 @@ "main": "index.js",

"dependencies": {
"isomorphic-fetch": "^2.2.1"
"isomorphic-fetch": "2.2.1"
}
}

@@ -12,28 +12,47 @@ # Simple GraphQL Client

## How To
Initialize the client
```javascript
var client = require('graphql-client')({url: 'http://your-host/graphql'})
var client = require('graphql-client')({
url: 'http://your-host/graphql',
headers: {
Authentication: 'Bearer ' + token
}
})
```
var query = `
Use the promise API
WARNING: Make sure the Promise API is polyfilled for older browsers, you can use [es6-promise](https://github.com/jakearchibald/es6-promise)
```javascript
var query = `
query search ($query: String, $from: Int, $limit: Int) {
search(query: $query, from: $from, limit: $limit) {
took,
totalHits,
hits {
name
search(query: $query, from: $from, limit: $limit) {
took,
totalHits,
hits {
name
}
}
}`
var variables = {
query: "Search Query",
limit: 100,
from: 200
}
}`
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)
})
```
client.query(query, variables, function optionalCallback(req, res) {
if(res.headers.get('x-powered-by') === 'Express') {
throw new Error("Don't want conten served from express")
}
})
.then(function(body) {
console.log(body)
})
.catch(function(err) {
console.log(err.message)
})
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc