import { settings } from 'nexus'

Guideissues (features | bugs)

The Settings component is designed for centrally managing your app's configuration. All components expose their options through the settings component. If you are new to this component then you may be interested in our configuration management guide.

change

1(settingsInput: {
2 server?: {
3 port?: number
4 host?: string
5 path?: string
6 playground?: boolean | PlaygroundSettings
7 cors?: boolean | CorsSettings
8 }
9 schema?: {
10 nullable?: {
11 outputs?: boolean
12 inputs?: boolean
13 }
14 connections?: Record<string, ConnectionConfig>
15 generateGraphQLSDLFile?: false | string
16 rootTypingsGlobPattern?: string
17 }
18 logger?: {
19 filter?:
20 | string
21 | {
22 level?: Level
23 pattern?: string
24 }
25 pretty?:
26 | boolean
27 | {
28 enabled: boolean
29 timeDiff: boolean
30 color: boolean
31 levelLabel: boolean
32 }
33 }
34}) => Settings

Change your app's current settings. This is an additive API meaning it can be called multiple times, each time merging with the previous settings.

server.playground

1boolean | PlaygroundSettings
  • Passing boolean is shorthand for { enabled: true | false }

server.playground.enabled

Should the app expose a GraphQL Playground to clients?

Default

true in dev, false otherwise.

server.playground.settings

1{
2 'request.credentials'?: string;
3 'tracing.hideTracingResponse'?: boolean;
4 'queryPlan.hideQueryPlanResponse'?: boolean;
5 'editor.theme'?: Theme;
6 'editor.cursorShape'?: CursorShape;
7 'editor.reuseHeaders'?: boolean;
8 'editor.fontSize'?: number;
9 'editor.fontFamily'?: string;
10}

Configure the Playground UI settings

Default

1{
2 'request.credentials': 'omit',
3 'tracing.hideTracingResponse': true,
4 'queryPlan.hideQueryPlanResponse': true,
5 'editor.theme': 'dark',
6 'editor.cursorShape': 'line',
7 'editor.reuseHeaders': true,
8 'editor.fontSize': 14,
9 'editor.fontFamily': `'Source Code Pro', 'Consolas', 'Inconsolata', 'Droid Sans Mono', 'Monaco', monospace`,
10}

server.port

1number

The port the server should listen on.

Default

  • Is NEXUS_PORT environment variable set? Then that.
  • Is PORT environment variable set? Then that.
  • Is NODE_ENV environment variable production? Then 80
  • Else 4000

server.host

1string

The host the server should listen on.

Default

server.path

1string

The path on which the GraphQL API should be served.

Default

/graphql

server.cors

1boolean | {
2 /**
3 * Enable or disable CORS.
4 *
5 * @default true
6 */
7 enabled?: boolean
8 /**
9 * Configures the Access-Control-Allow-Origin CORS header. Possible values:
10 *
11 * Boolean - set origin to true to reflect the request origin, as defined by req.header('Origin'), or set it to false to disable CORS.
12 *
13 * String - set origin to a specific origin. For example if you set it to "http://example.com" only requests from "http://example.com" will be allowed.
14 *
15 * RegExp - set origin to a regular expression pattern which will be used to test the request origin. If it's a match, the request origin will be reflected. For example the pattern /example\.com$/ will reflect any request that is coming from an origin ending with "example.com".
16 *
17 * Array - set origin to an array of valid origins. Each origin can be a String or a RegExp. For example ["http://example1.com", /\.example2\.com$/] will accept any request from "http://example1.com" or from a subdomain of "example2.com".
18 *
19 * Function - set origin to a function implementing some custom logic. The function takes the request origin as the first parameter and a callback (called as callback(err, origin), where origin is a non-function value of the origin option) as the second.
20 *
21 */
22 origin?: OriginalCorsOption['origin'] // TODO: Improve function interface with promise-based callback
23 /**
24 * Configures the Access-Control-Allow-Methods CORS header.
25 *
26 * @example ['GET', 'PUT', 'POST']
27 */
28 methods?: string | HTTPMethods[]
29 /**
30 * Configures the Access-Control-Allow-Headers CORS header.
31 *
32 * If not specified, defaults to reflecting the headers specified in the request's Access-Control-Request-Headers header.
33 *
34 * @example ['Content-Type', 'Authorization']
35 */
36 allowedHeaders?: string | string[]
37 /**
38 * Configures the Access-Control-Expose-Headers CORS header.
39 *
40 * If not specified, no custom headers are exposed.
41 *
42 * @example ['Content-Range', 'X-Content-Range']
43 */
44 exposedHeaders?: string | string[]
45 /**
46 * Configures the Access-Control-Allow-Credentials CORS header.
47 *
48 * Set to true to pass the header, otherwise it is omitted.
49 */
50 credentials?: boolean
51 /**
52 * Configures the Access-Control-Max-Age CORS header.
53 *
54 * Set to an integer to pass the header, otherwise it is omitted.
55 */
56 maxAge?: number
57 /**
58 * Pass the CORS preflight response to the next handler.
59 */
60 preflightContinue?: boolean
61 /**
62 * Provides a status code to use for successful OPTIONS requests, since some legacy browsers (IE11, various SmartTVs) choke on 204.
63 */
64 optionsSuccessStatus?: number
65}

Enable CORS for your server.

When true is passed, the default config is the following:

1{
2 "origin": "*",
3 "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
4 "preflightContinue": false,
5 "optionsSuccessStatus": 204
6}

Default

false

schema.nullable.inputs

1boolean

Should passing arguments be optional for clients by default?

Default

true

schema.nullable.outputs

1boolean

Should the data requested by clients not be guaranteed to be returned by default?

Default

true

schema.generateGraphQLSDLFile

