graphqljs-rails 0.3.5 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/assets/javascripts/graphql.js +84 -43
- data/app/assets/javascripts/graphql.min.js +1 -1
- data/lib/graphqljs/rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0f6fc4dfe6d26aad4d498a116ec59f4d1107701d
|
|
4
|
+
data.tar.gz: fceef4b7e201b89e7345bf92d8f10c6260304ef8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 036fc1147d670c2b4c5415b35df4a48b8cf138fdcb2c17adb714ffd4ce6350ad99949b324b9abbbb9446e9836dc18fec9b303905e73d0ec0842b0ee173c08056
|
|
7
|
+
data.tar.gz: c50ff6e5918cddd376119131021d4b2a79aedc56dc88b48a669ee6f870e265962312f3d8d1d07a99f4de4876945d8312d1b64da127a8d7e8de6a968a8e132563
|
|
@@ -25,23 +25,50 @@
|
|
|
25
25
|
return extended
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
for (var key in headers) {
|
|
34
|
-
xhr.setRequestHeader(key, headers[key])
|
|
35
|
-
}
|
|
36
|
-
xhr.onerror = function () {
|
|
37
|
-
callback(xhr, xhr.status)
|
|
28
|
+
function __xhr() {
|
|
29
|
+
if (window.XMLHttpRequest) {
|
|
30
|
+
return new window.XMLHttpRequest
|
|
31
|
+
} else {
|
|
32
|
+
try { return new ActiveXObject("MSXML2.XMLHTTP.3.0") } catch(ex) { return null }
|
|
38
33
|
}
|
|
39
|
-
|
|
40
|
-
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function __request(method, url, headers, data, callback) {
|
|
37
|
+
var body = "query=" + escape(data.query) + "&variables=" + escape(JSON.stringify(data.variables))
|
|
38
|
+
if (typeof XMLHttpRequest != 'undefined') {
|
|
39
|
+
var xhr = __xhr()
|
|
40
|
+
xhr.open(method, url, true)
|
|
41
|
+
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded')
|
|
42
|
+
xhr.setRequestHeader('Accept', 'application/json')
|
|
43
|
+
for (var key in headers) { xhr.setRequestHeader(key, headers[key]) }
|
|
44
|
+
xhr.onerror = function () { callback(xhr, xhr.status) }
|
|
45
|
+
xhr.onload = function () { callback(JSON.parse(xhr.responseText), xhr.status) }
|
|
46
|
+
xhr.send(body)
|
|
47
|
+
} else if (typeof require == 'function') {
|
|
48
|
+
var http = require('http'), URL = require('url'), uri = URL.parse(url)
|
|
49
|
+
var req = http.request({
|
|
50
|
+
protocol: uri.protocol,
|
|
51
|
+
hostname: uri.hostname,
|
|
52
|
+
port: uri.port,
|
|
53
|
+
path: uri.path,
|
|
54
|
+
method: "POST",
|
|
55
|
+
headers: __extend({
|
|
56
|
+
'Content-type': 'application/x-www-form-urlencoded',
|
|
57
|
+
'Accept': 'application/json'
|
|
58
|
+
}, headers)
|
|
59
|
+
}, function (response) {
|
|
60
|
+
var str = ''
|
|
61
|
+
response.setEncoding('utf8')
|
|
62
|
+
response.on('data', function (chunk) { str += chunk })
|
|
63
|
+
response.on('end', function () {
|
|
64
|
+
callback(JSON.parse(str), response.statusCode)
|
|
65
|
+
})
|
|
66
|
+
})
|
|
67
|
+
req.write(body)
|
|
68
|
+
req.end()
|
|
41
69
|
}
|
|
42
|
-
xhr.send("query=" + escape(data.query) + "&variables=" + escape(JSON.stringify(data.variables)))
|
|
43
70
|
}
|
|
44
|
-
|
|
71
|
+
|
|
45
72
|
function __isTagCall(strings) {
|
|
46
73
|
return Object.prototype.toString.call(strings) == '[object Array]' && strings.raw
|
|
47
74
|
}
|
|
@@ -53,6 +80,8 @@
|
|
|
53
80
|
for (var m in client) {
|
|
54
81
|
if (typeof client[m] == 'function') {
|
|
55
82
|
_lazy[m] = client[m].bind(client)
|
|
83
|
+
if (client[m].declare) _lazy[m].declare = client[m].declare.bind(client)
|
|
84
|
+
if (client[m].run) _lazy[m].run = client[m].run.bind(client)
|
|
56
85
|
}
|
|
57
86
|
}
|
|
58
87
|
return _lazy
|
|
@@ -74,9 +103,9 @@
|
|
|
74
103
|
// "fragment auth.login" will be "fragment auth_login"
|
|
75
104
|
FRAGMENT_SEPERATOR = "_"
|
|
76
105
|
|
|
77
|
-
// The
|
|
78
|
-
GraphQLClient.
|
|
79
|
-
|
|
106
|
+
// The autodeclare keyword.
|
|
107
|
+
GraphQLClient.AUTODECLARE_PATTERN = /\(@autodeclare\)|\(@autotype\)/
|
|
108
|
+
|
|
80
109
|
GraphQLClient.FRAGMENT_PATTERN = /\.\.\.\s*([A-Za-z0-9\.\_]+)/g
|
|
81
110
|
|
|
82
111
|
// Flattens nested object
|
|
@@ -107,7 +136,7 @@
|
|
|
107
136
|
}
|
|
108
137
|
return obj
|
|
109
138
|
}
|
|
110
|
-
|
|
139
|
+
|
|
111
140
|
GraphQLClient.prototype.collectFragments = function (query, fragments) {
|
|
112
141
|
var that = this
|
|
113
142
|
var fragmentRegexp = GraphQLClient.FRAGMENT_PATTERN
|
|
@@ -147,13 +176,13 @@
|
|
|
147
176
|
})).join("\n")
|
|
148
177
|
}
|
|
149
178
|
|
|
150
|
-
GraphQLClient.prototype.
|
|
179
|
+
GraphQLClient.prototype.autoDeclare = function (query, variables) {
|
|
151
180
|
var typeMap = {
|
|
152
181
|
string: "String",
|
|
153
182
|
number: "Int",
|
|
154
183
|
boolean: "Boolean"
|
|
155
184
|
}
|
|
156
|
-
return query.replace(GraphQLClient.
|
|
185
|
+
return query.replace(GraphQLClient.AUTODECLARE_PATTERN, function () {
|
|
157
186
|
var types = []
|
|
158
187
|
for (var key in variables) {
|
|
159
188
|
var value = variables[key]
|
|
@@ -164,11 +193,11 @@
|
|
|
164
193
|
}
|
|
165
194
|
}
|
|
166
195
|
types = types.join(", ")
|
|
167
|
-
return "("+ types +")"
|
|
196
|
+
return types ? "("+ types +")" : ""
|
|
168
197
|
})
|
|
169
198
|
}
|
|
170
199
|
|
|
171
|
-
GraphQLClient.prototype.
|
|
200
|
+
GraphQLClient.prototype.cleanAutoDeclareAnnotations = function (variables) {
|
|
172
201
|
if (!variables) variables = {}
|
|
173
202
|
var newVariables = {}
|
|
174
203
|
for (var key in variables) {
|
|
@@ -193,9 +222,9 @@
|
|
|
193
222
|
}
|
|
194
223
|
return fragmentObject
|
|
195
224
|
}
|
|
196
|
-
|
|
225
|
+
|
|
197
226
|
GraphQLClient.prototype.buildQuery = function (query, variables) {
|
|
198
|
-
return this.
|
|
227
|
+
return this.autoDeclare(this.processQuery(query, this._fragments, variables), variables)
|
|
199
228
|
}
|
|
200
229
|
|
|
201
230
|
GraphQLClient.prototype.createSenderFunction = function (url) {
|
|
@@ -213,7 +242,7 @@
|
|
|
213
242
|
return new Promise(function (resolve, reject) {
|
|
214
243
|
__request(that.options.method || "post", url, headers, {
|
|
215
244
|
query: fragmentedQuery,
|
|
216
|
-
variables: that.
|
|
245
|
+
variables: that.cleanAutoDeclareAnnotations(variables)
|
|
217
246
|
}, function (response, status) {
|
|
218
247
|
if (status == 200) {
|
|
219
248
|
if (response.errors) {
|
|
@@ -239,41 +268,50 @@
|
|
|
239
268
|
function helper(query) {
|
|
240
269
|
if (__isTagCall(query)) {
|
|
241
270
|
that.__prefix = this.prefix
|
|
271
|
+
that.__suffix = this.suffix
|
|
242
272
|
var result = that.run(that.ql.apply(that, arguments))
|
|
243
273
|
that.__prefix = ""
|
|
274
|
+
that.__suffix = ""
|
|
244
275
|
return result
|
|
245
276
|
}
|
|
246
|
-
var caller = sender(this.prefix + " " + query)
|
|
277
|
+
var caller = sender(this.prefix + " " + query + " " + this.suffix)
|
|
247
278
|
if (arguments.length > 1) {
|
|
248
279
|
return caller.apply(null, Array.prototype.slice.call(arguments, 1))
|
|
249
280
|
} else {
|
|
250
281
|
return caller
|
|
251
282
|
}
|
|
252
283
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
that[m]
|
|
261
|
-
|
|
284
|
+
|
|
285
|
+
var helpers = [
|
|
286
|
+
{method: 'mutate', type: 'mutation'},
|
|
287
|
+
{method: 'query', type: 'query'},
|
|
288
|
+
{method: 'subscribe', type: 'subscription'}
|
|
289
|
+
]
|
|
290
|
+
helpers.forEach(function (m) {
|
|
291
|
+
that[m.method] = function (query, options) {
|
|
292
|
+
if (that.options.alwaysAutodeclare === true || (options && options.declare === true)) {
|
|
293
|
+
return helper.call({prefix: m.type + " (@autodeclare) {", suffix: "}"}, query)
|
|
294
|
+
} else {
|
|
295
|
+
return helper.call({prefix: m.type, suffix: ""}, query)
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
that[m.method].run = function (query) {
|
|
299
|
+
return that[m.method](query, {})
|
|
262
300
|
}
|
|
263
301
|
})
|
|
264
302
|
this.run = function (query) {
|
|
265
303
|
return sender(query, {})
|
|
266
304
|
}
|
|
267
305
|
}
|
|
268
|
-
|
|
306
|
+
|
|
269
307
|
GraphQLClient.prototype.fragments = function () {
|
|
270
308
|
return this._fragments
|
|
271
309
|
}
|
|
272
|
-
|
|
310
|
+
|
|
273
311
|
GraphQLClient.prototype.getOptions = function () {
|
|
274
312
|
return this.options
|
|
275
313
|
}
|
|
276
|
-
|
|
314
|
+
|
|
277
315
|
GraphQLClient.prototype.fragment = function (fragment) {
|
|
278
316
|
if (typeof fragment == 'string') {
|
|
279
317
|
var _fragment = this._fragments[fragment.replace(/\./g, FRAGMENT_SEPERATOR)]
|
|
@@ -287,17 +325,20 @@
|
|
|
287
325
|
return this._fragments
|
|
288
326
|
}
|
|
289
327
|
}
|
|
290
|
-
|
|
328
|
+
|
|
291
329
|
GraphQLClient.prototype.ql = function (strings) {
|
|
292
330
|
var that = this
|
|
293
331
|
fragments = Array.prototype.slice.call(arguments, 1)
|
|
294
332
|
fragments = fragments.map(function (fragment) {
|
|
295
|
-
|
|
333
|
+
if (typeof fragment == 'string') {
|
|
334
|
+
return fragment.match(/fragment\s+([^\s]*)\s/)[1]
|
|
335
|
+
}
|
|
296
336
|
})
|
|
297
|
-
var query =
|
|
337
|
+
var query = (typeof strings == 'string') ? strings : strings.reduce(function (acc, seg, i) {
|
|
298
338
|
return acc + fragments[i - 1] + seg
|
|
299
|
-
})
|
|
300
|
-
query =
|
|
339
|
+
})
|
|
340
|
+
query = this.buildQuery(query)
|
|
341
|
+
query = ((this.__prefix||"") + " " + query + " " + (this.__suffix||"")).trim()
|
|
301
342
|
return query
|
|
302
343
|
}
|
|
303
344
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){function a(){var b={},c=!1,d=0,e=arguments.length;"[object Boolean]"==Object.prototype.toString.call(arguments[0])&&(c=arguments[0],d++);for(var f=function(d){for(var e in d)Object.prototype.hasOwnProperty.call(d,e)&&(c&&"[object Object]"==Object.prototype.toString.call(d[e])?b[e]=a(!0,b[e],d[e]):b[e]=d[e])};e>d;d++){var g=arguments[d];f(g)}return b}function b(a
|
|
1
|
+
!function(){function a(){var b={},c=!1,d=0,e=arguments.length;"[object Boolean]"==Object.prototype.toString.call(arguments[0])&&(c=arguments[0],d++);for(var f=function(d){for(var e in d)Object.prototype.hasOwnProperty.call(d,e)&&(c&&"[object Object]"==Object.prototype.toString.call(d[e])?b[e]=a(!0,b[e],d[e]):b[e]=d[e])};e>d;d++){var g=arguments[d];f(g)}return b}function b(){if(window.XMLHttpRequest)return new window.XMLHttpRequest;try{return new ActiveXObject("MSXML2.XMLHTTP.3.0")}catch(a){return null}}function c(c,d,e,f,g){var h="query="+escape(f.query)+"&variables="+escape(JSON.stringify(f.variables));if("undefined"!=typeof XMLHttpRequest){var i=b();i.open(c,d,!0),i.setRequestHeader("Content-type","application/x-www-form-urlencoded"),i.setRequestHeader("Accept","application/json");for(var j in e)i.setRequestHeader(j,e[j]);i.onerror=function(){g(i,i.status)},i.onload=function(){g(JSON.parse(i.responseText),i.status)},i.send(h)}else if("function"==typeof require){var k=require("http"),l=require("url"),m=l.parse(d),n=k.request({protocol:m.protocol,hostname:m.hostname,port:m.port,path:m.path,method:"POST",headers:a({"Content-type":"application/x-www-form-urlencoded",Accept:"application/json"},e)},function(a){var b="";a.setEncoding("utf8"),a.on("data",function(a){b+=a}),a.on("end",function(){g(JSON.parse(b),a.statusCode)})});n.write(h),n.end()}}function d(a){return"[object Array]"==Object.prototype.toString.call(a)&&a.raw}function e(a,b){if(!(this instanceof e)){var c=new e(a,b,!0),d=c._sender;for(var f in c)"function"==typeof c[f]&&(d[f]=c[f].bind(c),c[f].declare&&(d[f].declare=c[f].declare.bind(c)),c[f].run&&(d[f].run=c[f].run.bind(c)));return d}if(arguments[2]!==!0)throw"You cannot create GraphQLClient instance. Please call GraphQLClient as function.";b||(b={}),b.fragments||(b.fragments={}),this.options=b,this._fragments=this.buildFragments(b.fragments),this._sender=this.createSenderFunction(a),this.createHelpers(this._sender)}FRAGMENT_SEPERATOR="_",e.AUTODECLARE_PATTERN=/\(@autodeclare\)|\(@autotype\)/,e.FRAGMENT_PATTERN=/\.\.\.\s*([A-Za-z0-9\.\_]+)/g,e.prototype.flatten=function(a){var b,c=arguments[1]||"",d=arguments[2]||{};for(b in a)a.hasOwnProperty(b)&&("object"==typeof a[b]?this.flatten(a[b],c+b+FRAGMENT_SEPERATOR,d):d[c+b]=a[b]);return d},e.prototype.fragmentPath=function(a,b){var c=new Function("fragments","return fragments."+b.replace(/\./g,FRAGMENT_SEPERATOR)),d=c(a);if("on"!=b&&(!d||"string"!=typeof d))throw"Fragment "+b+" not found";return d},e.prototype.collectFragments=function(a,b){var c=this,d=e.FRAGMENT_PATTERN,f=[];return(a.match(d)||[]).forEach(function(a){var e=a.replace(d,function(a,b){return b}),a=c.fragmentPath(b,e);if(a){var g=new RegExp(d.source.replace(/\((.*)\)/,e));if(a.match(g))throw"Recursive fragment usage detected on "+e+".";f.push(a);var h=f.filter(function(a){return a.match(new RegExp("fragment "+e))});h.length>0&&d.test(a)&&c.collectFragments(a,b).forEach(function(a){f.unshift(a)})}}),f},e.prototype.processQuery=function(a,b){var c=e.FRAGMENT_PATTERN,d=this.collectFragments(a,b);return a=a.replace(c,function(a,b){return"... "+b.split(".").join(FRAGMENT_SEPERATOR)}),[a].concat(d.filter(function(b){return!a.match(b)})).join("\n")},e.prototype.autoDeclare=function(a,b){var c={string:"String",number:"Int","boolean":"Boolean"};return a.replace(e.AUTODECLARE_PATTERN,function(){var a=[];for(var d in b){var e=b[d],f=d.split("!"),g=f[1]||c[typeof e];g&&a.push("$"+f[0]+": "+g+"!")}return a=a.join(", "),a?"("+a+")":""})},e.prototype.cleanAutoDeclareAnnotations=function(a){a||(a={});var b={};for(var c in a){var d=a[c],e=c.split("!");b[e[0]]=d}return b},e.prototype.buildFragments=function(a){var b=this;a=this.flatten(a||{});var c={};for(var d in a){var e=a[d];"object"==typeof e?c[d]=b.buildFragments(e):c[d]="\nfragment "+d+" "+e}return c},e.prototype.buildQuery=function(a,b){return this.autoDeclare(this.processQuery(a,this._fragments,b),b)},e.prototype.createSenderFunction=function(b){var e=this;return function(f){if(d(f))return e.run(e.ql.apply(e,arguments));var g=function(d,g){g||(g={}),d||(d={});var h=e.buildQuery(f,d);return headers=a(e.options.headers||{},g.headers||{}),new Promise(function(a,f){c(e.options.method||"post",b,headers,{query:h,variables:e.cleanAutoDeclareAnnotations(d)},function(b,c){200==c?b.errors?f(b.errors):b.data&&a(b.data):f(b)})})};return arguments.length>1?g.apply(null,Array.prototype.slice.call(arguments,1)):g}},e.prototype.createHelpers=function(a){function b(b){if(d(b)){c.__prefix=this.prefix,c.__suffix=this.suffix;var e=c.run(c.ql.apply(c,arguments));return c.__prefix="",c.__suffix="",e}var f=a(this.prefix+" "+b+" "+this.suffix);return arguments.length>1?f.apply(null,Array.prototype.slice.call(arguments,1)):f}var c=this,e=[{method:"mutate",type:"mutation"},{method:"query",type:"query"},{method:"subscribe",type:"subscription"}];e.forEach(function(a){c[a.method]=function(d,e){return c.options.alwaysAutodeclare===!0||e&&e.declare===!0?b.call({prefix:a.type+" (@autodeclare) {",suffix:"}"},d):b.call({prefix:a.type,suffix:""},d)},c[a.method].run=function(b){return c[a.method](b,{})}}),this.run=function(b){return a(b,{})}},e.prototype.fragments=function(){return this._fragments},e.prototype.getOptions=function(){return this.options},e.prototype.fragment=function(b){if("string"==typeof b){var c=this._fragments[b.replace(/\./g,FRAGMENT_SEPERATOR)];if(!c)throw"Fragment "+b+" not found!";return c.trim()}return this.options.fragments=a(!0,this.options.fragments,b),this._fragments=this.buildFragments(this.options.fragments),this._fragments},e.prototype.ql=function(a){fragments=Array.prototype.slice.call(arguments,1),fragments=fragments.map(function(a){return"string"==typeof a?a.match(/fragment\s+([^\s]*)\s/)[1]:void 0});var b="string"==typeof a?a:a.reduce(function(a,b,c){return a+fragments[c-1]+b});return b=this.buildQuery(b),b=((this.__prefix||"")+" "+b+" "+(this.__suffix||"")).trim()},function(a,b){"function"==typeof define&&define.amd?define(function(){return a.graphql=b(e)}):"object"==typeof module&&module.exports?module.exports=b(a.GraphQLClient):a.graphql=b(a.GraphQLClient)}(this,function(){return e})}();
|