Skip to content

Commit

Permalink
Add await support to extendHttpServer (#9026)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Cousens <[email protected]>
  • Loading branch information
dcousens and dcousens authored Feb 14, 2024
1 parent c0dacbf commit 8147976
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/boolean-not-false-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
----
'@keystone-6/core': patch
----

Fix `config.server.cors` type blocking `false` values
2 changes: 1 addition & 1 deletion .changeset/boolean-not-false.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
----
'@keystone-6/auth': core
'@keystone-6/core': patch
----

Fix `defaultIsFilterable` and `defaultIsOrderable` types blocking `true` values
5 changes: 5 additions & 0 deletions .changeset/http-async.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-6/core': minor
---

Add `async` to `extendHttpServer`, for `await` on startup
4 changes: 2 additions & 2 deletions docs/pages/docs/config/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ export default config<TypeInfo>({
cors: { origin: ['http://localhost:7777'], credentials: true },
port: 3000,
maxFileSize: 200 * 1024 * 1024,
extendExpressApp: (app, commonContext) => { /* ... */ },
extendHttpServer: (httpServer, commonContext, graphQLSchema) => { /* ... */ },
extendExpressApp: async (app, commonContext) => { /* ... */ },
extendHttpServer: async (httpServer, commonContext, graphQLSchema) => { /* ... */ },
},
/* ... */
});
Expand Down
19 changes: 5 additions & 14 deletions packages/core/src/lib/server/createExpressServer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createServer, type Server } from 'http'
import cors, { type CorsOptions } from 'cors'
import cors from 'cors'
import { json } from 'body-parser'
import { expressMiddleware } from '@apollo/server/express4'
import express from 'express'
Expand All @@ -20,8 +20,6 @@ The Admin UI takes a while to build for dev, and is created separately
so the CLI can bring up the dev server early to handle GraphQL requests.
*/

const DEFAULT_MAX_FILE_SIZE = 200 * 1024 * 1024 // 200 MiB

function formatError (graphqlConfig: GraphQLConfig | undefined) {
return (formattedError: GraphQLFormattedError, error: unknown) => {
let debug = graphqlConfig?.debug
Expand Down Expand Up @@ -56,9 +54,7 @@ export async function createExpressServer (
const httpServer = createServer(expressServer)

if (config.server?.cors) {
// Setting config.server.cors = true will provide backwards compatible defaults
// Otherwise, the user can provide their own config object to use
const corsConfig: CorsOptions =
const corsConfig =
typeof config.server.cors === 'boolean'
? { origin: true, credentials: true }
: config.server.cors
Expand All @@ -80,13 +76,8 @@ export async function createExpressServer (
})
}

if (config.server?.extendExpressApp) {
await config.server.extendExpressApp(expressServer, context)
}

if (config.server?.extendHttpServer) {
config.server?.extendHttpServer(httpServer, context, graphQLSchema)
}
await config.server?.extendExpressApp?.(expressServer, context)
await config.server?.extendHttpServer?.(httpServer, context, graphQLSchema)

if (config.storage) {
for (const val of Object.values(config.storage)) {
Expand Down Expand Up @@ -129,8 +120,8 @@ export async function createExpressServer (
} as ApolloServerOptions<KeystoneContext>

const apolloServer = new ApolloServer({ ...serverConfig })
const maxFileSize = config.server?.maxFileSize

const maxFileSize = config.server?.maxFileSize ?? DEFAULT_MAX_FILE_SIZE
expressServer.use(graphqlUploadExpress({ maxFileSize }))
await apolloServer.start()
expressServer.use(
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ export function initConfig (config: KeystoneConfig) {
config.db.url ??= 'postgres://'

return {
...config,
types: {
path: 'node_modules/.keystone/types.ts'
path: 'node_modules/.keystone/types.ts',
...config.types,
},
...config,
db: {
prismaSchemaPath: 'schema.prisma',
...config.db,
Expand All @@ -38,6 +39,13 @@ export function initConfig (config: KeystoneConfig) {
...config.graphql,
},
lists: applyIdFieldDefaults(config),
server: {
cors: false,
maxFileSize: 200 * 1024 * 1024, // 200 MiB
extendExpressApp: async () => {},
extendHttpServer: async () => {},
...config.server,
},
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ export type AdminFileToWrite =

// config.server
export type ServerConfig<TypeInfo extends BaseKeystoneTypeInfo> = {
/** Configuration options for the cors middleware. Set to `true` to use core Keystone defaults */
cors?: CorsOptions | true
/** Configuration options for the cors middleware. Set to `true` to use Keystone's defaults */
cors?: boolean | CorsOptions
/** Maximum upload file size allowed (in bytes) */
maxFileSize?: number

Expand All @@ -210,14 +210,14 @@ export type ServerConfig<TypeInfo extends BaseKeystoneTypeInfo> = {
extendExpressApp?: (
app: express.Express,
context: KeystoneContext<TypeInfo>
) => void | Promise<void>
) => MaybePromise<void>

/** extend the node:http server used by Keystone */
extendHttpServer?: (
server: Server,
context: KeystoneContext<TypeInfo>,
graphqlSchema: GraphQLSchema
) => void
) => MaybePromise<void>
} & (
| {
/** Port number to start the server on. Defaults to process.env.PORT || 3000 */
Expand Down

0 comments on commit 8147976

Please sign in to comment.