@@ -7,15 +7,15 @@ import { createGQLObject, mapParametersToFields } from './typeMap';
7
7
8
8
type Endpoints = { [ string ] : Endpoint } ;
9
9
10
- const schemaFromEndpoints = ( endpoints : Endpoints ) => {
10
+ const schemaFromEndpoints = ( endpoints : Endpoints , proxyUrl : ? ( Function | string ) = null ) => {
11
11
const rootType = new GraphQLObjectType ( {
12
12
name : 'Query' ,
13
13
fields : ( ) => ( {
14
14
viewer : {
15
15
type : new GraphQLObjectType ( {
16
16
name : 'viewer' ,
17
17
fields : ( ) => {
18
- const queryFields = getQueriesFields ( endpoints , false ) ;
18
+ const queryFields = getQueriesFields ( endpoints , false , proxyUrl ) ;
19
19
if ( ! Object . keys ( queryFields ) . length ) {
20
20
throw new Error ( 'Did not find any GET endpoints' ) ;
21
21
}
@@ -31,7 +31,7 @@ const schemaFromEndpoints = (endpoints: Endpoints) => {
31
31
query : rootType
32
32
} ;
33
33
34
- const mutationFields = getQueriesFields ( endpoints , true ) ;
34
+ const mutationFields = getQueriesFields ( endpoints , true , proxyUrl ) ;
35
35
if ( Object . keys ( mutationFields ) . length ) {
36
36
graphQLSchema . mutation = new GraphQLObjectType ( {
37
37
name : 'Mutation' ,
@@ -42,17 +42,18 @@ const schemaFromEndpoints = (endpoints: Endpoints) => {
42
42
return new GraphQLSchema ( graphQLSchema ) ;
43
43
} ;
44
44
45
- const resolver = ( endpoint : Endpoint ) =>
45
+ const resolver = ( endpoint : Endpoint , proxyUrl : ? ( Function | string ) ) =>
46
46
async ( _ , args : GraphQLParameters , opts : SwaggerToGraphQLOptions ) = > {
47
- const req = endpoint . request ( args , opts . GQLProxyBaseUrl ) ;
47
+ const proxy = ! proxyUrl ? opts . GQLProxyBaseUrl : typeof proxyUrl === 'function' ? proxyUrl ( opts ) : proxyUrl
48
+ const req = endpoint . request ( args , proxy ) ;
48
49
if ( opts . headers ) {
49
50
req . headers = Object . assign ( { } , req . headers , opts . headers ) ;
50
51
}
51
52
const res = await rp ( req ) ;
52
53
return JSON . parse ( res ) ;
53
54
} ;
54
55
55
- const getQueriesFields = ( endpoints : Endpoints , isMutation : boolean ) : { [ string ] : GraphQLType } = > {
56
+ const getQueriesFields = ( endpoints : Endpoints , isMutation : boolean , proxyUrl : ? ( Function | string ) ) : { [ string ] : GraphQLType } = > {
56
57
return Object . keys ( endpoints ) . filter ( ( typeName : string ) => {
57
58
return ! ! endpoints [ typeName ] . mutation === ! ! isMutation ;
58
59
} ) . reduce ( ( result , typeName ) => {
@@ -62,17 +63,17 @@ const getQueriesFields = (endpoints: Endpoints, isMutation: boolean): {[string]:
62
63
type,
63
64
description : endpoint . description ,
64
65
args : mapParametersToFields ( endpoint . parameters , typeName ) ,
65
- resolve : resolver ( endpoint )
66
+ resolve : resolver ( endpoint , proxyUrl )
66
67
} ;
67
68
result [ typeName ] = gType ;
68
69
return result ;
69
70
} , { } ) ;
70
71
} ;
71
72
72
- const build = async ( swaggerPath : string ) => {
73
+ const build = async ( swaggerPath : string , proxyUrl : ? ( Function | string ) = null ) => {
73
74
const swaggerSchema = await loadSchema ( swaggerPath ) ;
74
75
const endpoints = getAllEndPoints ( swaggerSchema ) ;
75
- const schema = schemaFromEndpoints ( endpoints ) ;
76
+ const schema = schemaFromEndpoints ( endpoints , proxyUrl ) ;
76
77
return schema ;
77
78
} ;
78
79
0 commit comments