Skip to content

Commit

Permalink
Use community provided types for graphql-upload (#2844)
Browse files Browse the repository at this point in the history
* 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: #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: #2844

* Revert "Any types"

This reverts commit 829f22a.

While this was once necessary, it should no longer be with the changes in
8e49b28 and 7638f64.

* Update CHANGELOG.md for #2844.
  • Loading branch information
denkristoffer authored and abernix committed Jul 30, 2019
1 parent d7d3613 commit e6462c8
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 42 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/apollo-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 0 additions & 2 deletions packages/apollo-server-core/src/processFileUploads.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference path="./types/graphql-upload.d.ts" />

import runtimeSupportsUploads from './utils/runtimeSupportsUploads';

// We'll memoize this function once at module load time since it should never
Expand Down
30 changes: 0 additions & 30 deletions packages/apollo-server-core/src/types/graphql-upload.d.ts

This file was deleted.

13 changes: 7 additions & 6 deletions packages/apollo-server-fastify/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -35,19 +36,19 @@ const fileUploadMiddleware = (
server: ApolloServerBase,
) => (
req: FastifyRequest<IncomingMessage>,
reply: FastifyReply<OutgoingMessage>,
reply: FastifyReply<ServerResponse>,
done: (err: Error | null, body?: any) => void,
) => {
if (
(req.req as any)[kMultipart] &&
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], {
Expand Down Expand Up @@ -79,7 +80,7 @@ export class ApolloServer extends ApolloServerBase {
const promiseWillStart = this.willStart();

return async (
app: FastifyInstance<Server, IncomingMessage, OutgoingMessage>,
app: FastifyInstance<Server, IncomingMessage, ServerResponse>,
) => {
await promiseWillStart;

Expand Down Expand Up @@ -120,7 +121,7 @@ export class ApolloServer extends ApolloServerBase {
const beforeHandlers = [
(
req: FastifyRequest<IncomingMessage>,
reply: FastifyReply<OutgoingMessage>,
reply: FastifyReply<ServerResponse>,
done: () => void,
) => {
// Note: if you enable playground in production and expect to be able to see your
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-hapi/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e6462c8

Please sign in to comment.