Skip to content

Commit 23adfb8

Browse files
committed
add float autodeclaration support
1 parent 4bf84e2 commit 23adfb8

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

graphql.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,19 @@
190190
GraphQLClient.prototype.autoDeclare = function (query, variables) {
191191
var typeMap = {
192192
string: "String",
193-
number: "Int",
193+
number: function (value) {
194+
return value % 1 === 0 ? "Int" : "Float";
195+
},
194196
boolean: "Boolean"
195197
}
196198
return query.replace(GraphQLClient.AUTODECLARE_PATTERN, function () {
197199
var types = []
198200
for (var key in variables) {
199201
var value = variables[key]
200202
var keyAndType = key.split("!")
201-
var type = (keyAndType[1] || typeMap[typeof(value)])
203+
var mapping = typeMap[typeof(value)]
204+
var mappedType = typeof(mapping) === "function" ? mapping(value) : mapping
205+
var type = (keyAndType[1] || mappedType)
202206
if (type) {
203207
types.push("$" + keyAndType[0] + ": " + type + "!")
204208
}

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.

test/test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var queryIn = `query (@autodeclare) {
3333
}
3434
}`
3535

36-
var expectedQuery = `query ($name: String!, $bool: Boolean!, $int: Int!) {
36+
var expectedQuery = `query ($name: String!, $bool: Boolean!, $int: Int!, $float: Float!) {
3737
user(name: $name, bool: $bool, int: $int) {
3838
... auth_user
3939
... auth_error
@@ -49,7 +49,7 @@ fragment auth_user on User {token, ...user}
4949
5050
fragment auth_error on Error {messages}`
5151

52-
assert.equal(client.buildQuery(queryIn, {name: "fatih", bool: true, int: 2}), expectedQuery)
52+
assert.equal(client.buildQuery(queryIn, {name: "fatih", bool: true, int: 2, float: 2.3}), expectedQuery)
5353

5454
assert.equal(typeof client.query(`($email: String!, $password: String!) {
5555
auth(email: $email, password: $password) {

0 commit comments

Comments
 (0)