-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance(serve): fallback to
node:http
when uWebSockets is not avail…
…able (#6936) * enhance(serve): fallback to `node:http` if uWebSockets fails to start * Better logic * Add tests to artifacts integration test * Fix imports * Test HTTP connection * Do not check the response * chore(dependencies): updated changesets for modified dependencies * Fix leak tests * Try another hostname * Remove another step * Nevermind --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
039badd
commit c4d2249
Showing
18 changed files
with
423 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphql-mesh/cli": patch | ||
--- | ||
dependencies updates: | ||
- Added dependency [`ws@^8.17.0` ↗︎](https://www.npmjs.com/package/ws/v/8.17.0) (to `dependencies`) | ||
- Removed dependency [`uWebSockets.js@uNetworking/uWebSockets.js#semver:^20` ↗︎](https://www.npmjs.com/package/uWebSockets.js/v/20.0.0) (from `dependencies`) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"@graphql-mesh/utils": patch | ||
"@graphql-mesh/serve-cli": patch | ||
--- | ||
|
||
Fallback to node:http when uWebSockets.js is not available |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/legacy/cli/src/commands/serve/getGraphQLWSOpts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { execute, ExecutionArgs, subscribe } from 'graphql'; | ||
import { MeshInstance } from '@graphql-mesh/runtime'; | ||
|
||
export function getGraphQLWSOptions(getBuiltMesh: () => Promise<MeshInstance>) { | ||
// yoga's envelop may augment the `execute` and `subscribe` operations | ||
// so we need to make sure we always use the freshest instance | ||
type EnvelopedExecutionArgs = ExecutionArgs & { | ||
rootValue: { | ||
execute: typeof execute; | ||
subscribe: typeof subscribe; | ||
}; | ||
}; | ||
return { | ||
execute: args => (args as EnvelopedExecutionArgs).rootValue.execute(args), | ||
subscribe: args => (args as EnvelopedExecutionArgs).rootValue.subscribe(args), | ||
onSubscribe: async (ctx, msg) => { | ||
const { getEnveloped } = await getBuiltMesh(); | ||
const { schema, execute, subscribe, contextFactory, parse, validate } = getEnveloped(ctx); | ||
|
||
const args: EnvelopedExecutionArgs = { | ||
schema, | ||
operationName: msg.payload.operationName, | ||
document: parse(msg.payload.query), | ||
variableValues: msg.payload.variables, | ||
contextValue: await contextFactory(), | ||
rootValue: { | ||
execute, | ||
subscribe, | ||
}, | ||
}; | ||
|
||
const errors = validate(args.schema, args.document); | ||
if (errors.length) return errors; | ||
return args; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
import { promises as fsPromises } from 'fs'; | ||
import type { Server as HttpServer } from 'http'; | ||
import type { Server as HttpsServer } from 'https'; | ||
import { useServer } from 'graphql-ws/lib/use/ws'; | ||
import { getGraphQLWSOptions } from './getGraphQLWSOpts.js'; | ||
import type { ServerStartOptions, ServerStartResult } from './types.js'; | ||
|
||
export async function startNodeHttpServer({ | ||
meshHTTPHandler, | ||
getBuiltMesh, | ||
sslCredentials, | ||
graphqlPath, | ||
hostname, | ||
port, | ||
}: ServerStartOptions): Promise<ServerStartResult> { | ||
let server: HttpServer | HttpsServer; | ||
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, | ||
}); | ||
useServer(getGraphQLWSOptions(getBuiltMesh), wsServer); | ||
return new Promise((resolve, reject) => { | ||
server.once('error', err => reject(err)); | ||
server.listen(port, hostname, () => { | ||
resolve({ | ||
stop: () => { | ||
server.closeAllConnections(); | ||
server.close(); | ||
}, | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { MeshHTTPHandler } from '@graphql-mesh/http'; | ||
import type { MeshInstance } from '@graphql-mesh/runtime'; | ||
import type { YamlConfig } from '@graphql-mesh/types'; | ||
|
||
export interface ServerStartOptions { | ||
meshHTTPHandler: MeshHTTPHandler; | ||
getBuiltMesh: () => Promise<MeshInstance>; | ||
sslCredentials: YamlConfig.ServeConfig['sslCredentials']; | ||
graphqlPath: string; | ||
hostname: string; | ||
port: number; | ||
} | ||
|
||
export interface ServerStartResult { | ||
stop: VoidFunction; | ||
} |
Oops, something went wrong.