这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions community/tools/json2graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,21 @@ module.exports = {
};
```

If you need to do some asynchronous stuff before exporting your data, you can
also export an function:

*Note: You can require [node-fetch](https://www.npmjs.com/package/node-fetch) in your function*

```js
const fetch = require('node-fetch');

module.exports = async function() {

const db = await fetch (...)
return db
}
```

## Use cases

### Play with GraphQL on your MongoDB data
Expand Down
33 changes: 33 additions & 0 deletions community/tools/json2graphql/example-datasets/function.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 3 additions & 2 deletions community/tools/json2graphql/src/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class JSON2GraphQL extends Command {
if (!db) {
throw new CLIError('path to sample database is required: \'json2graphql <url> -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) {
Expand All @@ -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) {
Expand Down