|
| 1 | +--- |
| 2 | +title: Development Mode |
| 3 | +--- |
| 4 | + |
| 5 | +# Development Mode |
| 6 | + |
| 7 | +In development mode, GraphQL.JS can provides additional runtime check appropriate for |
| 8 | +development-time errors, including primarily the erroneous inclusion of multiple |
| 9 | +GraphQL.JS modules. |
| 10 | + |
| 11 | +Unlike earlier versions of GraphQL.JS, by default, development mode is disabled. |
| 12 | +This is to best ensure that production builds do not incur the performance and bundle |
| 13 | +size penalties associated with the additional checks. |
| 14 | + |
| 15 | +Also, unlike earlier versions, development mode is not configured by use of |
| 16 | +environment variables, which are accessed in disparate ways in varying environments. |
| 17 | +In particular, the `NODE_ENV` environment variable now has no effect on triggering |
| 18 | +development mode. Rather, development mode is either enabled: |
| 19 | +1. explicitly, by importing `graphql/dev` prior to other Graphql.JS imports or |
| 20 | +2. implicitly, by setting the 'development' condition, which is possible only in |
| 21 | + environments that support `package.json` conditional exports and custom conditions. |
| 22 | + |
| 23 | +Conditional exports are supported by: Node.js, Deno (canary), Bun, Webpack 5, Rollup |
| 24 | +(via the `node-resolve` plugin), esbuild, and Vite. create-react-app and Next.js |
| 25 | +support conditional exports when using Webpack 5 as their bundler. |
| 26 | + |
| 27 | +Conditional exports are not supported by Deno (current), Webpack 4, Rollup (without |
| 28 | +the `node-resolve` plugin), or swc. create-react-app and Next.js do not support |
| 29 | +conditional exports when using Webpack 4 as their bundler, nor does Next.js yet |
| 30 | +support conditional exports when using Turbopack (see |
| 31 | +https://github.com/vercel/next.js/discussions/78912). |
| 32 | + |
| 33 | +We encourage enabling development mode within the development environments, either |
| 34 | +explicitly or implicitly. Additional development-time checks may also be added in the future. |
| 35 | +For now, the primary check is to ensure that only a single GraphQL.JS module is used. |
| 36 | +First, we will discuss the implications of using multiple GraphQL.JS modules, and then |
| 37 | +we will share additional details for how to enable development mode in various environments. |
| 38 | + |
| 39 | +## Multiple Graphql.JS Modules |
| 40 | + |
| 41 | +Only a single GraphQL.JS can be used within a project. Different GraphQL.JS versions cannot be |
| 42 | +used at the same time since different versions may have different capabilities and behavior. |
| 43 | +The data from one version used in the function from another could produce confusing and spurious |
| 44 | +results. |
| 45 | + |
| 46 | +Duplicate modules of GraphQL.JS of the same version may also fail at runtime, sometimes in |
| 47 | +unexpected ways. This is because GraphQL.JS relies on the identity of the module for |
| 48 | +key features. Most significantly, `instanceof` checks are used throughout GraphQL.JS to |
| 49 | +identify and distinguish between GraphQL schema elements, such as the particular GraphQL type. |
| 50 | +Also, special exported constants like `BREAK` allow library users to manipulate visitor |
| 51 | +behavior, also relying on the module identity. |
| 52 | + |
| 53 | +To ensure that only a single GraphQL.JS module is used, all libraries depending on GraphQL.JS |
| 54 | +should use the appropriate peer dependency mechanism, as provided by their package manager, |
| 55 | +bundler, build tool, or runtime environment. |
| 56 | + |
| 57 | +In development, GraphQL.JS provides a validation check triggered by any use of `instanceof` |
| 58 | +that should catch most cases of multiple GraphQL.js modules being used in the same project. |
| 59 | + |
| 60 | +This additional validation is unnecessary in production, where the GraphQL.js library is |
| 61 | +expected to be have been setup correctly as a single module. So as to avoid the performance |
| 62 | +and bundle size overhead this check entails, it is only included when the `development` |
| 63 | +exports condition is explicitly enabled or when the `graphql/dev` module is |
| 64 | +explicitly imported before any other GraphQL.JS import. |
| 65 | + |
| 66 | +## Enabling Development Mode |
| 67 | + |
| 68 | +### A Catch-All Option: Explicit Enabling of Development Mode |
| 69 | + |
| 70 | +Development mode is activated by importing the `graphql/dev` module before |
| 71 | +any other GraphQL.js import. Introducing a new bootstrapping entrypoint simplifies this |
| 72 | +workflow: |
| 73 | + |
| 74 | +```js |
| 75 | +// bootstrap.js |
| 76 | +import 'graphql/dev'; |
| 77 | +import './path/to/my/original-entry-point.js'; |
| 78 | +``` |
| 79 | + |
| 80 | +The bootstrapping file can be used to enable development mode conditionally: |
| 81 | + |
| 82 | +```js |
| 83 | +import process from 'node:process'; |
| 84 | +// bootstrap.js |
| 85 | +if (process.env.NODE_ENV === 'development') { |
| 86 | + await import('graphql/dev'); |
| 87 | +} |
| 88 | +import './path/to/my/entry.js'; |
| 89 | +``` |
| 90 | + |
| 91 | +The above boiler plate is compatible with Node.js; the exact environment variable and method |
| 92 | +of accessing it depends on the individual environment and desired variable. |
| 93 | + |
| 94 | + |
| 95 | +### Conditional Exports and Implicit Enabling of Development Mode |
| 96 | + |
| 97 | +Depending on your environment, you may be able to use the 'development' condition |
| 98 | +to enable development mode without the need for an explicit import. |
| 99 | + |
| 100 | +### Node.js |
| 101 | + |
| 102 | +In Node.js, the development condition can be enabled by passing the `--conditions=development` |
| 103 | +flag to the Node.js runtime. This can be done within `package.json` scripts: |
| 104 | + |
| 105 | +```json |
| 106 | +{ |
| 107 | + "scripts": { |
| 108 | + "start": "node --conditions=development index.js" |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +Alternatively, this can be included within the `NODE_OPTIONS` environment variable: |
| 114 | + |
| 115 | +```bash |
| 116 | +export NODE_OPTIONS="--conditions=development" |
| 117 | +``` |
| 118 | + |
| 119 | +#### Deno |
| 120 | + |
| 121 | +In Deno, conditional exports are not yet released, but are available within the canary build |
| 122 | +(see https://github.com/denoland/deno/issues/23757) as follows: |
| 123 | + |
| 124 | +```bash |
| 125 | +deno run --unstable-node-conditions=development main.js |
| 126 | +``` |
| 127 | + |
| 128 | +#### Bun |
| 129 | + |
| 130 | +In Bun, you can enable the development condition by passing the `--conditions=development` flag |
| 131 | +when running your script: |
| 132 | + |
| 133 | +```bash |
| 134 | +bun --conditions=development main.js |
| 135 | +``` |
| 136 | + |
| 137 | +#### Webpack |
| 138 | + |
| 139 | +Webpack 5 supports the 'development' condition natively and requires no additional configuration. |
| 140 | + |
| 141 | +### Rollup |
| 142 | + |
| 143 | +Rollup supports the 'development' condition only when using the `@rollup/plugin-node-resolve` plugin. |
| 144 | + |
| 145 | +```ts |
| 146 | +//rollup.config.js |
| 147 | +import resolve from '@rollup/plugin-node-resolve'; |
| 148 | + |
| 149 | +export default { |
| 150 | + plugins: [ |
| 151 | + resolve({ |
| 152 | + exportConditions: ['development'], |
| 153 | + }) |
| 154 | + ] |
| 155 | +}; |
| 156 | +``` |
| 157 | + |
| 158 | +### esbuild |
| 159 | + |
| 160 | +When using esbuild, you can enable the 'development' condition by setting the `--conditions=development` |
| 161 | +flag in your build command: |
| 162 | + |
| 163 | +```bash |
| 164 | +esbuild --conditions=development entrypoint.js |
| 165 | +``` |
| 166 | + |
| 167 | +Note that setting any custom conditions will drop the default 'module' condition (used to avoid the dual |
| 168 | +package hazard), so you may need to use: |
| 169 | + |
| 170 | +```bash |
| 171 | +esbuild --conditions=development,module entrypoint.js |
| 172 | +``` |
| 173 | + |
| 174 | +See further discussion within the [esbuild documentation](https://esbuild.github.io/api/#conditions) for |
| 175 | +more details. |
| 176 | + |
| 177 | +### Vite |
| 178 | + |
| 179 | +Vite supports the 'development' condition natively and requires no additional configuration. |
| 180 | + |
| 181 | +### Next.js |
| 182 | + |
| 183 | +When using Webpack 5 as its bundler, Next.js supports the 'development' condition natively |
| 184 | +and require no additional configuration. When using Webpack 4 or Turbopack, development mode |
| 185 | +must be enabled explicitly. |
| 186 | + |
| 187 | +### create-react-app |
| 188 | + |
| 189 | +When using Webpack 5 as its bundler, create-react-app support the 'development' condition |
| 190 | +natively and requires no additional configuration. When using Webpack 4, development mode |
| 191 | +must be enabled explicitly. |
0 commit comments