diff --git a/README.md b/README.md index 787ee7cdd..002f43849 100644 --- a/README.md +++ b/README.md @@ -19,13 +19,17 @@ Please beware that this is a PROTOTYPE. Do NOT use this for serious work. Thanks - [Heroku](#heroku) - [Conventions](#conventions) - [`schema.ts` | `schema/*`](#schemats--schema) - - [`app.ts`](#appts) + - [About](#about) - [Aliases](#aliases) + - [`app.ts`](#appts) + - [About](#about-1) + - [Aliases](#aliases-1) - [Example Layouts](#example-layouts) - [API](#api) - [`app`](#app) - [`app.addContext`](#appaddcontext) - [`app.`](#appnexusdefblock) + - [`app.server.start`](#appserverstart) - [CLI](#cli) - [Development](#development) - [Overview](#overview-1) @@ -302,18 +306,32 @@ $ node node_modules/.build ### `schema.ts` | `schema/*` -Optional. Schema contains your GraphQL type definitions. It can be a single module or folder of modules. Multiple instances of module/folder-modules throughout your source tree is supported. +Optional –– Your GraphQL type definitions. + +##### About + +It can be a single module or folder of modules. Multiple instances of module/folder-modules throughout your source tree is supported. In dev mode schema modules are synchronously found and imported at server boot time. At build time however static imports for all schema modules are inlined for boot performance. +##### Aliases + +n/a + ### `app.ts` -Optional. App contains the entrypoint to your service, the place where it boots. There can only be at most a single `app.ts` in your source tree. +Optional –– The entrypoint to your app + +##### About + +There can only be at most a single `app.ts`/`server.ts`/`service.ts` module in your source tree. + +This module is optional **when** you just have schema modules and so pumpkins already knows how import them into the final build. Otherwise you'll need this module to import your custom modules etc. ##### Aliases ``` -main.ts server.ts +server.ts service.ts ``` ### Example Layouts @@ -414,6 +432,10 @@ app.objectType({ }) ``` +### `app.server.start` + +Start the server. If you don't call this pumpkins will. Usually you should not have to call it. Please share your use-case with us if you do! +
# CLI diff --git a/src/framework/app.ts b/src/framework/app.ts index d92b15f60..522ed3bb8 100644 --- a/src/framework/app.ts +++ b/src/framework/app.ts @@ -9,9 +9,13 @@ import { Plugin } from './plugin' import { createPrismaPlugin, isPrismaEnabledSync } from './plugins' import { stripIndent, stripIndents } from 'common-tags' import { sendServerReadySignalToDevModeMaster } from './dev-mode' +import * as singletonChecks from './singleton-checks' const log = pog.sub(__filename) +/** + * The available server options to configure how your app runs its server. + */ type ServerOptions = { port?: number startMessage?: (port: number) => string @@ -19,6 +23,10 @@ type ServerOptions = { introspection?: boolean } +/** + * Create a message suitable for printing to the terminal about the server + * having been booted. + */ const serverStartMessage = (port: number): string => { return stripIndent` Your GraphQL API is now ready @@ -27,6 +35,10 @@ const serverStartMessage = (port: number): string => { ` } +/** + * The default server options. These are merged with whatever you provide. Your + * settings take precedence over these. + */ const defaultServerOptions: Required = { port: typeof process.env.PUMPKINS_PORT === 'string' @@ -113,7 +125,14 @@ export function createApp(appConfig?: { types?: any }): App { intArg, stringArg, server: { + /** + * Start the server. If you do not call this explicitly then pumpkins will + * for you. You should not normally need to call this function yourself. + */ async start(config: ServerOptions = {}): Promise { + // Track the start call so that we can know in entrypoint whether to run + // or not start for the user. + singletonChecks.state.is_was_server_start_called = true // During development we dynamically import all the schema modules // // TODO IDEA we have concept of schema module and schema dir diff --git a/src/framework/singleton-checks.ts b/src/framework/singleton-checks.ts new file mode 100644 index 000000000..8e6fbfa2c --- /dev/null +++ b/src/framework/singleton-checks.ts @@ -0,0 +1,7 @@ +/** + * This module provides state about how the singleton API has been used, useful for some of the special framework features + */ + +export const state = { + is_was_server_start_called: false, +} diff --git a/src/framework/start.ts b/src/framework/start.ts index a89217c45..13a7b07b9 100644 --- a/src/framework/start.ts +++ b/src/framework/start.ts @@ -62,6 +62,22 @@ export function createStartModuleContent(config: StartModuleConfig): string { const { app } = require('pumpkins') app.server.start() ` + // TODO Despite the comment below there are still sometimes reasons to do so + // https://github.com/prisma/pumpkins/issues/141 + output += '\n\n\n' + output += stripIndent` + // Boot the server for the user if they did not alreay do so manually. + // Users should normally not boot the server manually as doing so does not + // bring value to the user's codebase. + + const { app } = require('pumpkins') + const { app } = require('pumpkins') + const singletonChecks = require('pumpkins/dist/framework/singleton-checks') + + if (singletonChecks.state.is_was_server_start_called === false) { + app.server.start() + } + ` return output }