1false | string

Should a GraphQL SDL file be generated when the app is built and to where?

A relative path is interpreted as being relative to the project directory. Intermediary folders are created automatically if they do not exist already.

Default

api.graphql

schema.rootTypingsGlobPattern

1string

A glob pattern which will be used to find the files from which to extract the backing types used in the rootTyping option of schema.(objectType|interfaceType|unionType|enumType)

Default

The default glob pattern used id ./**/*.ts

schema.connections

1Record<string, ConnectionConfig>

🚧 Work in progress.

Example of adding a specialized kind of connection field builder
1import { settings } from 'nexus'
2
3settings.change({
4 schema: {
5 connections: {
6 analyticsConnection: {
7 typePrefix: 'Analytics',
8 extendConnection: {
9 totalCount: { type: 'Int' },
10 avgDuration: { type: 'Int' },
11 },
12 },
13 },
14 },
15})
Example of including a nodes field like GitHub API globally

If you want to include a nodes field, which includes the nodes of the connection flattened into an array similar to how GitHub does in their GraphQL API, set schema setting includeNodesField to true.

1import { settings } from 'nexus'
2
3settings.change({
4 schema: {
5 connections: {
6 default: {
7 includeNodesField: true,
8 },
9 },
10 },
11})
1query IncludeNodesFieldExample {
2 users(first: 10) {
3 nodes {
4 id
5 }
6 pageInfo {
7 endCursor
8 hasNextPage
9 }
10 }
11}

logger.filter

1string | { level?: Level, pattern?: string }

string form is shorthand for logger.filter.pattern. See below for object form.

logger.filter.level

Set the default filter level. This setting is used whenever a filter pattern does not specify the level. For example filter patterns like * app and app:* would use this setting but filter patterns like *@* app@info+ app:router@trace would not.

When a log falls below the set level then it is not sent to the logger output (typically stdout).

Default

First found in the following order

  1. LOG_LEVEL env var
  2. 'info' if NODE_ENV=production
  3. 'debug'

logger.filter.pattern

1string

Filter logs by path and/or level.

Default

'app:*, nexus:*@info+, *@warn+'

Examples
1 All logs at...
2
3 Path
4 app app path at default level
5 app:router app:router path at default level
6
7 List
8 app,nexus app and nexus paths at default level
9
10 Path Wildcard
11 * any path at default level
12 app:* app path plus descendants at default level
13 app::* app path descendants at default level
14
15 Negation
16 !app any path at any level _except_ those at app path at default level
17 !* no path (meaning, nothing will be logged)
18
19 Removal
20 *,!app any path at default level _except_ logs at app path at default level
21 *,!*@2- any path _except_ those at debug level or lower
22 app,!app@4 app path at default level _except_ those at warn level
23
24 Levels
25 *@info all paths at info level
26 *@error- all paths at error level or lower
27 *@debug+ all paths at debug level or higher
28 *@3 all paths at info level
29 *@4- all paths at error level or lower
30 *@2+ all paths at debug level or higher
31 app:*@2- app path plus descendants at debug level or lower
32 app::*@2+ app path descendants at debug level or higher
33
34 Level Wildcard
35 app@* app path at all levels
36 *@* all paths at all levels
37
38 Explicit Root
39 . root path at default level
40 .@info root path at info level
41 .:app Same as "app"
42 .:* Same as "*"

logger.pretty

Shorthand for logger.pretty.enabled.

logger.pretty.enabled

Should logs be logged with rich formatting etc. (true), or as JSON (false)?

Default

  • Is LOG_PRETTY environment variable true? Then true.
  • Is LOG_PRETTY environment variable false? Then false.
  • Is process.stdout attached to a TTY? Then true
Example of what it looks like
1LOG_DEMO=true nexus dev
1 -----------
2 LOGGER DEMO
3 -----------
4 4 ✕ root:foo -- lib: /see/
5 0 ■ root:foo
6 | har { mar: 'tek' }
7 | jar [
8 | 1, 2, 3, 4, 4, 5, 6,
9 | 6, 7, 9, 1, 2, 4, 5,
10 | 6, 7, 3, 6, 5, 4
11 | ]
12 | kio [Object: null prototype] [foo] {}
13 1 ▲ root:foo -- bleep: [ 1, '2', true ]
14 0 ● root:foo
15 1 ○ root:foo
16 | results [
17 | { userId: 1, id: 1, title: 'delectus aut autem', completed: false },
18 | { userId: 1, id: 2, title: 'quis ut nam facilis et officia qui', completed: false },
19 | { userId: 1, id: 3, title: 'fugiat veniam minus', completed: false },
20 | { userId: 1, id: 4, title: 'et porro tempora', completed: true },
21 | {
22 | userId: 1,
23 | id: 5,
24 | title: 'laboriosam mollitia et enim quasi adipisci quia provident illum',
25 | completed: false
26 | }
27 | ]
28 | tri 'wiz'
29 | on false
30 0 ○ root:foo -- foo: 'bar'
31 0 — root:foo -- a: 1 b: 2 c: 'three'
32 -----------

logger.pretty.color

Should logs have color?

Default

true

logger.pretty.timeDiff

Should a time delta between each log be shown in the gutter?

Default

true

logger.pretty.levelLabel

Should the label of the level be shown in the gutter?

Default

false

Example
1import { settings } from 'nexus'
2
3settings.change({
4 server: {
5 port: 9876,
6 },
7})

current

Type
1SettingsData

A reference to the current settings object.

original

1SettingsData

A reference to the original settings object.

Type Glossary

Level

1'trace' | 'debug' | 'info' | 'warn' | 'critical' | 'fatal'

SettingsData

🚧 Work in progress.

Edit this page on Github