graphql-client
Advanced tools
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) | ||
}) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
3444
59
58
1
Updatedisomorphic-fetch@2.2.1