Skip to content

Commit 9c563fa

Browse files
committed
Throw error at startup when file uploads are enabled on Node.js < 8.5.0.
Due to changes in the third-party `graphql-upload` package which Apollo Server utilizes to implement out-of-the-box file upload functionality, we must drop support for file uploads in versions of the Node.js engine prior to v8.5.0. Since file uploads are supported by default in Apollo Server 2.x, and there is an explicit dependency on `graphql-upload`, we must prevent users who are affected by this mid-major-release deprecation by being surprised by the sudden lack of upload support. By `throw`-ing an error at server startup for affected users, we certainly are breaking a semantic versioning agreement for these users, however with a relatively simple ergonomic (setting `uploads: false`) we allow those users who are NOT utilizing file uploads (as we believe is the case with a majority) to continue using their version of Node.js until it reaches the end of its supported lifetime (as dictated by its Long Term Support agreement with the Node.js Foundation). If we did not `throw` the error at server start-up, those affected may not notice since they may update and start their updated server without noticing the impending chance of failure when someone tries updating! Apollo Server 2.x has attempted to maintain full compatibility with versions of Node.js which are still under Long Term Support agreements with the Node.js Foundation. While this continues to mostly be true, file uploads are an exception which we've now had to make. Third-party open-source projects must absolutely do what's best for their project. From an architecture standpoint, I suspect that we (the designers behind Apollo Server) are mostly to blame for this. Namely, it's unfortunate that we had made such an incredibly coupled integration with a third-party package that we restricted our users from incrementally adopting the changes (and new/improved functionality) of, in this particular case, the `graphql-upload` package. I hope we can take better care with decisions like this in the future! Lastly, this commit also adds documentation to help those affected.
1 parent 0c2fbc3 commit 9c563fa

File tree

16 files changed

+159
-26
lines changed

16 files changed

+159
-26
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
### vNEXT
44

