@@ -33,6 +33,8 @@ var _typeMap = require('./typeMap');
33
33
function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { default : obj } ; }
34
34
35
35
var schemaFromEndpoints = function schemaFromEndpoints ( endpoints ) {
36
+ var proxyUrl = arguments . length > 1 && arguments [ 1 ] !== undefined ? arguments [ 1 ] : null ;
37
+
36
38
var rootType = new _graphql . GraphQLObjectType ( {
37
39
name : 'Query' ,
38
40
fields : function fields ( ) {
@@ -41,7 +43,7 @@ var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
41
43
type : new _graphql . GraphQLObjectType ( {
42
44
name : 'viewer' ,
43
45
fields : function fields ( ) {
44
- var queryFields = getQueriesFields ( endpoints , false ) ;
46
+ var queryFields = getQueriesFields ( endpoints , false , proxyUrl ) ;
45
47
if ( ! ( 0 , _keys2 . default ) ( queryFields ) . length ) {
46
48
throw new Error ( 'Did not find any GET endpoints' ) ;
47
49
}
@@ -60,7 +62,7 @@ var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
60
62
query : rootType
61
63
} ;
62
64
63
- var mutationFields = getQueriesFields ( endpoints , true ) ;
65
+ var mutationFields = getQueriesFields ( endpoints , true , proxyUrl ) ;
64
66
if ( ( 0 , _keys2 . default ) ( mutationFields ) . length ) {
65
67
graphQLSchema . mutation = new _graphql . GraphQLObjectType ( {
66
68
name : 'Mutation' ,
@@ -72,41 +74,42 @@ var schemaFromEndpoints = function schemaFromEndpoints(endpoints) {
72
74
} ;
73
75
74
76
75
- var resolver = function resolver ( endpoint ) {
77
+ var resolver = function resolver ( endpoint , proxyUrl ) {
76
78
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 ;
79
81
return _regenerator2 . default . wrap ( function _callee$ ( _context ) {
80
82
while ( 1 ) {
81
83
switch ( _context . prev = _context . next ) {
82
84
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 ) ;
84
87
85
88
if ( opts . headers ) {
86
89
req . headers = ( 0 , _assign2 . default ) ( { } , req . headers , opts . headers ) ;
87
90
}
88
- _context . next = 4 ;
91
+ _context . next = 5 ;
89
92
return ( 0 , _requestPromise2 . default ) ( req ) ;
90
93
91
- case 4 :
94
+ case 5 :
92
95
res = _context . sent ;
93
96
return _context . abrupt ( 'return' , JSON . parse ( res ) ) ;
94
97
95
- case 6 :
98
+ case 7 :
96
99
case 'end' :
97
100
return _context . stop ( ) ;
98
101
}
99
102
}
100
103
} , _callee , undefined ) ;
101
104
} ) ) ;
102
105
103
- return function ( _x , _x2 , _x3 ) {
106
+ return function ( _x2 , _x3 , _x4 ) {
104
107
return _ref . apply ( this , arguments ) ;
105
108
} ;
106
109
} ( ) ;
107
110
} ;
108
111
109
- var getQueriesFields = function getQueriesFields ( endpoints , isMutation ) {
112
+ var getQueriesFields = function getQueriesFields ( endpoints , isMutation , proxyUrl ) {
110
113
return ( 0 , _keys2 . default ) ( endpoints ) . filter ( function ( typeName ) {
111
114
return ! ! endpoints [ typeName ] . mutation === ! ! isMutation ;
112
115
} ) . reduce ( function ( result , typeName ) {
@@ -116,15 +119,16 @@ var getQueriesFields = function getQueriesFields(endpoints, isMutation) {
116
119
type : type ,
117
120
description : endpoint . description ,
118
121
args : ( 0 , _typeMap . mapParametersToFields ) ( endpoint . parameters , typeName ) ,
119
- resolve : resolver ( endpoint )
122
+ resolve : resolver ( endpoint , proxyUrl )
120
123
} ;
121
124
result [ typeName ] = gType ;
122
125
return result ;
123
126
} , { } ) ;
124
127
} ;
125
128
126
129
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 ;
128
132
var swaggerSchema , endpoints , schema ;
129
133
return _regenerator2 . default . wrap ( function _callee2$ ( _context2 ) {
130
134
while ( 1 ) {
@@ -136,7 +140,7 @@ var build = function () {
136
140
case 2 :
137
141
swaggerSchema = _context2 . sent ;
138
142
endpoints = ( 0 , _swagger . getAllEndPoints ) ( swaggerSchema ) ;
139
- schema = schemaFromEndpoints ( endpoints ) ;
143
+ schema = schemaFromEndpoints ( endpoints , proxyUrl ) ;
140
144
return _context2 . abrupt ( 'return' , schema ) ;
141
145
142
146
case 6 :
@@ -147,7 +151,7 @@ var build = function () {
147
151
} , _callee2 , undefined ) ;
148
152
} ) ) ;
149
153
150
- return function build ( _x4 ) {
154
+ return function build ( _x5 ) {
151
155
return _ref2 . apply ( this , arguments ) ;
152
156
} ;
153
157
} ( ) ;
0 commit comments