|
31 | 31 | })
|
32 | 32 | }
|
33 | 33 |
|
34 |
| - function __request(method, url, headers, data, asJson, onRequestError, callback) { |
| 34 | + function __request(debug, method, url, headers, data, asJson, onRequestError, callback) { |
35 | 35 | if (!url) {
|
36 | 36 | return;
|
37 | 37 | }
|
|
40 | 40 | } else {
|
41 | 41 | var body = "query=" + encodeURIComponent(data.query) + "&variables=" + encodeURIComponent(JSON.stringify(data.variables))
|
42 | 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 | + } |
43 | 52 | if (typeof XMLHttpRequest != 'undefined') {
|
44 | 53 | var xhr = new XMLHttpRequest
|
45 | 54 | xhr.open(method, url, true)
|
|
114 | 123 | this.url = url
|
115 | 124 | this.options = options || {}
|
116 | 125 | this._fragments = this.buildFragments(options.fragments)
|
117 |
| - this._sender = this.createSenderFunction() |
| 126 | + this._sender = this.createSenderFunction(options.debug) |
118 | 127 | this.createHelpers(this._sender)
|
| 128 | + this._transaction = {} |
119 | 129 | }
|
120 | 130 |
|
121 | 131 | // "fragment auth.login" will be "fragment auth_login"
|
122 | 132 | FRAGMENT_SEPERATOR = "_"
|
123 | 133 |
|
124 | 134 | // The autodeclare keyword.
|
125 |
| - GraphQLClient.AUTODECLARE_PATTERN = /\(@autodeclare\)|\(@autotype\)/ |
| 135 | + GraphQLClient.AUTODECLARE_PATTERN = /\(@autodeclare\)|\(@autotype\)/g |
126 | 136 |
|
127 | 137 | GraphQLClient.FRAGMENT_PATTERN = /\.\.\.\s*([A-Za-z0-9\.\_]+)/g
|
128 | 138 |
|
|
265 | 275 | return this.autoDeclare(this.processQuery(query, this._fragments), variables)
|
266 | 276 | }
|
267 | 277 |
|
268 |
| - GraphQLClient.prototype.createSenderFunction = function () { |
| 278 | + GraphQLClient.prototype.createSenderFunction = function (debug) { |
269 | 279 | var that = this
|
270 |
| - return function (query) { |
| 280 | + return function (query, originalQuery, type) { |
271 | 281 | if (__isTagCall(query)) {
|
272 | 282 | return that.run(that.ql.apply(that, arguments))
|
273 | 283 | }
|
|
278 | 288 | headers = __extend((that.options.headers||{}), (requestOptions.headers||{}))
|
279 | 289 |
|
280 | 290 | return new Promise(function (resolve, reject) {
|
281 |
| - __request(that.options.method || "post", that.getUrl(), headers, { |
| 291 | + __request(debug, that.options.method || "post", that.getUrl(), headers, { |
282 | 292 | query: fragmentedQuery,
|
283 | 293 | variables: that.cleanAutoDeclareAnnotations(variables)
|
284 | 294 | }, !!that.options.asJSON, that.options.onRequestError, function (response, status) {
|
|
296 | 306 | })
|
297 | 307 | })
|
298 | 308 | }
|
299 |
| - if (arguments.length > 1) { |
300 |
| - return caller.apply(null, Array.prototype.slice.call(arguments, 1)) |
| 309 | + |
| 310 | + caller.merge = function (mergeName, variables) { |
| 311 | + that._transaction[mergeName] = that._transaction[mergeName] || { |
| 312 | + query: [], |
| 313 | + mutation: [] |
| 314 | + } |
| 315 | + return new Promise(function (resolve) { |
| 316 | + that._transaction[mergeName][type].push({ |
| 317 | + type: type, |
| 318 | + query: originalQuery, |
| 319 | + variables: variables, |
| 320 | + resolver: resolve |
| 321 | + }) |
| 322 | + }) |
| 323 | + } |
| 324 | + if (arguments.length > 3) { |
| 325 | + return caller.apply(null, Array.prototype.slice.call(arguments, 3)) |
301 | 326 | }
|
302 | 327 | return caller
|
303 | 328 | }
|
304 | 329 | }
|
305 | 330 |
|
| 331 | + GraphQLClient.prototype.commit = function (mergeName) { |
| 332 | + var that = this |
| 333 | + var resolveMap = {} |
| 334 | + var mergedVariables = {} |
| 335 | + var mergedQueries = {} |
| 336 | + Object.keys(this._transaction[mergeName]).forEach(function (method) { |
| 337 | + if (that._transaction[mergeName][method].length === 0) return |
| 338 | + var subQuery = that._transaction[mergeName][method].map(function (merge) { |
| 339 | + var reqId = 'merge' + Math.random().toString().split('.')[1].substr(0, 4) |
| 340 | + resolveMap[reqId] = merge.resolver |
| 341 | + var query = merge.query.replace(/\$([^\.\,\s\)]*)/g, function (_, m) { |
| 342 | + var matchingKey = Object.keys(merge.variables).filter(function (key) { |
| 343 | + return key === m || key.match(new RegExp('^' + m + '!')) |
| 344 | + })[0] |
| 345 | + var variable = reqId + '__' + matchingKey |
| 346 | + mergedVariables[method] = mergedVariables[method] || {} |
| 347 | + mergedVariables[method][variable] = merge.variables[matchingKey] |
| 348 | + return '$' + variable.split('!')[0] |
| 349 | + }) |
| 350 | + return reqId + '_' + query.trim().match(/^[^\(]+/)[0] + ': ' + query |
| 351 | + }).join('\n') |
| 352 | + |
| 353 | + mergedQueries[method] = mergedQueries[method] || [] |
| 354 | + mergedQueries[method].push(method + " (@autodeclare) {\n" + subQuery + "\n }") |
| 355 | + }) |
| 356 | + |
| 357 | + Promise.all(Object.keys(mergedQueries).map(function (method) { |
| 358 | + var query = mergedQueries[method].join('\n') |
| 359 | + var variables = mergedVariables[method] |
| 360 | + return that._sender(query, query, null, variables) |
| 361 | + })).then(function (responses) { |
| 362 | + responses.forEach(function (response) { |
| 363 | + Object.keys(response).forEach(function (mergeKey) { |
| 364 | + var parsedKey = mergeKey.match(/^(merge\d+)\_(.*)/) |
| 365 | + var newResponse = {} |
| 366 | + newResponse[parsedKey[2]] = response[mergeKey] |
| 367 | + resolveMap[parsedKey[1]](newResponse) |
| 368 | + }) |
| 369 | + }) |
| 370 | + }).finally(function () { |
| 371 | + that._transaction[mergeName] = { query: [], mutation: [] } |
| 372 | + }) |
| 373 | + // console.log(mergedQueries) |
| 374 | + // console.log(mergedVariables) |
| 375 | + // console.log(resolveMap) |
| 376 | + // return this._sender(mergedQuery, mergedQuery, null, mergedVariables).then(function (response) { |
| 377 | + // console.log(response) |
| 378 | + // }) |
| 379 | + } |
| 380 | + |
306 | 381 | GraphQLClient.prototype.createHelpers = function (sender) {
|
307 | 382 | var that = this
|
308 | 383 | function helper(query) {
|
|
314 | 389 | that.__suffix = ""
|
315 | 390 | return result
|
316 | 391 | }
|
317 |
| - var caller = sender(this.prefix + " " + query + " " + this.suffix) |
| 392 | + var caller = sender(this.prefix + " " + query + " " + this.suffix, query.trim(), this.type) |
318 | 393 | if (arguments.length > 1 && arguments[1] != null) {
|
319 | 394 | return caller.apply(null, Array.prototype.slice.call(arguments, 1))
|
320 |
| - } else { |
321 |
| - return caller |
322 | 395 | }
|
| 396 | + return caller |
323 | 397 | }
|
324 | 398 |
|
325 | 399 | var helpers = [
|
|
330 | 404 | helpers.forEach(function (m) {
|
331 | 405 | that[m.method] = function (query, variables, options) {
|
332 | 406 | if (that.options.alwaysAutodeclare === true || (options && options.declare === true)) {
|
333 |
| - return helper.call({prefix: m.type + " (@autodeclare) {", suffix: "}"}, query, variables) |
334 |
| - } else { |
335 |
| - return helper.call({prefix: m.type, suffix: ""}, query, variables) |
| 407 | + return helper.call({type: m.type, prefix: m.type + " (@autodeclare) {", suffix: "}"}, query, variables) |
336 | 408 | }
|
| 409 | + return helper.call({type: m.type, prefix: m.type, suffix: ""}, query, variables) |
337 | 410 | }
|
338 | 411 | that[m.method].run = function (query, options) {
|
339 | 412 | return that[m.method](query, options)({})
|
340 | 413 | }
|
341 | 414 | })
|
342 | 415 | this.run = function (query) {
|
343 |
| - return sender(query, {}) |
| 416 | + return sender(query, originalQuery, m.type, {}) |
344 | 417 | }
|
345 | 418 | }
|
346 | 419 |
|
|
386 | 459 | return query
|
387 | 460 | }
|
388 | 461 |
|
| 462 | + // GraphQLClient.prototype.startTransaction = function () { |
| 463 | + // if (this.transaction) { |
| 464 | + // console.groupEnd() |
| 465 | + // console.error('[graphql]: transaction is already started') |
| 466 | + // return |
| 467 | + // } |
| 468 | + // if (this.options.debug) { |
| 469 | + // console.group('%c[graphql]: transaction started, following requests will be collected until end', 'font-weight: bold') |
| 470 | + // } |
| 471 | + // this.transaction = [] |
| 472 | + // } |
| 473 | + |
| 474 | + // GraphQLClient.prototype.endTransaction = function () { |
| 475 | + // if (!this.transaction) { |
| 476 | + // console.error('[graphql]: cannot end a transaction which is not started') |
| 477 | + // return |
| 478 | + // } |
| 479 | + // if (this.options.debug) { |
| 480 | + // console.groupEnd() |
| 481 | + // } |
| 482 | + // this.transaction = null |
| 483 | + // } |
| 484 | + |
389 | 485 | ;(function (root, factory) {
|
390 | 486 | if (typeof define === 'function' && define.amd) {
|
391 | 487 | define(function () {
|
|
0 commit comments