5+
- **BREAKING FOR NODE.JS <= 8.5.0 ONLY**: To continue using Apollo Server 2.x in versions of Node.js prior to v8.5.0, file uploads must be disabled by setting `uploads: false` on the `ApolloServer` constructor options. Without explicit disabling file-uploads, the server will `throw` at launch (with instructions and a link to our documentation).
6+
7+
This early deprecation is due to changes in the third-party `graphql-upload` package which Apollo Server utilizes to implement out-of-the-box file upload functionality. While, in general, Apollo Server 2.x aims to support all Node.js versions which were under an LTS policy at the time of its release, we felt this required an exception. By `throw`-ing when `uploads` is not explicitly set to `false`, we aim to make it clear immediately (rather than surprisingly) that this deprecation has taken effect.
8+
9+
While Node.js 6.x is covered by a [Long Term Support agreement by the Node.js Foundation](https://github.com/nodejs/Release#release-schedule) until April 2019, there are substantial performance (e.g. [V8](https://v8.dev/) improvements) and language changes (e.g. "modern" ECMAScript support) offered by newer Node.js engines (e.g. 8.x, 10.x). We encourage _all users_ of Apollo Server to update to newer LTS versions of Node.js prior to the "end-of-life" dates for their current server version.
10+
11+
**We intend to drop support for Node.js 6.x in the next major version of Apollo Server.**
12+
513
### v2.2.5
614

715
- Follow-up on the update to `graphql-playground-html` in previous release by also bumping the minor version of the `graphql-playground-react` dependency to `1.7.10` — which is the version requested from the from the CDN bundle by `graphql-playground-html`. [PR #2037](https://github.com/apollographql/apollo-server/pull/2037)

docs/_config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ sidebar_categories:
4444
Migration:
4545
- migration-two-dot
4646
- migration-engine
47+
- migration-file-uploads
4748

4849
github_repo: apollographql/apollo-server
4950
content_root: docs/source
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: File uploads in Node.js < v8.5.0
3+
---
4+
5+
File uploads are supported in Apollo Server 2.x through the third-party [`graphql-upload`](https://npm.im/graphql-upload/) package. While Apollo Server 2.x aims to support Node.js LTS versions prior to v8.5.0, the `graphql-upload` project no longer supports file uploads on versions of Node.js prior to v8.5.0 due to changes in the underlying architecture.
6+
7+
While Node.js versions prior to v8.5.0 are still under [_Long Term Support_ (LTS) agreements](https://github.com/nodejs/Release#release-schedule) from the Node.js Foundation, **we encourage _all users_ of Apollo Server to update to newer LTS versions of Node.js** prior to the "end-of-life" dates for their current server version.
8+
9+
For example, while Node.js 6.x is covered by Long Term Support until April 2019, there are substantial performance (e.g. [V8](https://v8.dev/) improvements) and language changes (e.g. "modern" ECMAScript support) offered by newer Node.js engines (e.g. 8.x, 10.x). Switching to newer Long Term Support versions of Node.js comes with long-term benefits; Node.js 10.x LTS releases are covered by the Node.js Foundation through April 2021.
10+
11+
Since file upload support for Node.js versions prior to v8.5.0 is no longer offered by `graphql-upload`, users of those versions must must disable file uploads to continue using newer Apollo Server 2.x versions.
12+
13+
**To disable file uploads and continue using Apollo Server 2.x on Node.js 6.x**, add the `uploads: false` setting to the options when constructing the server. For example:
14+
15+
```js
16+
const { typeDefs, resolvers } = require('./anOutsideDependency');
17+
const server = new ApolloServer({
18+
/* Existing Apollo Server settings — e.g. type definitions */
19+
typeDefs,
20+
resolvers,
21+
22+
/* Add this line to disable upload support! */
23+
uploads: false,
24+
25+
/* ... other Apollo Server settings ... */
26+
})
27+
```
28+
29+
For additional assistance, please [search for existing issues](https://github.com/apollographql/apollo-server/issues?q=uploads) or file a [new issue](https://github.com/apollographql/apollo-server/issues/new) on the Apollo Server GitHub repository.

package-lock.json

Lines changed: 1 addition & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/apollo-server-core/src/ApolloServer.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { GraphQLExtension } from 'graphql-extensions';
1414
import { EngineReportingAgent } from 'apollo-engine-reporting';
1515
import { InMemoryLRUCache } from 'apollo-server-caching';
1616
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
17+
import supportsUploadsInNode from './utils/supportsUploadsInNode';
1718

1819
import {
1920
SubscriptionServer,
@@ -201,6 +202,14 @@ export class ApolloServerBase {
201202

202203
if (uploads !== false) {
203204
if (this.supportsUploads()) {
205+
if (!supportsUploadsInNode) {
206+
printNodeFileUploadsMessage();
207+
throw new Error(
208+
'`graphql-upload` is no longer supported on Node.js < v8.5.0. ' +
209+
'See https://bit.ly/gql-upload-node-6.',
210+
);
211+
}
212+
204213
if (uploads === true || typeof uploads === 'undefined') {
205214
this.uploadsConfig = {};
206215
} else {
@@ -540,3 +549,32 @@ export class ApolloServerBase {
540549
return processGraphQLRequest(options, requestCtx);
541550
}
542551
}
552+
553+
function printNodeFileUploadsMessage() {
554+
console.error(
555+
[
556+
'*****************************************************************',
557+
'* *',
558+
'* ERROR! Manual intervention is necessary for Node.js < v8.5.0! *',
559+
'* *',
560+
'*****************************************************************',
561+
'',
562+
'The third-party `graphql-upload` package, which is used to implement',
563+
'file uploads in Apollo Server 2.x, no longer supports Node.js LTS',
564+
'versions prior to Node.js v8.5.0.',
565+
'',
566+
'If this server DOES NOT USE file uploads, it is necessary to add:',
567+
'',
568+
' uploads: false,',
569+
'',
570+
'to the options for Apollo Server and re-deploy to disable file uploads',
571+
'and continue using this version of Node.js.',
572+
'',
573+
'Deployments which need file upload capabilities should update to',
574+
'Node.js >= v8.5.0 to continue using newer Apollo Server versions.',
575+
'',
576+
'For more information, see https://bit.ly/gql-upload-node-6.',
577+
'',
578+
].join('\n'),
579+
);
580+
}

packages/apollo-server-core/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ export const gql: (
4141
...substitutions: any[]
4242
) => DocumentNode = gqlTag;
4343

44+
import supportsUploadsInNode from './utils/supportsUploadsInNode';
4445
import { GraphQLScalarType } from 'graphql';
45-
import { GraphQLUpload as UploadScalar } from 'graphql-upload';
46-
export const GraphQLUpload = UploadScalar as GraphQLScalarType;
46+
export { default as processFileUploads } from './processFileUploads';
47+
48+
// This is a conditional export intended to avoid traversing the
49+
// entire module tree of `graphql-upload`. This only defined if the
50+
// version of Node.js is >= 8.5.0 since those are the only Node.js versions
51+
// which are supported by `graphql-upload@8`. Since the source of
52+
// `graphql-upload` is not transpiled for older targets (in fact, it includes
53+
// experimental ECMAScript modules), this conditional export is necessary
54+
// to avoid modern ECMAScript from failing to parse by versions of Node.js
55+
// which don't support it (yet — eg. Node.js 6 and async/await).
56+
export const GraphQLUpload = supportsUploadsInNode
57+
? (require('graphql-upload').GraphQLUpload as GraphQLScalarType)
58+
: undefined;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import supportsUploadsInNode from './utils/supportsUploadsInNode';
2+
3+
// We'll memoize this function once at module load time since it should never
4+
// change during runtime. In the event that we're using a version of Node.js
5+
// less than 8.5.0, we'll
6+
const processFileUploads:
7+
| typeof import('graphql-upload').processRequest
8+
| undefined = (() => {
9+
if (supportsUploadsInNode) {
10+
return require('graphql-upload')
11+
.processRequest as typeof import('graphql-upload').processRequest;
12+
}
13+
return undefined;
14+
})();
15+
16+
export default processFileUploads;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const supportsUploadsInNode = (() => {
2+
if (
3+
process &&
4+
process.release &&
5+
process.release.name === 'node' &&
6+
process.versions &&
7+
typeof process.versions.node === 'string'
8+
) {
9+
const [nodeMajor, nodeMinor] = process.versions.node
10+
.split('.', 2)
11+
.map(segment => parseInt(segment, 10));
12+
13+
if (nodeMajor < 8 || (nodeMajor === 8 && nodeMinor < 5)) {
14+
return false;
15+
}
16+
}
17+
18+
return true;
19+
})();
20+
21+
export default supportsUploadsInNode;

packages/apollo-server-express/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"cors": "^2.8.4",
3838
"graphql-subscriptions": "^1.0.0",
3939
"graphql-tools": "^4.0.0",
40-
"graphql-upload": "^8.0.0",
4140
"type-is": "^1.6.16"
4241
},
4342
"devDependencies": {

packages/apollo-server-express/src/ApolloServer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import {
1010
FileUploadOptions,
1111
ApolloServerBase,
1212
formatApolloErrors,
13+
processFileUploads,
1314
} from 'apollo-server-core';
1415
import accepts from 'accepts';
1516
import typeis from 'type-is';
1617

1718
import { graphqlExpress } from './expressApollo';
1819

19-
import { processRequest as processFileUploads } from 'graphql-upload';
20-
2120
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
2221

2322
export interface ServerRegistration {
@@ -44,7 +43,10 @@ const fileUploadMiddleware = (
4443
next: express.NextFunction,
4544
) => {
4645
// Note: we use typeis directly instead of via req.is for connect support.
47-
if (typeis(req, ['multipart/form-data'])) {
46+
if (
47+
typeof processFileUploads === 'function' &&
48+
typeis(req, ['multipart/form-data'])
49+
) {
4850
processFileUploads(req, res, uploadsConfig)
4951
.then(body => {
5052
req.body = body;
@@ -134,7 +136,7 @@ export class ApolloServer extends ApolloServerBase {
134136
}
135137

136138
let uploadsMiddleware;
137-
if (this.uploadsConfig) {
139+
if (this.uploadsConfig && typeof processFileUploads === 'function') {
138140
uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
139141
}
140142

0 commit comments

Comments
 (0)