Skip to content

Commit 2a66d7a

Browse files
committed
add asJSON option
1 parent a40049e commit 2a66d7a

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ var graph = graphql("/graphql", {
307307
info: `on User { id, name, surname, avatar }`
308308
}
309309
}
310-
})
310+
})
311311
```
312312

313313
Accessing them is also intuitive:
@@ -414,7 +414,7 @@ graph.fragment({
414414
}`,
415415
admin: `on AdminUser {
416416
username,
417-
administrationLevel
417+
administrationLevel
418418
}`
419419
}
420420
})
@@ -446,7 +446,7 @@ fragment username_user on User {
446446

447447
fragment username_admin on AdminUser {
448448
username,
449-
administrationLevel
449+
administrationLevel
450450
}
451451
```
452452

@@ -501,6 +501,15 @@ methods: {
501501
}
502502
```
503503

504+
## Change POST Method
505+
506+
As default, GraphQL.js makes a POST request. But you can change the behavior by setting `asJSON`.
507+
508+
```js
509+
var graph = graphql("http://localhost:3000/graphql", {
510+
asJSON: true
511+
});
512+
```
504513

505514
## Todo App Example
506515

graphql.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@
3131
})
3232
}
3333

34-
function __request(method, url, headers, data, callback) {
35-
var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables))
34+
function __request(method, url, headers, data, asJson, callback) {
35+
if (asJson) {
36+
var body = JSON.stringify({query: data.query, variables: data.variables});
37+
} else {
38+
var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables))
39+
}
3640
if (typeof XMLHttpRequest != 'undefined') {
3741
var xhr = new XMLHttpRequest
3842
xhr.open(method, url, true)
39-
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
43+
xhr.setRequestHeader('Content-Type', (asJson ? 'application/json' : 'application/x-www-form-urlencoded'))
4044
xhr.setRequestHeader('Accept', 'application/json')
4145
for (var key in headers) { xhr.setRequestHeader(key, headers[key]) }
4246
xhr.onerror = function () { callback(xhr, xhr.status) }
@@ -51,7 +55,7 @@
5155
path: uri.path,
5256
method: "POST",
5357
headers: __extend({
54-
'Content-type': 'application/x-www-form-urlencoded',
58+
'Content-type': (asJson ? 'application/json' : 'application/x-www-form-urlencoded'),
5559
'Accept': 'application/json'
5660
}, headers)
5761
}, function (response) {
@@ -241,7 +245,7 @@
241245
__request(that.options.method || "post", url, headers, {
242246
query: fragmentedQuery,
243247
variables: that.cleanAutoDeclareAnnotations(variables)
244-
}, function (response, status) {
248+
}, !!this.options.asJSON, function (response, status) {
245249
if (status == 200) {
246250
if (response.errors) {
247251
reject(response.errors)

graphql.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)