From e6462c84284a3ef267fa2f975487897498c14fbd Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Wed, 31 Jul 2019 01:56:20 +0200 Subject: [PATCH] Use community provided types for graphql-upload (#2844) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Use community provided types for graphql-upload * Update package-lock.json * Update package-lock.json * Type processFileUploads promise * Use any type to pass server response type check * Any types * Fix package-lock.json * fastify: Switch `req` type to `ServerResponse` rather than `OutgoingMessage`. While currently we use `OutgoingMessage`, it actually seems to be suggested that `ServerResonse` — which is an extension of `OutgoingMessage`: https://github.com/fastify/fastify/blob/master/docs/TypeScript.md#example Helps-to-land: https://github.com/apollographql/apollo-server/pull/2844 * hapi: Switch uploads to receive "raw" request and response. The immediately pressing reason is to align on the same types that the `processRequest` method from `graphql-upload` (aliased here as `processFileUploads`) uses. Using the raw types should be fine as `graphql-upload` only binds to the `response` to receive "close"-like `EventEmitter` events, and uses the `request` side for the piping of data, which should be identical to Hapi's abstraction. Helps-to-land: https://github.com/apollographql/apollo-server/pull/2844 * Revert "Any types" This reverts commit 829f22ae035555100b9b6f8e4248dd760f0bbf69. While this was once necessary, it should no longer be with the changes in 8e49b28 and 7638f64. * Update CHANGELOG.md for #2844. --- CHANGELOG.md | 3 ++ package-lock.json | 14 +++++++-- packages/apollo-server-core/package.json | 1 + .../src/processFileUploads.ts | 2 -- .../src/types/graphql-upload.d.ts | 30 ------------------- .../apollo-server-fastify/src/ApolloServer.ts | 13 ++++---- .../apollo-server-hapi/src/ApolloServer.ts | 4 +-- 7 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 packages/apollo-server-core/src/types/graphql-upload.d.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 93e2492d53b..689362b1aa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ The version headers in this history reflect the versions of Apollo Server itself - `apollo-engine-reporting`: Add missing `apollo-server-caching` dependency. [PR #3054](https://github.com/apollographql/apollo-server/pull/3054) - `apollo-server-hapi`: Revert switch from `accept` and `boom` which took place in v2.8.0. [PR #3089](https://github.com/apollographql/apollo-server/pull/3089) - `@apollo/gateway`: Change the `setInterval` timer, which is used to continuously check for updates to a federated graph from the Apollo Graph Manager, to be an `unref`'d timer. Without this change, the server wouldn't terminate properly once polling had started since the event-loop would continue to have unprocessed events on it. [PR #3105](https://github.com/apollographql/apollo-server/pull/3105) +- Switch to using community `@types/graphql-upload` types. +- `apollo-server-fastify`: Change the typing of the HTTP `response` from `OutgoingMessage` to `ServerResponse`. [Commit](https://github.com/apollographql/apollo-server/commit/7638f643fa0445f5f8151ef884da779d85fb954c) +- `apollo-server-hapi`: Pass the `raw` request and response objects to `graphql-upload`s `processRequest` method to align on the same TypeScript types. [Commit](https://github.com/apollographql/apollo-server/commit/8e49b288a6aecd0e134637e64ef4ed751aa8d304) ### v2.8.0 diff --git a/package-lock.json b/package-lock.json index 8e5c277193d..d8a22801b7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2904,8 +2904,17 @@ "@types/graphql": { "version": "14.2.3", "resolved": "https://registry.npmjs.org/@types/graphql/-/graphql-14.2.3.tgz", - "integrity": "sha512-UoCovaxbJIxagCvVfalfK7YaNhmxj3BQFRQ2RHQKLiu+9wNXhJnlbspsLHt/YQM99IaLUUFJNzCwzc6W0ypMeQ==", - "dev": true + "integrity": "sha512-UoCovaxbJIxagCvVfalfK7YaNhmxj3BQFRQ2RHQKLiu+9wNXhJnlbspsLHt/YQM99IaLUUFJNzCwzc6W0ypMeQ==" + }, + "@types/graphql-upload": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/@types/graphql-upload/-/graphql-upload-8.0.0.tgz", + "integrity": "sha512-xeDYfZb0SeRpCRuivN9TXLEVsbG0F4inFtx03yadZeaTXr1kC224/ZvlV6NKqQ//HNvUxneYcEoUB5ugJc8dnA==", + "requires": { + "@types/express": "*", + "@types/graphql": "*", + "@types/koa": "*" + } }, "@types/hapi": { "version": "17.8.6", @@ -3699,6 +3708,7 @@ "requires": { "@apollographql/apollo-tools": "^0.4.0", "@apollographql/graphql-playground-html": "1.6.24", + "@types/graphql-upload": "^8.0.0", "@types/ws": "^6.0.0", "apollo-cache-control": "file:packages/apollo-cache-control", "apollo-datasource": "file:packages/apollo-datasource", diff --git a/packages/apollo-server-core/package.json b/packages/apollo-server-core/package.json index d89a7575514..596d9f1c0bc 100644 --- a/packages/apollo-server-core/package.json +++ b/packages/apollo-server-core/package.json @@ -26,6 +26,7 @@ "dependencies": { "@apollographql/apollo-tools": "^0.4.0", "@apollographql/graphql-playground-html": "1.6.24", + "@types/graphql-upload": "^8.0.0", "@types/ws": "^6.0.0", "apollo-cache-control": "file:../apollo-cache-control", "apollo-datasource": "file:../apollo-datasource", diff --git a/packages/apollo-server-core/src/processFileUploads.ts b/packages/apollo-server-core/src/processFileUploads.ts index c2e0cb84808..22ffb483e6d 100644 --- a/packages/apollo-server-core/src/processFileUploads.ts +++ b/packages/apollo-server-core/src/processFileUploads.ts @@ -1,5 +1,3 @@ -/// - import runtimeSupportsUploads from './utils/runtimeSupportsUploads'; // We'll memoize this function once at module load time since it should never diff --git a/packages/apollo-server-core/src/types/graphql-upload.d.ts b/packages/apollo-server-core/src/types/graphql-upload.d.ts deleted file mode 100644 index 3516f84fa35..00000000000 --- a/packages/apollo-server-core/src/types/graphql-upload.d.ts +++ /dev/null @@ -1,30 +0,0 @@ -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; -} diff --git a/packages/apollo-server-fastify/src/ApolloServer.ts b/packages/apollo-server-fastify/src/ApolloServer.ts index d8a6f73d50c..4158d9ad05b 100644 --- a/packages/apollo-server-fastify/src/ApolloServer.ts +++ b/packages/apollo-server-fastify/src/ApolloServer.ts @@ -8,8 +8,9 @@ import { processFileUploads, } from 'apollo-server-core'; import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify'; -import { IncomingMessage, OutgoingMessage, Server } from 'http'; +import { IncomingMessage, ServerResponse, Server } from 'http'; import { graphqlFastify } from './fastifyApollo'; +import { GraphQLOperation } from 'graphql-upload'; const kMultipart = Symbol('multipart'); const fastJson = require('fast-json-stringify'); @@ -35,7 +36,7 @@ const fileUploadMiddleware = ( server: ApolloServerBase, ) => ( req: FastifyRequest, - reply: FastifyReply, + reply: FastifyReply, done: (err: Error | null, body?: any) => void, ) => { if ( @@ -43,11 +44,11 @@ const fileUploadMiddleware = ( typeof processFileUploads === 'function' ) { processFileUploads(req.req, reply.res, uploadsConfig) - .then(body => { + .then((body: GraphQLOperation | GraphQLOperation[]) => { req.body = body; done(null); }) - .catch(error => { + .catch((error: any) => { if (error.status && error.expose) reply.status(error.status); throw formatApolloErrors([error], { @@ -79,7 +80,7 @@ export class ApolloServer extends ApolloServerBase { const promiseWillStart = this.willStart(); return async ( - app: FastifyInstance, + app: FastifyInstance, ) => { await promiseWillStart; @@ -120,7 +121,7 @@ export class ApolloServer extends ApolloServerBase { const beforeHandlers = [ ( req: FastifyRequest, - reply: FastifyReply, + reply: FastifyReply, done: () => void, ) => { // Note: if you enable playground in production and expect to be able to see your diff --git a/packages/apollo-server-hapi/src/ApolloServer.ts b/packages/apollo-server-hapi/src/ApolloServer.ts index 79c7d2dc2d8..0822446ce4d 100644 --- a/packages/apollo-server-hapi/src/ApolloServer.ts +++ b/packages/apollo-server-hapi/src/ApolloServer.ts @@ -23,8 +23,8 @@ function handleFileUploads(uploadsConfig: FileUploadOptions) { ) { Object.defineProperty(request, 'payload', { value: await processFileUploads( - request, - request.response, + request.raw.req, + request.raw.res, uploadsConfig, ), writable: false,