From 879671307856c45cf8c3c25f85551bcaf7fa4aeb Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 31 Jul 2017 14:30:20 +0200 Subject: [PATCH 1/2] Added expressions support --- index.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 9fa5123..84de0d5 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -module.exports = nanogql +'use strict' var getOpname = /query ([\w\d-_]+)? ?\(.*?\)? \{/ @@ -16,3 +16,28 @@ function nanogql (str) { function merge (str, chunk) { return str + chunk } + +function taggify () { + var args = new Array(arguments.length) + for (var i = 0; i < args.length; i++) { + args[i] = arguments[i] + } + + var string = args.shift() + var raw = string.raw + var result = '' + + args.forEach(applySubstitutions) + result += raw[raw.length - 1] + return nanogql(result) + + function applySubstitutions (arg, i) { + var lit = raw[i] + if (Array.isArray(arg)) { + arg = arg.join('') + } + result += lit + arg + } +} + +module.exports = taggify From a4a6b77199bcf4493f668f0b9de17a5079158b19 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 31 Jul 2017 14:30:28 +0200 Subject: [PATCH 2/2] Updated test --- test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test.js b/test.js index 6873c63..deed737 100644 --- a/test.js +++ b/test.js @@ -48,3 +48,27 @@ tape('should have a name', function (assert) { }) assert.end() }) + +tape('should support expressions', function (assert) { + var query = gql` + query foo ($number_of_repos:Int!) { + viewer { + ${'na' + 'me'} + repositories(last: $number_of_repos) { + ${'nodes'} { + name + } + } + } + } + ` + + var variables = { number_of_repos: 3 } + var data = query(variables) + spok(assert, data, { + query: spok.string, + operationName: 'foo', + variables: JSON.stringify(variables) + }) + assert.end() +})