UNPKG

@graphql-mesh/cli

Version:
47 lines (46 loc) 1.62 kB
/* eslint-disable import/no-nodejs-modules */ import { promises as fsPromises } from 'fs'; import { getGraphQLWSOptions } from './getGraphQLWSOpts.js'; export async function startNodeHttpServer({ meshHTTPHandler, getBuiltMesh, sslCredentials, graphqlPath, hostname, port, }) { let server; if (sslCredentials) { const [key, cert] = await Promise.all([ fsPromises.readFile(sslCredentials.key), fsPromises.readFile(sslCredentials.cert), ]); const nodeHttps = await import('https'); server = nodeHttps.createServer({ key, cert, }, meshHTTPHandler); } else { const nodeHttp = await import('http'); server = nodeHttp.createServer(meshHTTPHandler); } const ws = await import('ws'); const wsServer = new ws.WebSocketServer({ path: graphqlPath, server, }); const { useServer } = await import('graphql-ws/use/ws'); useServer(getGraphQLWSOptions(getBuiltMesh), wsServer); return new Promise((resolve, reject) => { server.once('error', err => reject(err)); server.listen(port, hostname, () => { resolve({ stop: () => new Promise((resolve, reject) => { server.closeAllConnections(); server.close(err => { if (err) { reject(err); } else { resolve(); } }); }), }); }); }); }