Skip to content

Commit 35f5101

Browse files
committed
chore: include lib files and temporary scoped release
1 parent a86d3a8 commit 35f5101

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

lib/index.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ var _typeMap = require('./typeMap');
3333
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
3434

3535
var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
36+
var proxyUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
37+
3638
var rootType = new _graphql.GraphQLObjectType({
3739
name: 'Query',
3840
fields: function fields() {
@@ -41,7 +43,7 @@ var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
4143
type: new _graphql.GraphQLObjectType({
4244
name: 'viewer',
4345
fields: function fields() {
44-
var queryFields = getQueriesFields(endpoints, false);
46+
var queryFields = getQueriesFields(endpoints, false, proxyUrl);
4547
if (!(0, _keys2.default)(queryFields).length) {
4648
throw new Error('Did not find any GET endpoints');
4749
}
@@ -60,7 +62,7 @@ var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
6062
query: rootType
6163
};
6264

63-
var mutationFields = getQueriesFields(endpoints, true);
65+
var mutationFields = getQueriesFields(endpoints, true, proxyUrl);
6466
if ((0, _keys2.default)(mutationFields).length) {
6567
graphQLSchema.mutation = new _graphql.GraphQLObjectType({
6668
name: 'Mutation',
@@ -72,41 +74,42 @@ var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
7274
};
7375

7476

75-
var resolver = function resolver(endpoint) {
77+
var resolver = function resolver(endpoint, proxyUrl) {
7678
return function () {
77-
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(_, args, opts) {
78-
var req, res;
79+
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(_, args, opts) {
80+
var proxy, req, res;
7981
return _regenerator2.default.wrap(function _callee$(_context) {
8082
while (1) {
8183
switch (_context.prev = _context.next) {
8284
case 0:
83-
req = endpoint.request(args, opts.GQLProxyBaseUrl);
85+
proxy = !proxyUrl ? opts.GQLProxyBaseUrl : typeof proxyUrl === 'function' ? proxyUrl(opts) : proxyUrl;
86+
req = endpoint.request(args, proxy);
8487

8588
if (opts.headers) {
8689
req.headers = (0, _assign2.default)({}, req.headers, opts.headers);
8790
}
88-
_context.next = 4;
91+
_context.next = 5;
8992
return (0, _requestPromise2.default)(req);
9093

91-
case 4:
94+
case 5:
9295
res = _context.sent;
9396
return _context.abrupt('return', JSON.parse(res));
9497

95-
case 6:
98+
case 7:
9699
case 'end':
97100
return _context.stop();
98101
}
99102
}
100103
}, _callee, undefined);
101104
}));
102105

103-
return function (_x, _x2, _x3) {
106+
return function (_x2, _x3, _x4) {
104107
return _ref.apply(this, arguments);
105108
};
106109
}();
107110
};
108111

109-
var getQueriesFields = function getQueriesFields(endpoints, isMutation) {
112+
var getQueriesFields = function getQueriesFields(endpoints, isMutation, proxyUrl) {
110113
return (0, _keys2.default)(endpoints).filter(function (typeName) {
111114
return !!endpoints[typeName].mutation === !!isMutation;
112115
}).reduce(function (result, typeName) {
@@ -116,15 +119,16 @@ var getQueriesFields = function getQueriesFields(endpoints, isMutation) {
116119
type: type,
117120
description: endpoint.description,
118121
args: (0, _typeMap.mapParametersToFields)(endpoint.parameters, typeName),
119-
resolve: resolver(endpoint)
122+
resolve: resolver(endpoint, proxyUrl)
120123
};
121124
result[typeName] = gType;
122125
return result;
123126
}, {});
124127
};
125128

126129
var build = function () {
127-
var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2(swaggerPath) {
130+
var _ref2 = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2(swaggerPath) {
131+
var proxyUrl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
128132
var swaggerSchema, endpoints, schema;
129133
return _regenerator2.default.wrap(function _callee2$(_context2) {
130134
while (1) {
@@ -136,7 +140,7 @@ var build = function () {
136140
case 2:
137141
swaggerSchema = _context2.sent;
138142
endpoints = (0, _swagger.getAllEndPoints)(swaggerSchema);
139-
schema = schemaFromEndpoints(endpoints);
143+
schema = schemaFromEndpoints(endpoints, proxyUrl);
140144
return _context2.abrupt('return', schema);
141145

142146
case 6:
@@ -147,7 +151,7 @@ var build = function () {
147151
}, _callee2, undefined);
148152
}));
149153

150-
return function build(_x4) {
154+
return function build(_x5) {
151155
return _ref2.apply(this, arguments);
152156
};
153157
}();

package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
2-
"name": "swagger-to-graphql",
3-
"version": "1.2.2",
2+
"name": "@kbrandwijk/swagger-to-graphql",
3+
"version": "1.3.2",
44
"author": "Roman Krivtsov",
5+
"contributors": [
6+
"Kim Brandwijk <[email protected]>"
7+
],
58
"bin": "./bin/swagger2graphql",
69
"dependencies": {
710
"babel-runtime": "^6.25.0",

0 commit comments

Comments
 (0)