@@ -4,44 +4,51 @@ const rp = require('request-promise');
4
4
const { GraphQLSchema, GraphQLObjectType} = require ( 'graphql' ) ;
5
5
const { getAllEndPoints, loadSchema} = require ( './swagger' ) ;
6
6
const { createGQLObject, mapParametersToFields} = require ( './type_map' ) ;
7
- const build = ( swaggerPath ) => {
8
- return loadSchema ( swaggerPath ) . then ( swaggerSchema => {
9
- const endpoints = getAllEndPoints ( swaggerSchema ) ;
10
- const rootType = new GraphQLObjectType ( {
11
- name : 'Query' ,
12
- fields : ( ) => ( {
13
- viewer : {
14
- type : new GraphQLObjectType ( {
15
- name : 'viewer' ,
16
- fields : ( ) => {
17
- const queryFields = getQueriesFields ( endpoints , false ) ;
18
- if ( ! Object . keys ( queryFields ) . length ) {
19
- throw new Error ( 'Did not find any GET endpoints' ) ;
20
- }
21
- return queryFields ;
7
+
8
+ const schemaFromEndpoints = ( endpoints ) => {
9
+ const rootType = new GraphQLObjectType ( {
10
+ name : 'Query' ,
11
+ fields : ( ) => ( {
12
+ viewer : {
13
+ type : new GraphQLObjectType ( {
14
+ name : 'viewer' ,
15
+ fields : ( ) => {
16
+ const queryFields = getQueriesFields ( endpoints , false ) ;
17
+ if ( ! Object . keys ( queryFields ) . length ) {
18
+ throw new Error ( 'Did not find any GET endpoints' ) ;
22
19
}
23
- } ) ,
24
- resolve : ( ) => 'Without this resolver graphql does not resolve further'
25
- }
26
- } )
27
- } ) ;
20
+ return queryFields ;
21
+ }
22
+ } ) ,
23
+ resolve : ( ) => 'Without this resolver graphql does not resolve further'
24
+ }
25
+ } )
26
+ } ) ;
28
27
29
- const graphQLSchema = {
30
- query : rootType
31
- } ;
28
+ const graphQLSchema = {
29
+ query : rootType
30
+ } ;
32
31
33
- const mutationFields = getQueriesFields ( endpoints , true ) ;
34
- if ( Object . keys ( mutationFields ) . length ) {
35
- graphQLSchema . mutation = new GraphQLObjectType ( {
36
- name : 'Mutation' ,
37
- fields : mutationFields
38
- } ) ;
39
- }
32
+ const mutationFields = getQueriesFields ( endpoints , true ) ;
33
+ if ( Object . keys ( mutationFields ) . length ) {
34
+ graphQLSchema . mutation = new GraphQLObjectType ( {
35
+ name : 'Mutation' ,
36
+ fields : mutationFields
37
+ } ) ;
38
+ }
39
+
40
+ return new GraphQLSchema ( graphQLSchema ) ;
41
+ } ;
40
42
41
- return new GraphQLSchema ( graphQLSchema ) ;
43
+ const build = ( swaggerPath ) => {
44
+ return loadSchema ( swaggerPath ) . then ( swaggerSchema => {
45
+ const endpoints = getAllEndPoints ( swaggerSchema ) ;
46
+ return schemaFromEndpoints ( endpoints ) ;
42
47
} ) ;
43
48
} ;
44
49
50
+ build . schemaFromEndpoints = schemaFromEndpoints ;
51
+
45
52
function resolver ( endpoint ) {
46
53
return ( _ , args , opts ) => {
47
54
const req = endpoint . request ( args , {
0 commit comments