@@ -26,6 +26,11 @@ export const builder = {
26
26
describe : 'Open web version (even if desktop app available)' ,
27
27
type : 'boolean' ,
28
28
} ,
29
+ 'server-only' : {
30
+ describe : 'Run only server' ,
31
+ type : 'boolean' ,
32
+ 'default' : false
33
+ }
29
34
}
30
35
31
36
function randomString ( len = 32 ) {
@@ -37,37 +42,27 @@ function randomString(len = 32) {
37
42
. replace ( / \/ / g, '0' )
38
43
}
39
44
40
- export async function handler (
41
- context : Context ,
42
- argv : { endpoint : string ; port : string ; web : boolean } ,
43
- ) {
44
- const localPlaygroundPath = `/Applications/GraphQL\ Playground.app/Contents/MacOS/GraphQL\ Playground`
45
-
46
- if ( fs . existsSync ( localPlaygroundPath ) && ! argv . web ) {
47
- const envPath = path . join ( os . tmpdir ( ) , `${ randomString ( ) } .json` )
48
- fs . writeFileSync ( envPath , JSON . stringify ( process . env ) )
49
- const url = `graphql-playground://?cwd=${ process . cwd ( ) } &envPath=${ envPath } `
50
- opn ( url , { wait : false } )
51
- } else {
45
+ const startServer = async ( { context, endpoint, port = 3000 } : { context : Context , endpoint : string , port : string } ) =>
46
+ new Promise < string > ( async ( resolve , reject ) => {
52
47
const app = express ( )
53
-
54
48
const config = await context . getConfig ( )
55
49
const projects = config . getProjects ( )
56
50
57
51
if ( projects === undefined ) {
58
52
const projectConfig = await context . getProjectConfig ( )
53
+
59
54
if ( ! projectConfig . endpointsExtension ) {
60
55
throw noEndpointError
61
56
}
62
- const endpoint = projectConfig . endpointsExtension . getEndpoint (
63
- argv . endpoint ,
57
+ const { url , headers } = projectConfig . endpointsExtension . getEndpoint (
58
+ endpoint ,
64
59
)
65
60
66
61
app . use (
67
62
'/graphql' ,
68
63
requestProxy ( {
69
- url : endpoint . url ,
70
- headers : endpoint . headers ,
64
+ url,
65
+ headers,
71
66
} ) ,
72
67
)
73
68
@@ -85,16 +80,40 @@ export async function handler(
85
80
)
86
81
}
87
82
88
- const port = argv . port || 3000
89
-
90
83
const listener = app . listen ( port , ( ) => {
91
84
let host = listener . address ( ) . address
92
85
if ( host === '::' ) {
93
86
host = 'localhost'
94
87
}
95
88
const link = `http://${ host } :${ port } /playground`
96
89
console . log ( 'Serving playground at %s' , chalk . blue ( link ) )
97
- opn ( link )
90
+
91
+ resolve ( link )
98
92
} )
93
+ } )
94
+
95
+ export async function handler (
96
+ context : Context ,
97
+ argv : { endpoint : string ; port : string ; web : boolean , serverOnly : boolean } ,
98
+ ) {
99
+ const localPlaygroundPath = `/Applications/GraphQL\ Playground.app/Contents/MacOS/GraphQL\ Playground`
100
+
101
+ const isLocalPlaygroundAvailable = fs . existsSync ( localPlaygroundPath )
102
+
103
+ const shouldStartServer = argv . serverOnly || argv . web || ! isLocalPlaygroundAvailable
104
+
105
+ const shouldOpenBrowser = ! argv . serverOnly
106
+
107
+ if ( shouldStartServer ) {
108
+ const link = await startServer ( { context, endpoint : argv . endpoint , port : argv . port } )
109
+
110
+ if ( shouldOpenBrowser ) {
111
+ opn ( link )
112
+ }
113
+ } else {
114
+ const envPath = path . join ( os . tmpdir ( ) , `${ randomString ( ) } .json` )
115
+ fs . writeFileSync ( envPath , JSON . stringify ( process . env ) )
116
+ const url = `graphql-playground://?cwd=${ process . cwd ( ) } &envPath=${ envPath } `
117
+ opn ( url , { wait : false } )
99
118
}
100
119
}
0 commit comments