From ca67b2656c1e72da67ce804dafb881cd4c9b26d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dar=C3=ADo=20Javier=20Cravero?= Date: Sun, 27 Jan 2019 23:56:48 +0000 Subject: [PATCH 1/2] feat: allow json2graphql db.js to export a function to do async stuff inside --- community/tools/json2graphql/README.md | 13 +++++++++++++ community/tools/json2graphql/src/command.js | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/community/tools/json2graphql/README.md b/community/tools/json2graphql/README.md index fd2c5f1ff7357..7a3aa3a78491d 100644 --- a/community/tools/json2graphql/README.md +++ b/community/tools/json2graphql/README.md @@ -202,6 +202,19 @@ module.exports = { }; ``` +If you need to do some asynchronous stuff before exporting your data, you can +also export a function: + +```js +module.exports = async function() { + let db = {} + + // do asynchronous stuff + + return db +} +``` + ## Use cases ### Play with GraphQL on your MongoDB data diff --git a/community/tools/json2graphql/src/command.js b/community/tools/json2graphql/src/command.js index f745b2bca1a94..981b623d5dec0 100644 --- a/community/tools/json2graphql/src/command.js +++ b/community/tools/json2graphql/src/command.js @@ -23,7 +23,7 @@ class JSON2GraphQL extends Command { if (!db) { throw new CLIError('path to sample database is required: \'json2graphql -d ./db.js\''); } - const dbJson = this.getDbJson(db); + const dbJson = await this.getDbJson(db); const headers = key ? {'x-hasura-access-key': key} : {}; const urlVerification = await this.verifyUrl(safeUrl, headers); if (urlVerification.error) { @@ -35,7 +35,8 @@ class JSON2GraphQL extends Command { } getDbJson(db) { - return require(resolve(db)); + const ret = require(resolve(db)); + return typeof ret === 'function'? ret() : ret } getSafeUrl(url) { From 264f3df0f52c02de5fe0b9462ac4f11417d56738 Mon Sep 17 00:00:00 2001 From: wawhal Date: Fri, 1 Feb 2019 16:59:07 +0530 Subject: [PATCH 2/2] lint; add instructions about node-fetch in readme --- community/tools/json2graphql/README.md | 12 ++++--- .../json2graphql/example-datasets/function.js | 33 +++++++++++++++++++ community/tools/json2graphql/src/command.js | 2 +- 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 community/tools/json2graphql/example-datasets/function.js diff --git a/community/tools/json2graphql/README.md b/community/tools/json2graphql/README.md index 7a3aa3a78491d..825b21542e5c9 100644 --- a/community/tools/json2graphql/README.md +++ b/community/tools/json2graphql/README.md @@ -203,14 +203,16 @@ module.exports = { ``` If you need to do some asynchronous stuff before exporting your data, you can -also export a function: +also export an function: -```js -module.exports = async function() { - let db = {} +*Note: You can require [node-fetch](https://www.npmjs.com/package/node-fetch) in your function* - // do asynchronous stuff +```js +const fetch = require('node-fetch'); +module.exports = async function() { + + const db = await fetch (...) return db } ``` diff --git a/community/tools/json2graphql/example-datasets/function.js b/community/tools/json2graphql/example-datasets/function.js new file mode 100644 index 0000000000000..bc7ffec3dce1e --- /dev/null +++ b/community/tools/json2graphql/example-datasets/function.js @@ -0,0 +1,33 @@ +const fetch = require('node-fetch'); + +const db = async () => { + const response = await fetch( + 'https://bazookaand.herokuapp.com/v1alpha1/graphql', + { + method: 'POST', + headers: { + 'x-hasura-access-key': 'advancedbitch' + }, + body: JSON.stringify({ + query: ` + query { + items { + id + item + order_id + } + users { + id + name + balance + } + } + `, + }) + } + ); + const respObj = await response.json(); + return respObj.data; +} + +module.exports = db; \ No newline at end of file diff --git a/community/tools/json2graphql/src/command.js b/community/tools/json2graphql/src/command.js index 981b623d5dec0..1f11ec4db7fed 100644 --- a/community/tools/json2graphql/src/command.js +++ b/community/tools/json2graphql/src/command.js @@ -36,7 +36,7 @@ class JSON2GraphQL extends Command { getDbJson(db) { const ret = require(resolve(db)); - return typeof ret === 'function'? ret() : ret + return typeof ret === 'function' ? ret() : ret; } getSafeUrl(url) {