Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,20 @@ npm install graphql-client -S

## How To
```javascript
var client = require('graphql-client')({url: 'http://your-host/graphql'})
var client = require('graphql-client')({
url: 'http://your-host/graphql',

// before request hook
onRequest (req) {
// Do whatever you want with `req`, e.g. add JWT auth header
req.headers.set('Authentication', 'Bearer ' + token)
},

// response hook
onResponse (res) {
...
}
})

var query = `
query search ($query: String, $from: Int, $limit: Int) {
Expand Down
11 changes: 8 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function highlightQuery (query, errors) {

query.split('\n').forEach(function (row, index) {
var line = index + 1
var lineErrors = locations.filter(function (loc) { return loc.line === line })
var lineErrors = locations.filter(function (loc) { return loc && loc.line === line })

queryHighlight += row + '\n'

Expand Down Expand Up @@ -40,15 +40,20 @@ module.exports = function (params) {
var headers = new Headers()
headers.append('Content-Type', 'application/json')

return fetch(params.url, {
var req = {
method: 'POST',
body: JSON.stringify({
query: query,
variables: variables
}),
headers: headers,
credentials: params.credentials
}).then(function (res) {
}

if (params.onRequest) params.onRequest(req)

return fetch(params.url, req).then(function (res) {
if (params.onResponse) params.onResponse(res)
return res.json()
}).then(function (data) {
if (data.errors && data.errors.length) {
Expand Down