Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for a config file and TLS option for unauthorised CA/self-signed certs. #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
56 changes: 46 additions & 10 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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 <schemaPath>`.

```
Usage: index [options] <schemaPath>

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 <typeSpec> Define custom scalar types where typeSpec is <graphql type>:<flow type>
-i --ignored-types <ignoredTypes> names of types to ignore (comma delimited)
-w --whitelist <whitelist> names of types to whitelist (comma delimited)
```
Usage: gql2flow [options] <schema.json>

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 <ignoredTypes> names of types to ignore (comma delimited)
-w --whitelist <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
Expand Down
5 changes: 5 additions & 0 deletions example.gql2flow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"unauthorised": true,
"export": true,
"outputFile": "flow-output.js"
}
30 changes: 19 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,17 @@ const moduleUtils = require('./util/module');
const fetchUtils = require('./util/fetcher');

program
.version('0.4.4')
.usage('[options] <schema.json>')
.version('0.5.0')
.usage('[options] <schemaPath>')
.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]',
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -38,7 +38,7 @@
"engines": {
"node": ">=4.0.0"
},
"perferGlobal": true,
"perferGlobal": false,
"bugs": {
"url": "https://github.com/joarwilk/gql2flow/issues"
},
Expand Down
10 changes: 8 additions & 2 deletions util/fetcher.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const https = require('https')
const fetch = require('node-fetch');
const fs = require('fs');
const {
Expand All @@ -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',
Expand All @@ -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));
},
};
3 changes: 3 additions & 0 deletions util/fileIO.js
Original file line number Diff line number Diff line change
@@ -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) )
}