Skip to content

Commit ac506a7

Browse files
authoredFeb 23, 2018
feat(playground): introduce --server-only flag
Add `--server-only` flag for playground cmd
2 parents b8503a5 + 1d32f8f commit ac506a7

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed
 

‎src/cmds/playground.ts

+39-20
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export const builder = {
2626
describe: 'Open web version (even if desktop app available)',
2727
type: 'boolean',
2828
},
29+
'server-only': {
30+
describe: 'Run only server',
31+
type: 'boolean',
32+
'default': false
33+
}
2934
}
3035

3136
function randomString(len = 32) {
@@ -37,37 +42,27 @@ function randomString(len = 32) {
3742
.replace(/\//g, '0')
3843
}
3944

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) => {
5247
const app = express()
53-
5448
const config = await context.getConfig()
5549
const projects = config.getProjects()
5650

5751
if (projects === undefined) {
5852
const projectConfig = await context.getProjectConfig()
53+
5954
if (!projectConfig.endpointsExtension) {
6055
throw noEndpointError
6156
}
62-
const endpoint = projectConfig.endpointsExtension.getEndpoint(
63-
argv.endpoint,
57+
const { url, headers } = projectConfig.endpointsExtension.getEndpoint(
58+
endpoint,
6459
)
6560

6661
app.use(
6762
'/graphql',
6863
requestProxy({
69-
url: endpoint.url,
70-
headers: endpoint.headers,
64+
url,
65+
headers,
7166
}),
7267
)
7368

@@ -85,16 +80,40 @@ export async function handler(
8580
)
8681
}
8782

88-
const port = argv.port || 3000
89-
9083
const listener = app.listen(port, () => {
9184
let host = listener.address().address
9285
if (host === '::') {
9386
host = 'localhost'
9487
}
9588
const link = `http://${host}:${port}/playground`
9689
console.log('Serving playground at %s', chalk.blue(link))
97-
opn(link)
90+
91+
resolve(link)
9892
})
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 })
99118
}
100119
}

0 commit comments

Comments
 (0)