Skip to content

Commit 8775b5c

Browse files
committed
es6 fixes, babel, example, tests, bin mock
1 parent d48c3b8 commit 8775b5c

File tree

8 files changed

+713
-11
lines changed

8 files changed

+713
-11
lines changed

.babelrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"presets": ["es2015", "stage-0"]
2+
"presets": ["es2015", "stage-0"],
3+
"plugins": ["add-module-exports"]
34
}

bin/config-yargs.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = function Conf(yargs) {
2+
yargs
3+
.help('help')
4+
.alias('help', 'h', '?')
5+
.version()
6+
.alias('version', 'v')
7+
.options({
8+
'swagger-schema': {
9+
type: 'string',
10+
alias: 'i',
11+
describe: 'Path to Swagger schema (JSON or YAML)',
12+
requiresArg: true
13+
},
14+
'output': {
15+
alias: 'o',
16+
type: 'string',
17+
requiresArg: true,
18+
describe: 'Path to file with GraphQL schema'
19+
}
20+
}).strict();
21+
};

bin/swagger2graphql.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
var argv = require('yargs')
3+
.usage('Usage: $0 -i [path] -o [path]')
4+
.demandOption(['i','o'])
5+
.argv;

example/example.js example/app.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
require('babel-polyfill');
22
const express = require('express');
33
const app = express();
4-
var graphqlHTTP = require('express-graphql');
5-
var graphql = require('graphql');
6-
var graphQLSchema = require('swagger-to-graphql').default;
4+
const graphqlHTTP = require('express-graphql');
5+
const graphQLSchema = require('../lib');
76

8-
graphQLSchema('../test/fixtures/petstore.json').then(schema => {
7+
graphQLSchema(`${__dirname}/../test/fixtures/petstore.yaml`).then(schema => {
98
app.use('/graphql', graphqlHTTP(() => {
109
return {
1110
schema,
@@ -17,7 +16,7 @@ graphQLSchema('../test/fixtures/petstore.json').then(schema => {
1716
}));
1817

1918
app.listen(3009, 'localhost', () => {
20-
console.info(`http://localhost:3009/graphql`);
19+
console.info('http://localhost:3009/graphql');
2120
});
2221
}).catch(e => {
2322
console.log(e);

package.json

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,30 @@
22
"name": "swagger-to-graphql",
33
"version": "1.0.6",
44
"author": "Roman Krivtsov",
5+
"bin": "./bin/swagger2graphql",
56
"dependencies": {
67
"graphql": "^0.10.1",
78
"isomorphic-fetch": "^2.2.1",
9+
"js-yaml": "^3.8.4",
810
"json-schema-ref-parser": "^3.1.2",
911
"lodash": "^4.16.4",
1012
"node-request-by-swagger": "^1.0.5",
1113
"request": "^2.75.0",
12-
"request-promise": "^4.1.1"
14+
"request-promise": "^4.1.1",
15+
"yargs": "^8.0.2"
1316
},
1417
"devDependencies": {
1518
"babel-cli": "^6.24.1",
1619
"babel-core": "^6.24.1",
1720
"babel-eslint": "^7.2.3",
21+
"babel-plugin-add-module-exports": "^0.2.1",
1822
"babel-preset-es2015": "^6.24.1",
1923
"babel-preset-stage-0": "^6.24.1",
2024
"eslint": "^3.9.1",
2125
"eslint-config-airbnb-es5": "^1.1.0",
2226
"eslint-plugin-react": "^6.5.0",
27+
"express": "^4.15.3",
28+
"express-graphql": "^0.6.6",
2329
"flow-bin": "^0.47.0",
2430
"mocha": "^3.1.2"
2531
},

src/index.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// @flow
2+
require('babel-polyfill');
23
import rp from 'request-promise';
3-
import { GraphQLSchema, GraphQLObjectType } from 'graphql';
4+
import { GraphQLSchema, GraphQLObjectType, printSchema } from 'graphql';
45
import { getAllEndPoints, loadSchema } from './swagger';
56
import { createGQLObject, mapParametersToFields } from './typeMap';
67

7-
export const schemaFromEndpoints = (endpoints) => {
8+
const schemaFromEndpoints = (endpoints) => {
89
const rootType = new GraphQLObjectType({
910
name: 'Query',
1011
fields: () => ({
@@ -67,7 +68,8 @@ const getQueriesFields = (endpoints, isMutation) => {
6768
const build = async (swaggerPath) => {
6869
const swaggerSchema = await loadSchema(swaggerPath);
6970
const endpoints = getAllEndPoints(swaggerSchema);
70-
return schemaFromEndpoints(endpoints);
71+
const schema = schemaFromEndpoints(endpoints);
72+
console.log(printSchema(schema));
7173
};
7274

7375
export default build;

src/typeMap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export const getTypeFields = (jsonSchema, title, isInputType) => {
8181

8282
if (!Object.keys(fields).length) {
8383
fields.empty = {
84-
description: 'This object is empty actually',
84+
description: 'default field',
8585
type: graphql.GraphQLString
8686
}
8787
}

0 commit comments

Comments
 (0)