2
2
3
3
import * as express from 'express'
4
4
import * as graphqlHTTP from 'express-graphql'
5
+ import * as cors from 'cors'
5
6
import { createGraphQlSchema } from 'oasgraph'
6
7
import * as path from 'path'
7
8
import * as request from 'request'
@@ -25,6 +26,7 @@ program
25
26
. option ( '-s, --strict' , 'throw an error if OASGraph cannot run without compensating for errors or missing data in the OAS' )
26
27
. option ( '-a, --addSubOperations' , 'nest operations based on path hierarchy' )
27
28
. option ( '-f, --fillEmptyResponses' , 'create placeholder schemas for operations with HTTP status code 204 (no response) rather than ignore them' )
29
+ . option ( '--cors' , 'enable Cross-origin resource sharing (CORS)' )
28
30
. option ( '--no-viewer' , 'do not create GraphQL viewer objects for passing authentication credentials' )
29
31
. option ( '--save <file path>' , 'save schema to path and do not start server' )
30
32
. action ( function ( path ) {
@@ -42,16 +44,16 @@ if (program.port) {
42
44
portNumber = program . port
43
45
}
44
46
45
- // check if the file exists
47
+ // check if the file exists
46
48
if ( fs . existsSync ( path . resolve ( filePath ) ) ) {
47
49
try {
48
50
let oas = readFile ( path . resolve ( filePath ) )
49
51
startGraphQLServer ( oas , portNumber )
50
52
} catch ( e ) {
51
53
console . error ( e )
52
54
}
53
-
54
- } else {
55
+
56
+ } else {
55
57
// falls back to a remote location
56
58
if ( filePath . match ( / ^ h t t p s ? / g) ) {
57
59
getRemoteFileSpec ( filePath ) . then ( remoteContent => {
@@ -105,7 +107,7 @@ function getRemoteFileSpec (uri) {
105
107
}
106
108
107
109
/**
108
- * generates a GraphQL schema and starts the GraphQL server on the specified port
110
+ * generates a GraphQL schema and starts the GraphQL server on the specified port
109
111
* @param {Object } oas the OAS specification file
110
112
* @param {number } port the port number to listen on on this server
111
113
*/
@@ -117,14 +119,19 @@ function startGraphQLServer(oas, port) {
117
119
addSubOperations : program . addSubOperations ,
118
120
fillEmptyResponses : program . fillEmptyResponses ,
119
121
baseUrl : program . url
120
- } )
122
+ } )
121
123
. then ( ( { schema, report} ) => {
122
124
console . log ( JSON . stringify ( report , null , 2 ) )
123
125
124
- // save local file if required
126
+ // save local file if required
125
127
if ( program . save ) {
126
128
writeSchema ( schema ) ;
127
129
} else {
130
+ // Enable CORS
131
+ if ( program . cors ) {
132
+ app . use ( cors ( ) ) ;
133
+ }
134
+
128
135
// mounting graphql endpoint using the middleware express-graphql
129
136
app . use ( '/graphql' , graphqlHTTP ( {
130
137
schema : schema ,
@@ -144,7 +151,7 @@ function startGraphQLServer(oas, port) {
144
151
145
152
/**
146
153
* saves a grahpQL schema generated by OASGraph to a file
147
- * @param {createGraphQlSchema } schema
154
+ * @param {createGraphQlSchema } schema
148
155
*/
149
156
function writeSchema ( schema ) {
150
157
fs . writeFile ( program . save , printSchema ( schema ) , ( err ) => {
0 commit comments