|
16 | 16 | } |
17 | 17 | } |
18 | 18 | } |
19 | | - |
| 19 | + |
20 | 20 | for (; i < length; i++) { |
21 | 21 | var obj = arguments[i] |
22 | 22 | merge(obj) |
23 | 23 | } |
24 | | - |
| 24 | + |
25 | 25 | return extended |
26 | 26 | } |
27 | 27 |
|
28 | 28 | function __unique(array) { |
29 | | - return array.filter( function onlyUnique(value, index, self) { |
| 29 | + return array.filter( function onlyUnique(value, index, self) { |
30 | 30 | return self.indexOf(value) === index; |
31 | 31 | }) |
32 | 32 | } |
33 | | - |
| 33 | + |
34 | 34 | function __request(method, url, headers, data, asJson, callback) { |
35 | 35 | if (asJson) { |
36 | 36 | var body = JSON.stringify({query: data.query, variables: data.variables}); |
|
70 | 70 | req.end() |
71 | 71 | } |
72 | 72 | } |
73 | | - |
| 73 | + |
74 | 74 | function __isTagCall(strings) { |
75 | 75 | return Object.prototype.toString.call(strings) == '[object Array]' && strings.raw |
76 | 76 | } |
77 | | - |
| 77 | + |
78 | 78 | function GraphQLClient(url, options) { |
79 | 79 | if (!(this instanceof GraphQLClient)) { |
80 | 80 | var client = new GraphQLClient(url, options, true) |
|
92 | 92 | } |
93 | 93 | if (!options) |
94 | 94 | options = {} |
95 | | - |
| 95 | + |
96 | 96 | if (!options.fragments) |
97 | 97 | options.fragments = {} |
98 | | - |
| 98 | + |
99 | 99 | this.options = options |
100 | 100 | this._fragments = this.buildFragments(options.fragments) |
101 | 101 | this._sender = this.createSenderFunction(url) |
102 | 102 | this.createHelpers(this._sender) |
103 | 103 | } |
104 | | - |
| 104 | + |
105 | 105 | // "fragment auth.login" will be "fragment auth_login" |
106 | 106 | FRAGMENT_SEPERATOR = "_" |
107 | | - |
| 107 | + |
108 | 108 | // The autodeclare keyword. |
109 | 109 | GraphQLClient.AUTODECLARE_PATTERN = /\(@autodeclare\)|\(@autotype\)/ |
110 | | - |
| 110 | + |
111 | 111 | GraphQLClient.FRAGMENT_PATTERN = /\.\.\.\s*([A-Za-z0-9\.\_]+)/g |
112 | | - |
| 112 | + |
113 | 113 | // Flattens nested object |
114 | 114 | /* |
115 | 115 | * {a: {b: {c: 1, d: 2}}} => {"a.b.c": 1, "a.b.d": 2} |
|
125 | 125 | } |
126 | 126 | return out |
127 | 127 | } |
128 | | - |
| 128 | + |
129 | 129 | // Gets path from object |
130 | 130 | /* |
131 | 131 | * {a: {b: {c: 1, d: 2}}}, "a.b.c" => 1 |
|
138 | 138 | } |
139 | 139 | return obj |
140 | 140 | } |
141 | | - |
| 141 | + |
142 | 142 | GraphQLClient.prototype.collectFragments = function (query, fragments) { |
143 | 143 | var that = this |
144 | 144 | var fragmentRegexp = GraphQLClient.FRAGMENT_PATTERN |
|
165 | 165 | }) |
166 | 166 | return __unique(collectedFragments) |
167 | 167 | } |
168 | | - |
| 168 | + |
169 | 169 | GraphQLClient.prototype.processQuery = function (query, fragments) { |
170 | 170 | var fragmentRegexp = GraphQLClient.FRAGMENT_PATTERN |
171 | 171 | var collectedFragments = this.collectFragments(query, fragments) |
|
177 | 177 | return !query.match(fragment) |
178 | 178 | })).join("\n") |
179 | 179 | } |
180 | | - |
| 180 | + |
181 | 181 | GraphQLClient.prototype.autoDeclare = function (query, variables) { |
182 | 182 | var typeMap = { |
183 | 183 | string: "String", |
|
198 | 198 | return types ? "("+ types +")" : "" |
199 | 199 | }) |
200 | 200 | } |
201 | | - |
| 201 | + |
202 | 202 | GraphQLClient.prototype.cleanAutoDeclareAnnotations = function (variables) { |
203 | 203 | if (!variables) variables = {} |
204 | 204 | var newVariables = {} |
|
209 | 209 | } |
210 | 210 | return newVariables |
211 | 211 | } |
212 | | - |
| 212 | + |
213 | 213 | GraphQLClient.prototype.buildFragments = function (fragments) { |
214 | 214 | var that = this |
215 | 215 | fragments = this.flatten(fragments || {}) |
|
224 | 224 | } |
225 | 225 | return fragmentObject |
226 | 226 | } |
227 | | - |
| 227 | + |
228 | 228 | GraphQLClient.prototype.buildQuery = function (query, variables) { |
229 | 229 | return this.autoDeclare(this.processQuery(query, this._fragments, variables), variables) |
230 | 230 | } |
231 | | - |
| 231 | + |
232 | 232 | GraphQLClient.prototype.createSenderFunction = function (url) { |
233 | 233 | var that = this |
234 | 234 | return function (query) { |
|
240 | 240 | if (!variables) variables = {} |
241 | 241 | var fragmentedQuery = that.buildQuery(query, variables) |
242 | 242 | headers = __extend((that.options.headers||{}), (requestOptions.headers||{})) |
243 | | - |
| 243 | + |
244 | 244 | return new Promise(function (resolve, reject) { |
245 | 245 | __request(that.options.method || "post", url, headers, { |
246 | 246 | query: fragmentedQuery, |
247 | 247 | variables: that.cleanAutoDeclareAnnotations(variables) |
248 | | - }, !!this.options.asJSON, function (response, status) { |
| 248 | + }, !!that.options.asJSON, function (response, status) { |
249 | 249 | if (status == 200) { |
250 | 250 | if (response.errors) { |
251 | 251 | reject(response.errors) |
|
264 | 264 | return caller |
265 | 265 | } |
266 | 266 | } |
267 | | - |
| 267 | + |
268 | 268 | GraphQLClient.prototype.createHelpers = function (sender) { |
269 | 269 | var that = this |
270 | 270 | function helper(query) { |
|
305 | 305 | return sender(query, {}) |
306 | 306 | } |
307 | 307 | } |
308 | | - |
| 308 | + |
309 | 309 | GraphQLClient.prototype.fragments = function () { |
310 | 310 | return this._fragments |
311 | 311 | } |
312 | | - |
| 312 | + |
313 | 313 | GraphQLClient.prototype.getOptions = function () { |
314 | 314 | return this.options |
315 | 315 | } |
316 | | - |
| 316 | + |
317 | 317 | GraphQLClient.prototype.fragment = function (fragment) { |
318 | 318 | if (typeof fragment == 'string') { |
319 | 319 | var _fragment = this._fragments[fragment.replace(/\./g, FRAGMENT_SEPERATOR)] |
|
327 | 327 | return this._fragments |
328 | 328 | } |
329 | 329 | } |
330 | | - |
| 330 | + |
331 | 331 | GraphQLClient.prototype.ql = function (strings) { |
332 | 332 | var that = this |
333 | 333 | fragments = Array.prototype.slice.call(arguments, 1) |
|
343 | 343 | query = ((this.__prefix||"") + " " + query + " " + (this.__suffix||"")).trim() |
344 | 344 | return query |
345 | 345 | } |
346 | | - |
| 346 | + |
347 | 347 | ;(function (root, factory) { |
348 | 348 | if (typeof define === 'function' && define.amd) { |
349 | 349 | define(function () { |
|
0 commit comments