From 442f900fda1d74f2e2aee3e024cf8decfb3a88fd Mon Sep 17 00:00:00 2001 From: Panoplos Date: Sun, 30 Jul 2017 23:29:36 +0900 Subject: [PATCH] Added support for a config file and TLS option for unauthorised CA or self-signed certs (for local development.) --- Changelog.md | 3 +++ Readme.md | 56 +++++++++++++++++++++++++++++++++++-------- example.gql2flow.json | 5 ++++ index.js | 30 ++++++++++++++--------- package.json | 4 ++-- util/fetcher.js | 10 ++++++-- util/fileIO.js | 3 +++ 7 files changed, 86 insertions(+), 25 deletions(-) create mode 100644 example.gql2flow.json diff --git a/Changelog.md b/Changelog.md index 93d6341..b3cd1a6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,6 @@ +## 0.5.0 +- Added config file support and option for unauthorised TSL CA or self-signed certs (for local development.) + ## 0.2.1 - Fix Version number in command line diff --git a/Readme.md b/Readme.md index 5b0b4b0..d975c83 100644 --- a/Readme.md +++ b/Readme.md @@ -1,25 +1,61 @@ # GraphQL To Flow Types +## Installation & Usage + +To install globally: + ```shell npm i -g gql2flow ``` +To install locally and add as a dependency to project: + +```shell +npm i gql2flow --save-dev +``` + +You can then call gql2flow via `node run gql2flow `. + +``` +Usage: index [options] + + Options: + -h, --help output usage information + -V, --version output the version number + -c --config [configFile] Use specified configuation file. By default, gql2flow looks for a gql2flow.json config file in the current directory. + -u --unauthorised Allow fetch over TLS with unauthorised CA or self-signed certs. (Only use in development environments.) + -e --export use export rather than declare to define types + -o --output-file [outputFile] name for ouput file, defaults to graphql-export.flow.js + --null-keys [nullKeys] makes all keys nullable + --null-values [nullValues] makes all values nullable + -m --module-name [moduleName] name for the export module. Types are not wrapped in a module if this is not set + -t --typeMap Define custom scalar types where typeSpec is : + -i --ignored-types names of types to ignore (comma delimited) + -w --whitelist names of types to whitelist (comma delimited) ``` -Usage: gql2flow [options] -Options: +## Configuration Options +gql2flow can be used with command line parameters only, or it is possible to create a `gql2flow.json` configuration file and either save it to the current +working directory, where gqp2flow will look automatically, or specify it with the `-c` or `--config` command line option. - -h, --help output usage information - -V, --version output the version number - -o --output-file [outputFile] name for output file, defaults to graphql-export.flow.js - -m --module-name [moduleName] name for the export module. Types are not wrapped in a module if this is not set - -i --ignored-types names of types to ignore (comma delimited) - -w --whitelist names of types to whitelist (comma delimited) - --null-keys adds a '?' to all keys - --null-values adds a '?' to all values +#### Example Config File + +```json +{ + "unauthorised": true, + "export": true, + "outputFile": "flow-output.js" +} ``` +You would then save this to `gql2flow.json` and place in the base directory, or call gql2flow as follows: +```shell +gql2flow https://localhost:3000/graphql +``` + +This can be added to the script section of your `package.json` file for simplicity. + ## Examples #### Fetching from a server diff --git a/example.gql2flow.json b/example.gql2flow.json new file mode 100644 index 0000000..ce380f8 --- /dev/null +++ b/example.gql2flow.json @@ -0,0 +1,5 @@ +{ + "unauthorised": true, + "export": true, + "outputFile": "flow-output.js" +} \ No newline at end of file diff --git a/index.js b/index.js index 8c967a8..f016a4f 100755 --- a/index.js +++ b/index.js @@ -17,8 +17,17 @@ const moduleUtils = require('./util/module'); const fetchUtils = require('./util/fetcher'); program - .version('0.4.4') - .usage('[options] ') + .version('0.5.0') + .usage('[options] ') + .option( + '-c --config [configFile]', + 'Use specified configuation file. By default, gql2flow looks for a gql2flow.json config file in the current directory.', + fileIO.pathJoin(__dirname, 'gql2flow.json') + ) + .option( + '-u --unauthorised', + 'Allow fetch over TLS with unauthorised CA or self-signed certs. (Only use in development environments.)', + false) .option('-e --export', 'use export rather than declare to define types') .option( '-o --output-file [outputFile]', @@ -60,9 +69,10 @@ program [] ) .action((path, options) => { + const config = fileIO.isFile(options.config) ? Object.assign({}, options, fileIO.readFile(options.config)) : options const getSchema = new Promise((resolve, reject) => { if (isUrl(path)) { - fetchUtils.fetchWithIntrospection(path, (err, schema) => { + fetchUtils.fetchWithIntrospection(path, config, (err, schema) => { if (err) { reject(err); } else if (!schema.data) { @@ -80,16 +90,14 @@ program getSchema .then(schema => { - let interfaces = interfaceUtils.generateTypes(schema, options); + let interfaces = interfaceUtils.generateTypes(schema, config); + let module = moduleUtils.generateModule(config.moduleName, interfaces); - let module = moduleUtils.generateModule(options.moduleName, interfaces); - - moduleUtils.writeModuleToFile(options.outputFile, module); + moduleUtils.writeModuleToFile(config.outputFile, module); }) .catch(err => console.error(err.message)); }) - .parse(process.argv); -if (!process.argv.slice(2).length) { - program.outputHelp(); -} +program.on(['-h', '--help'], program.outputHelp); + +program.parse(process.argv); diff --git a/package.json b/package.json index b5d14f2..fa51d95 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gql2flow", - "version": "0.4.5", + "version": "0.5.0", "description": "Convert a GraphQL Schema to a Flowtype definition", "main": "index.js", "dependencies": { @@ -38,7 +38,7 @@ "engines": { "node": ">=4.0.0" }, - "perferGlobal": true, + "perferGlobal": false, "bugs": { "url": "https://github.com/joarwilk/gql2flow/issues" }, diff --git a/util/fetcher.js b/util/fetcher.js index 188ff3f..22abfba 100644 --- a/util/fetcher.js +++ b/util/fetcher.js @@ -1,3 +1,4 @@ +const https = require('https') const fetch = require('node-fetch'); const fs = require('fs'); const { @@ -6,10 +7,15 @@ const { printSchema, } = require('graphql/utilities'); +const agent = new https.Agent({ + rejectUnauthorized: false +}); + module.exports = { - fetchWithIntrospection: (url, callback) => { + fetchWithIntrospection: (url, config, callback) => { console.log('Fetching', url); fetch(url, { + agent: config.unauthorised ? agent : undefined, method: 'POST', headers: { Accept: 'application/json', @@ -18,7 +24,7 @@ module.exports = { body: JSON.stringify({ query: introspectionQuery }), }) .then(res => res.json()) - .then(schemaa => callback(null, schemaa)) + .then(schema => callback(null, schema)) .catch(err => callback(err)); }, }; diff --git a/util/fileIO.js b/util/fileIO.js index c3e12f5..bbcd612 100644 --- a/util/fileIO.js +++ b/util/fileIO.js @@ -1,7 +1,10 @@ 'use strict'; const fs = require('fs'); +const path = require('path') module.exports = { + pathJoin: (dir, file) => path.join(dir, file), + isFile: fileName => fs.existsSync(fileName) && fs.lstatSync(fileName).isFile(), readFile: fileName => JSON.parse(fs.readFileSync(fileName).toString()), writeToFile: (fileName, data) => fs.writeFile(fileName, data, err => err && console.error('Failed to write', err) ) }