Merge pull request #2054 from apollographql/abernix/throw-when-uploads-on-pre-node-8.5

Upgrade to graphql-upload v8, dropping upload support for Node.js v6.
This commit is contained in:
Jesse Rosenberger 2018-12-04 14:30:26 +02:00 committed by GitHub
commit b5c516c04d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 307 additions and 154 deletions

View file

@ -1,6 +1,16 @@
# Changelog
### vNEXT
### v2.3.0-alpha
- **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 explicitly disabling file-uploads, the server will `throw` at launch (with instructions and a link to our documentation).
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.
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.
**We intend to drop support for Node.js 6.x in the next major version of Apollo Server.**
### v2.2.7-beta.0
### v2.2.7-alpha.0

View file

@ -44,6 +44,7 @@ sidebar_categories:
Migration:
- migration-two-dot
- migration-engine
- migration-file-uploads
github_repo: apollographql/apollo-server
content_root: docs/source

View file

@ -0,0 +1,29 @@
---
title: File uploads in Node.js < v8.5.0
---
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.
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.
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.
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 disable file uploads to continue using newer Apollo Server 2.x versions.
**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:
```js
const { typeDefs, resolvers } = require('./anOutsideDependency');
const server = new ApolloServer({
/* Existing Apollo Server settings — e.g. type definitions */
typeDefs,
resolvers,
/* Add this line to disable upload support! */
uploads: false,
/* ... other Apollo Server settings ... */
})
```
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.

View file

