Skip to content

Commit 28c6347

Browse files
halostatuef
authored andcommitted
Early resolution of request function (#38)
* Allow late resolution of header values - I have an authorization token that may not be set when I create the Vue instance of graphql.js. This allows the provision of a function for late resolution of a header value. * Early resolution of request function - Deferring the resolution of the request function seems unlikely to help
1 parent 1a56a34 commit 28c6347

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ var graph = graphql("http://localhost:3000/graphql", {
121121
headers: {
122122
// headers
123123
"Access-Token": "some-access-token"
124+
// OR "Access-Token": () => "some-access-token"
124125
},
125126
fragments: {
126127
// fragments, you don't need to say `fragment name`.

graphql.js

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,14 @@
3131
})
3232
}
3333

34-
function __request(debug, method, url, headers, data, asJson, onRequestError, callback) {
35-
if (!url) {
36-
return;
37-
}
38-
if (asJson) {
39-
var body = JSON.stringify({query: data.query, variables: data.variables});
40-
} else {
41-
var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables))
42-
}
43-
if (debug) {
44-
console.groupCollapsed('[graphql]: '
45-
+ method.toUpperCase() + ' ' + url + ': '
46-
+ data.query.split(/\n/)[0].substr(0, 50) + '... with '
47-
+ JSON.stringify(data.variables).substr(0, 50) + '...')
48-
console.log('QUERY: %c%s', 'font-weight: bold', data.query)
49-
console.log('VARIABLES: %c%s\n\nsending as ' + (asJson ? 'json' : 'form url-data'), 'font-weight: bold', JSON.stringify(data.variables, null, 2), data.variables)
50-
console.groupEnd()
51-
}
52-
if (typeof XMLHttpRequest != 'undefined') {
34+
if (typeof XMLHttpRequest !== 'undefined') {
35+
function __doRequest(
36+
method, url, contentType, accept, headers, body, _onRequestError, callback
37+
) {
5338
var xhr = new XMLHttpRequest
5439
xhr.open(method, url, true)
55-
xhr.setRequestHeader('Content-Type', (asJson ? 'application/json' : 'application/x-www-form-urlencoded'))
56-
xhr.setRequestHeader('Accept', 'application/json')
40+
xhr.setRequestHeader('Content-Type', contentType)
41+
xhr.setRequestHeader('Accept', accept)
5742
for (var key in headers) { xhr.setRequestHeader(key, headers[key]) }
5843
xhr.onerror = function () { callback(xhr, xhr.status) }
5944
xhr.onload = function () {
@@ -65,18 +50,19 @@
6550
}
6651
}
6752
xhr.send(body)
68-
} else if (typeof require == 'function') {
69-
var http = require('http'), https = require('https'), URL = require('url'), uri = URL.parse(url);
53+
}
54+
} else if (typeof require !== 'function') {
55+
function __doRequest(
56+
method, url, contentType, accept, headers, body, onRequestError, callback
57+
) {
58+
var http = require('http'), https = require('https'), URL = require('url'), uri = URL.parse(url)
7059
var req = (uri.protocol === 'https:' ? https : http).request({
7160
protocol: uri.protocol,
7261
hostname: uri.hostname,
7362
port: uri.port,
7463
path: uri.path,
75-
method: "POST",
76-
headers: __extend({
77-
'Content-type': (asJson ? 'application/json' : 'application/x-www-form-urlencoded'),
78-
'Accept': 'application/json'
79-
}, headers)
64+
method: method.toUpperCase(),
65+
headers: __extend({ 'Content-type': contentType, 'Accept': accept }, headers)
8066
}, function (response) {
8167
var str = ''
8268
response.setEncoding('utf8')
@@ -95,6 +81,43 @@
9581
}
9682
}
9783

84+
function __request(debug, method, url, headers, data, asJson, onRequestError, callback) {
85+
if (!url) {
86+
return;
87+
}
88+
if (asJson) {
89+
var body = JSON.stringify({query: data.query, variables: data.variables});
90+
} else {
91+
var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables))
92+
}
93+
if (debug) {
94+
console.groupCollapsed('[graphql]: '
95+
+ method.toUpperCase() + ' ' + url + ': '
96+
+ data.query.split(/\n/)[0].substr(0, 50) + '... with '
97+
+ JSON.stringify(data.variables).substr(0, 50) + '...')
98+
console.log('QUERY: %c%s', 'font-weight: bold', data.query)
99+
console.log('VARIABLES: %c%s\n\nsending as ' + (asJson ? 'json' : 'form url-data'), 'font-weight: bold', JSON.stringify(data.variables, null, 2), data.variables)
100+
console.groupEnd()
101+
}
102+
103+
for (var key in headers) {
104+
if (typeof headers[key] === 'function') {
105+
headers[key] = headers[key]()
106+
}
107+
}
108+
109+
__doRequest(
110+
method,
111+
url,
112+
asJson ? 'application/json' : 'application/x-www-form-urlencoded',
113+
'application/json',
114+
headers,
115+
body,
116+
onRequestError,
117+
callback
118+
)
119+
}
120+
98121
function __isTagCall(strings) {
99122
return Object.prototype.toString.call(strings) == '[object Array]' && strings.raw
100123
}

0 commit comments

Comments
 (0)