@ -246,7 +246,7 @@ const resolvers = {
Mutation: {
singleUpload: (parent, args) => {
return args.file.then(file => {
//Contents of Upload scalar: https://github.com/jaydenseric/apollo-upload-server#upload-scalar
//Contents of Upload scalar: https://github.com/jaydenseric/graphql-upload#class-graphqlupload
//file.stream is a node stream that contains the contents of the uploaded file
//node stream api: https://nodejs.org/api/stream.html
return file;

105
package-lock.json generated
View file

@ -4,21 +4,27 @@
"lockfileVersion": 1,
"dependencies": {
"@apollographql/apollo-tools": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/@apollographql/apollo-tools/-/apollo-tools-0.2.6.tgz",
"integrity": "sha512-IKn35EzAHFQw4x8THux+fkVLkFNs8HDzIgKqcU5ZMfkDt3MOa1nfMbGVoVh2YdvpIjPtyUR3kvAAVQwmRPRjAQ==",
"version": "0.2.8",
"resolved": "https://registry.npmjs.org/@apollographql/apollo-tools/-/apollo-tools-0.2.8.tgz",
"integrity": "sha512-A7FTUigtpGCFBaLT1ILicdjM6pZ7LQNw7Vgos0t4aLYtvlKO/L1nMi/NO7bPypzZaJSToTgcxHJPRydP1Md+Kw==",
"requires": {
"apollo-env": "0.2.3"
}
},
"@apollographql/apollo-upload-server": {
"version": "5.0.3",
"resolved": "https://registry.npmjs.org/@apollographql/apollo-upload-server/-/apollo-upload-server-5.0.3.tgz",
"integrity": "sha512-tGAp3ULNyoA8b5o9LsU2Lq6SwgVPUOKAqKywu2liEtTvrFSGPrObwanhYwArq3GPeOqp2bi+JknSJCIU3oQN1Q==",
"requires": {
"@babel/runtime-corejs2": "^7.0.0-rc.1",
"busboy": "^0.2.14",
"object-path": "^0.11.4"
"apollo-env": "0.2.5"
},
"dependencies": {
"apollo-env": {
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/apollo-env/-/apollo-env-0.2.5.tgz",
"integrity": "sha512-Gc7TEbwCl7jJVutnn8TWfzNSkrrqyoo0DP92BQJFU9pZbJhpidoXf2Sw1YwOJl82rRKH3ujM3C8vdZLOgpFcFA==",
"requires": {
"core-js": "^3.0.0-beta.3",
"node-fetch": "^2.2.0"
}
},
"core-js": {
"version": "3.0.0-beta.3",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-3.0.0-beta.3.tgz",
"integrity": "sha512-kM/OfrnMThP5PwGAj5HhQLdjUqzjrllqN2EVnk/X9qrLsfYjR2hzZ+E/8CzH0xuosexZtqMTLQrk//BULrBj9w=="
}
}
},
"@apollographql/graphql-playground-html": {
@ -63,15 +69,6 @@
"regenerator-runtime": "^0.12.0"
}
},
"@babel/runtime-corejs2": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/@babel/runtime-corejs2/-/runtime-corejs2-7.1.2.tgz",
"integrity": "sha512-drxaPByExlcRDKW4ZLubUO4ZkI8/8ax9k9wve1aEthdLKFzjB7XRkOQ0xoTIWGxqdDnWDElkjYq77bt7yrcYJQ==",
"requires": {
"core-js": "^2.5.7",
"regenerator-runtime": "^0.12.0"
}
},
"@iamstarkov/listr-update-renderer": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/@iamstarkov/listr-update-renderer/-/listr-update-renderer-0.4.1.tgz",
@ -2039,22 +2036,6 @@
"protobufjs": "^6.8.6"
}
},
"apollo-env": {
"version": "0.2.3",
"resolved": "https://registry.npmjs.org/apollo-env/-/apollo-env-0.2.3.tgz",
"integrity": "sha512-7hRWPG8tWQNXCNrZsOMqxtkHGqhTzFgsw7RpFDyC1xgcZvVFCJvthWgWO07EhcaHhRqvrPxmicLnoTw2e/iCsA==",
"requires": {
"core-js": "^3.0.0-beta.3",
"node-fetch": "^2.2.0"
},
"dependencies": {
"core-js": {
"version": "3.0.0-beta.3",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-3.0.0-beta.3.tgz",
"integrity": "sha512-kM/OfrnMThP5PwGAj5HhQLdjUqzjrllqN2EVnk/X9qrLsfYjR2hzZ+E/8CzH0xuosexZtqMTLQrk//BULrBj9w=="
}
}
},
"apollo-fetch": {
"version": "0.7.0",
"resolved": "https://registry.npmjs.org/apollo-fetch/-/apollo-fetch-0.7.0.tgz",
@ -2177,7 +2158,6 @@
"version": "file:packages/apollo-server-core",
"requires": {
"@apollographql/apollo-tools": "^0.2.6",
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"@types/ws": "^6.0.0",
"apollo-cache-control": "file:packages/apollo-cache-control",
@ -2192,10 +2172,34 @@
"graphql-subscriptions": "^1.0.0",
"graphql-tag": "^2.9.2",
"graphql-tools": "^4.0.0",
"graphql-upload": "^8.0.2",
"json-stable-stringify": "^1.0.1",
"lodash": "^4.17.10",
"subscriptions-transport-ws": "^0.9.11",
"ws": "^6.0.0"
},
"dependencies": {
"graphql-upload": {
"version": "8.0.2",
"bundled": true,
"requires": {
"busboy": "^0.2.14",
"fs-capacitor": "^1.0.0",
"http-errors": "^1.7.1",
"object-path": "^0.11.4"
}
},
"http-errors": {
"version": "1.7.1",
"bundled": true,
"requires": {
"depd": "~1.1.2",
"inherits": "2.0.3",
"setprototypeof": "1.1.0",
"statuses": ">= 1.5.0 < 2",
"toidentifier": "1.0.0"
}
}
}
},
"apollo-server-env": {
@ -2211,7 +2215,6 @@
"apollo-server-express": {
"version": "file:packages/apollo-server-express",
"requires": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"@types/accepts": "^1.3.5",
"@types/body-parser": "1.17.0",
@ -2235,19 +2238,12 @@
"apollo-server-hapi": {
"version": "file:packages/apollo-server-hapi",
"requires": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"accept": "^3.0.2",
"apollo-server-core": "file:packages/apollo-server-core",
"boom": "^7.1.0",
"graphql-subscriptions": "^1.0.0",
"graphql-tools": "^4.0.0"
},
"dependencies": {
"@apollographql/graphql-playground-html": {
"version": "1.6.6",
"bundled": true
}
}
},
"apollo-server-integration-testsuite": {
@ -2259,7 +2255,6 @@
"apollo-server-koa": {
"version": "file:packages/apollo-server-koa",
"requires": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"@koa/cors": "^2.2.1",
"@types/accepts": "^1.3.5",
@ -2302,7 +2297,6 @@
"apollo-server-micro": {
"version": "file:packages/apollo-server-micro",
"requires": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"accept": "^3.0.2",
"apollo-server-core": "file:packages/apollo-server-core",
@ -3849,7 +3843,8 @@
"core-js": {
"version": "2.5.7",
"resolved": "https://registry.npmjs.org/core-js/-/core-js-2.5.7.tgz",
"integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw=="
"integrity": "sha512-RszJCAxg/PP6uzXVXL6BsxSXx/B05oJAQ2vkJRjyjrEcNVycaqOmNb5OTxZPE3xa5gwZduqza6L9JOCenh/Ecw==",
"dev": true
},
"core-util-is": {
"version": "1.0.2",
@ -5295,6 +5290,11 @@
}
}
},
"fs-capacitor": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/fs-capacitor/-/fs-capacitor-1.0.1.tgz",
"integrity": "sha512-XdZK0Q78WP29Vm3FGgJRhRhrBm51PagovzWtW2kJ3Q6cYJbGtZqWSGTSPwvtEkyjIirFd7b8Yes/dpOYjt4RRQ=="
},
"fs-extra": {
"version": "7.0.1",
"resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-7.0.1.tgz",
@ -11023,7 +11023,8 @@
"regenerator-runtime": {
"version": "0.12.1",
"resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.12.1.tgz",
"integrity": "sha512-odxIc1/vDlo4iZcfXqRYFj0vpXFNoGdKMAUieAlFYO6m/nl5e9KR/beGf41z4a1FI+aQgtjhuaSlDxQ0hmkrHg=="
"integrity": "sha512-odxIc1/vDlo4iZcfXqRYFj0vpXFNoGdKMAUieAlFYO6m/nl5e9KR/beGf41z4a1FI+aQgtjhuaSlDxQ0hmkrHg==",
"dev": true
},
"regex-cache": {
"version": "0.4.4",

View file

@ -25,7 +25,6 @@
},
"dependencies": {
"@apollographql/apollo-tools": "^0.2.6",
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"@types/ws": "^6.0.0",
"apollo-cache-control": "file:../apollo-cache-control",
@ -40,6 +39,7 @@
"graphql-subscriptions": "^1.0.0",
"graphql-tag": "^2.9.2",
"graphql-tools": "^4.0.0",
"graphql-upload": "^8.0.2",
"json-stable-stringify": "^1.0.1",
"lodash": "^4.17.10",
"subscriptions-transport-ws": "^0.9.11",

View file

@ -14,6 +14,7 @@ import { GraphQLExtension } from 'graphql-extensions';
import { EngineReportingAgent } from 'apollo-engine-reporting';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import supportsUploadsInNode from './utils/supportsUploadsInNode';
import {
SubscriptionServer,
@ -88,6 +89,9 @@ function getEngineServiceId(engine: Config['engine']): string | undefined {
return;
}
const forbidUploadsForTesting =
process && process.env.NODE_ENV === 'test' && !supportsUploadsInNode;
export class ApolloServerBase {
public subscriptionsPath?: string;
public graphqlPath: string = '/graphql';
@ -199,8 +203,16 @@ export class ApolloServerBase {
this.requestOptions = requestOptions as GraphQLOptions;
this.context = context;
if (uploads !== false) {
if (uploads !== false && !forbidUploadsForTesting) {
if (this.supportsUploads()) {
if (!supportsUploadsInNode) {
printNodeFileUploadsMessage();
throw new Error(
'`graphql-upload` is no longer supported on Node.js < v8.5.0. ' +
'See https://bit.ly/gql-upload-node-6.',
);
}
if (uploads === true || typeof uploads === 'undefined') {
this.uploadsConfig = {};
} else {
@ -249,9 +261,7 @@ export class ApolloServerBase {
);
if (this.uploadsConfig) {
const {
GraphQLUpload,
} = require('@apollographql/apollo-upload-server');
const { GraphQLUpload } = require('graphql-upload');
if (resolvers && !resolvers.Upload) {
resolvers.Upload = GraphQLUpload;
}
@ -542,3 +552,32 @@ export class ApolloServerBase {
return processGraphQLRequest(options, requestCtx);
}
}
function printNodeFileUploadsMessage() {
console.error(
[
'*****************************************************************',
'* *',
'* ERROR! Manual intervention is necessary for Node.js < v8.5.0! *',
'* *',
'*****************************************************************',
'',
'The third-party `graphql-upload` package, which is used to implement',
'file uploads in Apollo Server 2.x, no longer supports Node.js LTS',
'versions prior to Node.js v8.5.0.',
'',
'Deployments which NEED file upload capabilities should update to',
'Node.js >= v8.5.0 to continue using uploads.',
'',
'If this server DOES NOT NEED file uploads and wishes to continue',
'using this version of Node.js, uploads can be disabled by adding:',
'',
' uploads: false,',
'',
'...to the options for Apollo Server and re-deploying the server.',
'',
'For more information, see https://bit.ly/gql-upload-node-6.',
'',
].join('\n'),
);
}

View file

@ -41,6 +41,18 @@ export const gql: (
...substitutions: any[]
) => DocumentNode = gqlTag;
import supportsUploadsInNode from './utils/supportsUploadsInNode';
import { GraphQLScalarType } from 'graphql';
import { GraphQLUpload as UploadScalar } from '@apollographql/apollo-upload-server';
export const GraphQLUpload = UploadScalar as GraphQLScalarType;
export { default as processFileUploads } from './processFileUploads';
// This is a conditional export intended to avoid traversing the
// entire module tree of `graphql-upload`. This only defined if the
// version of Node.js is >= 8.5.0 since those are the only Node.js versions
// which are supported by `graphql-upload@8`. Since the source of
// `graphql-upload` is not transpiled for older targets (in fact, it includes
// experimental ECMAScript modules), this conditional export is necessary
// to avoid modern ECMAScript from failing to parse by versions of Node.js
// which don't support it (yet — eg. Node.js 6 and async/await).
export const GraphQLUpload = supportsUploadsInNode
? (require('graphql-upload').GraphQLUpload as GraphQLScalarType)
: undefined;

View file

@ -0,0 +1,16 @@
import supportsUploadsInNode from './utils/supportsUploadsInNode';
// We'll memoize this function once at module load time since it should never
// change during runtime. In the event that we're using a version of Node.js
// less than 8.5.0, we'll
const processFileUploads:
| typeof import('graphql-upload').processRequest
| undefined = (() => {
if (supportsUploadsInNode) {
return require('graphql-upload')
.processRequest as typeof import('graphql-upload').processRequest;
}
return undefined;
})();
export default processFileUploads;

View file

@ -72,7 +72,7 @@ export interface Config
plugins?: PluginDefinition[];
persistedQueries?: PersistedQueryOptions | false;
subscriptions?: Partial<SubscriptionServerOptions> | string | false;
//https://github.com/jaydenseric/apollo-upload-server#options
//https://github.com/jaydenseric/graphql-upload#type-uploadoptions
uploads?: boolean | FileUploadOptions;
playground?: PlaygroundConfig;
}

View file

@ -0,0 +1,21 @@
const supportsUploadsInNode = (() => {
if (
process &&
process.release &&
process.release.name === 'node' &&
process.versions &&
typeof process.versions.node === 'string'
) {
const [nodeMajor, nodeMinor] = process.versions.node
.split('.', 2)
.map(segment => parseInt(segment, 10));
if (nodeMajor < 8 || (nodeMajor === 8 && nodeMinor < 5)) {
return false;
}
}
return true;
})();
export default supportsUploadsInNode;

View file

@ -26,7 +26,6 @@
"node": ">=6"
},
"dependencies": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"@types/accepts": "^1.3.5",
"@types/body-parser": "1.17.0",

View file

@ -10,14 +10,13 @@ import {
FileUploadOptions,
ApolloServerBase,
formatApolloErrors,
processFileUploads,
} from 'apollo-server-core';
import accepts from 'accepts';
import typeis from 'type-is';
import { graphqlExpress } from './expressApollo';
import { processRequest as processFileUploads } from '@apollographql/apollo-upload-server';
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
export interface ServerRegistration {
@ -44,8 +43,11 @@ const fileUploadMiddleware = (
next: express.NextFunction,
) => {
// Note: we use typeis directly instead of via req.is for connect support.
if (typeis(req, ['multipart/form-data'])) {
processFileUploads(req, uploadsConfig)
if (
typeof processFileUploads === 'function' &&
typeis(req, ['multipart/form-data'])
) {
processFileUploads(req, res, uploadsConfig)
.then(body => {
req.body = body;
next();
@ -134,7 +136,7 @@ export class ApolloServer extends ApolloServerBase {
}
let uploadsMiddleware;
if (this.uploadsConfig) {
if (this.uploadsConfig && typeof processFileUploads === 'function') {
uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
}

View file

@ -11,7 +11,7 @@ import { gql, AuthenticationError, Config } from 'apollo-server-core';
import { ApolloServer, ServerRegistration } from '../ApolloServer';
import {
atLeastMajorNodeVersion,
NODE_MAJOR_VERSION,
testApolloServer,
createServerInfo,
} from 'apollo-server-integration-testsuite';
@ -427,8 +427,9 @@ describe('apollo-server-express', () => {
});
});
});
// NODE: Intentionally skip file upload tests on Node.js 10 or higher.
(atLeastMajorNodeVersion(10) ? describe.skip : describe)(
// NODE: Skip Node.js 6, but only because `graphql-upload`
// doesn't support it.
(NODE_MAJOR_VERSION === 6 ? describe.skip : describe)(
'file uploads',
() => {
it('enabled uploads', async () => {

View file

@ -25,7 +25,6 @@
"node": ">=8"
},
"dependencies": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"accept": "^3.0.2",
"apollo-server-core": "file:../apollo-server-core",

View file

@ -4,7 +4,6 @@ import {
renderPlaygroundPage,
RenderPageOptions as PlaygroundRenderPageOptions,
} from '@apollographql/graphql-playground-html';
import { processRequest as processFileUploads } from '@apollographql/apollo-upload-server';
import { graphqlHapi } from './hapiApollo';
@ -13,13 +12,21 @@ import {
ApolloServerBase,
GraphQLOptions,
FileUploadOptions,
processFileUploads,
} from 'apollo-server-core';
function handleFileUploads(uploadsConfig: FileUploadOptions) {
return async (request: hapi.Request) => {
if (request.mime === 'multipart/form-data') {
return async (request: hapi.Request, _h?: hapi.ResponseToolkit) => {
if (
typeof processFileUploads === 'function' &&
request.mime === 'multipart/form-data'
) {
Object.defineProperty(request, 'payload', {
value: await processFileUploads(request, uploadsConfig),
value: await processFileUploads(
request,
request.response,
uploadsConfig,
),
writable: false,
});
}
@ -64,7 +71,7 @@ export class ApolloServer extends ApolloServerBase {
return h.continue;
}
if (this.uploadsConfig) {
if (this.uploadsConfig && typeof processFileUploads === 'function') {
await handleFileUploads(this.uploadsConfig)(request);
}

View file

@ -1,5 +1,5 @@
import {
atLeastMajorNodeVersion,
NODE_MAJOR_VERSION,
testApolloServer,
createServerInfo,
} from 'apollo-server-integration-testsuite';
@ -16,7 +16,7 @@ import { ApolloServer } from '../ApolloServer';
const port = 5555;
// NODE: Intentionally skip for Node.js < 8 since Hapi 17 doesn't support those.
(atLeastMajorNodeVersion(8) ? describe : describe.skip)(
(NODE_MAJOR_VERSION < 8 ? describe.skip : describe)(
'apollo-server-hapi',
() => {
let server: ApolloServer;

View file

@ -4,40 +4,37 @@ import { Config } from 'apollo-server-core';
import testSuite, {
schema as Schema,
CreateAppOptions,
atLeastMajorNodeVersion,
NODE_MAJOR_VERSION,
} from 'apollo-server-integration-testsuite';
// NODE: Intentionally skip on Node.js < 8 since Hapi 17 doesn't support less
(atLeastMajorNodeVersion(8) ? describe : describe.skip)(
'integration:Hapi',
() => {
async function createApp(options: CreateAppOptions = {}) {
const { Server } = require('hapi');
(NODE_MAJOR_VERSION < 8 ? describe.skip : describe)('integration:Hapi', () => {
async function createApp(options: CreateAppOptions = {}) {
const { Server } = require('hapi');
const app: import('hapi').Server = new Server({
host: 'localhost',
port: 8000,
});
const app: import('hapi').Server = new Server({
host: 'localhost',
port: 8000,
});
const server = new ApolloServer(
(options.graphqlOptions as Config) || { schema: Schema },
);
await server.applyMiddleware({
app,
});
const server = new ApolloServer(
(options.graphqlOptions as Config) || { schema: Schema },
);
await server.applyMiddleware({
app,
});
await app.start();
await app.start();
return app.listener;
return app.listener;
}
async function destroyApp(app) {
if (!app || !app.close) {
return;
}
await new Promise(resolve => app.close(resolve));
}
async function destroyApp(app) {
if (!app || !app.close) {
return;
}
await new Promise(resolve => app.close(resolve));
}
testSuite(createApp, destroyApp);
},
);
testSuite(createApp, destroyApp);
});

View file

@ -23,13 +23,10 @@ import gql from 'graphql-tag';
export * from './ApolloServer';
const NODE_MAJOR_VERSION: number = parseInt(
export const NODE_MAJOR_VERSION: number = parseInt(
process.versions.node.split('.', 1)[0],
10,
);
export function atLeastMajorNodeVersion(desiredVersion: number): boolean {
return NODE_MAJOR_VERSION >= desiredVersion;
}
const QueryRootType = new GraphQLObjectType({
name: 'QueryRoot',

View file

@ -25,7 +25,6 @@
"node": ">=6"
},
"dependencies": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"@koa/cors": "^2.2.1",
"@types/accepts": "^1.3.5",

View file

@ -6,14 +6,16 @@ import {
renderPlaygroundPage,
RenderPageOptions as PlaygroundRenderPageOptions,
} from '@apollographql/graphql-playground-html';
import { ApolloServerBase, formatApolloErrors } from 'apollo-server-core';
import {
ApolloServerBase,
formatApolloErrors,
processFileUploads,
} from 'apollo-server-core';
import accepts from 'accepts';
import typeis from 'type-is';
import { graphqlKoa } from './koaApollo';
import { processRequest as processFileUploads } from '@apollographql/apollo-upload-server';
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
import { GraphQLOptions, FileUploadOptions } from 'apollo-server-core';
@ -32,7 +34,11 @@ const fileUploadMiddleware = (
) => async (ctx: Koa.Context, next: Function) => {
if (typeis(ctx.req, ['multipart/form-data'])) {
try {
ctx.request.body = await processFileUploads(ctx.req, uploadsConfig);
ctx.request.body = await processFileUploads(
ctx.req,
ctx.res,
uploadsConfig,
);
return next();
} catch (error) {
if (error.status && error.expose) ctx.status = error.status;
@ -134,7 +140,7 @@ export class ApolloServer extends ApolloServerBase {
}
let uploadsMiddleware;
if (this.uploadsConfig) {
if (this.uploadsConfig && typeof processFileUploads === 'function') {
uploadsMiddleware = fileUploadMiddleware(this.uploadsConfig, this);
}

View file

@ -11,7 +11,7 @@ import { gql, AuthenticationError, Config } from 'apollo-server-core';
import { ApolloServer, ServerRegistration } from '../ApolloServer';
import {
atLeastMajorNodeVersion,
NODE_MAJOR_VERSION,
testApolloServer,
createServerInfo,
} from 'apollo-server-integration-testsuite';
@ -323,8 +323,9 @@ describe('apollo-server-koa', () => {
});
});
});
// NODE: Intentionally skip file upload tests on Node.js 10 or higher.
(atLeastMajorNodeVersion(10) ? describe.skip : describe)(
// NODE: Skip Node.js 6, but only because `graphql-upload`
// doesn't support it anymore.
(NODE_MAJOR_VERSION === 6 ? describe.skip : describe)(
'file uploads',
() => {
it('enabled uploads', async () => {

View file

@ -23,7 +23,6 @@
},
"homepage": "https://github.com/apollographql/apollo-server#readme",
"dependencies": {
"@apollographql/apollo-upload-server": "^5.0.3",
"@apollographql/graphql-playground-html": "^1.6.6",
"accept": "^3.0.2",
"apollo-server-core": "file:../apollo-server-core",

View file

@ -1,5 +1,8 @@
import { ApolloServerBase, GraphQLOptions } from 'apollo-server-core';
import { processRequest as processFileUploads } from '@apollographql/apollo-upload-server';
import {
ApolloServerBase,
GraphQLOptions,
processFileUploads,
} from 'apollo-server-core';
import { ServerResponse } from 'http';
import { send } from 'micro';
import { renderPlaygroundPage } from '@apollographql/graphql-playground-html';
@ -39,7 +42,9 @@ export class ApolloServer extends ApolloServerBase {
await promiseWillStart;
await this.handleFileUploads(req);
if (typeof processFileUploads === 'function') {
await this.handleFileUploads(req, res);
}
(await this.handleHealthCheck({
req,
@ -161,15 +166,19 @@ export class ApolloServer extends ApolloServerBase {
}
// If file uploads are detected, prepare them for easier handling with
// the help of `apollo-upload-server`.
private async handleFileUploads(req: MicroRequest) {
// the help of `graphql-upload`.
private async handleFileUploads(req: MicroRequest, res: ServerResponse) {
if (typeof processFileUploads !== 'function') {
return;
}
const contentType = req.headers['content-type'];
if (
this.uploadsConfig &&
contentType &&
contentType.startsWith('multipart/form-data')
) {
req.filePayload = await processFileUploads(req, this.uploadsConfig);
req.filePayload = await processFileUploads(req, res, this.uploadsConfig);
}
}
}

View file

@ -1,7 +1,7 @@
import micro from 'micro';
import listen from 'test-listen';
import { createApolloFetch } from 'apollo-fetch';
import { atLeastMajorNodeVersion } from 'apollo-server-integration-testsuite';
import { NODE_MAJOR_VERSION } from 'apollo-server-integration-testsuite';
import { gql } from 'apollo-server-core';
import FormData from 'form-data';
import fs from 'fs';
@ -146,7 +146,10 @@ describe('apollo-server-micro', function() {
});
});
(atLeastMajorNodeVersion(10) ? describe.skip : describe)(
// NODE: Intentionally skip file upload tests on Node.js 10.
// Also skip Node.js 6, but only because `graphql-upload`
// doesn't support it.
(NODE_MAJOR_VERSION === 6 ? describe.skip : describe)(
'file uploads',
function() {
it('should handle file uploads', async function() {

View file

@ -1,25 +0,0 @@
import { GraphQLScalarType } from 'graphql';
export const GraphQLUpload: GraphQLScalarType;
export interface ApolloUploadOptions {
/**
* Max allowed non-file multipart form field size in bytes; enough for your queries (default: 1 MB)
*/
maxFieldSize?: number;
/**
* Max allowed file size in bytes (default: Infinity)
*/
maxFileSize?: number;
/**
* Max allowed number of files (default: Infinity)
*/
maxFiles?: number;
}
export type Request = any;
export function processRequest(
request: Request,
options?: ApolloUploadOptions,
): Promise<any>;

30
types/graphql-upload/index.d.ts vendored Normal file
View file

@ -0,0 +1,30 @@
declare module 'graphql-upload' {
import { GraphQLScalarType } from 'graphql';
export const GraphQLUpload: GraphQLScalarType;
export interface ApolloUploadOptions {
/**
* Max allowed non-file multipart form field size in bytes; enough for your queries (default: 1 MB)
*/
maxFieldSize?: number;
/**
* Max allowed file size in bytes (default: Infinity)
*/
maxFileSize?: number;
/**
* Max allowed number of files (default: Infinity)
*/
maxFiles?: number;
}
export type Request = any;
export type Response = any;
export function processRequest(
request: Request,
response: Response,
options?: ApolloUploadOptions,
): Promise<any>;
}