2018-06-11 14:25:59 +02:00
|
|
|
import express from 'express';
|
|
|
|
import corsMiddleware from 'cors';
|
2018-05-10 22:51:26 -07:00
|
|
|
import { json, OptionsJson } from 'body-parser';
|
2018-06-13 15:56:06 -07:00
|
|
|
import playgroundMiddleware from 'graphql-playground-middleware-express';
|
|
|
|
import { MiddlewareOptions as PlaygroundMiddlewareOptions } from 'graphql-playground-html';
|
2018-05-29 15:58:52 -07:00
|
|
|
import { ApolloServerBase, formatApolloErrors } from 'apollo-server-core';
|
2018-06-11 14:25:59 +02:00
|
|
|
import accepts from 'accepts';
|
2018-06-13 15:19:08 -07:00
|
|
|
import typeis from 'type-is';
|
2018-05-01 06:09:48 -07:00
|
|
|
|
|
|
|
import { graphqlExpress } from './expressApollo';
|
|
|
|
|
2018-05-29 15:58:52 -07:00
|
|
|
import {
|
|
|
|
processRequest as processFileUploads,
|
|
|
|
GraphQLUpload,
|
|
|
|
} from 'apollo-upload-server';
|
|
|
|
|
2018-06-12 17:46:56 -07:00
|
|
|
export { GraphQLOptions, GraphQLExtension } from 'apollo-server-core';
|
2018-06-14 11:48:59 -07:00
|
|
|
import { GraphQLOptions, gql, makeExecutableSchema } from 'apollo-server-core';
|
2018-05-29 15:58:52 -07:00
|
|
|
|
2018-05-01 06:09:48 -07:00
|
|
|
export interface ServerRegistration {
|
2018-06-13 15:19:08 -07:00
|
|
|
// Note: You can also pass a connect.Server here. If we changed this field to
|
|
|
|
// `express.Application | connect.Server`, it would be very hard to get the
|
|
|
|
// app.use calls to typecheck even though they do work properly. Our
|
|
|
|
// assumption is that very few people use connect with TypeScript (and in fact
|
|
|
|
// we suspect the only connect users left writing GraphQL apps are Meteor
|
|
|
|
// users).
|
2018-05-01 06:09:48 -07:00
|
|
|
app: express.Application;
|
|
|
|
path?: string;
|
2018-06-13 15:19:08 -07:00
|
|
|
cors?: corsMiddleware.CorsOptions | boolean;
|
|
|
|
bodyParserConfig?: OptionsJson | boolean;
|
2018-05-22 21:58:37 -07:00
|
|
|
onHealthCheck?: (req: express.Request) => Promise<any>;
|
|
|
|
disableHealthCheck?: boolean;
|
2018-06-13 15:56:06 -07:00
|
|
|
gui?: boolean | PlaygroundMiddlewareOptions;
|
2018-05-29 15:58:52 -07:00
|
|
|
//https://github.com/jaydenseric/apollo-upload-server#options
|
|
|
|
uploads?: boolean | Record<string, any>;
|
2018-05-01 06:09:48 -07:00
|
|
|
}
|
|
|
|
|
2018-05-29 15:58:52 -07:00
|
|
|
const fileUploadMiddleware = (
|
|
|
|
uploadsConfig: Record<string, any>,
|
2018-06-12 17:46:56 -07:00
|
|
|
server: ApolloServerBase,
|
2018-05-29 15:58:52 -07:00
|
|
|
) => (
|
|
|
|
req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
next: express.NextFunction,
|
|
|
|
) => {
|
2018-06-13 15:19:08 -07:00
|
|
|
// Note: we use typeis directly instead of via req.is for connect support.
|
|
|
|
if (typeis(req, ['multipart/form-data'])) {
|
2018-05-29 15:58:52 -07:00
|
|
|
processFileUploads(req, uploadsConfig)
|
|
|
|
.then(body => {
|
|
|
|
req.body = body;
|
|
|
|
next();
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
if (error.status && error.expose) res.status(error.status);
|
|
|
|
|
|
|
|
next(
|
|
|
|
formatApolloErrors([error], {
|
|
|
|
formatter: server.requestOptions.formatError,
|
|
|
|
debug: server.requestOptions.debug,
|
|
|
|
logFunction: server.requestOptions.logFunction,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-13 16:59:27 -07:00
|
|
|
export class ApolloServer extends ApolloServerBase {
|
|
|
|
//This translates the arguments from the middleware into graphQL options It
|
|
|
|
//provides typings for the integration specific behavior, ideally this would
|
|
|
|
//be propagated with a generic to the super class
|
|
|
|
async createGraphQLServerOptions(
|
|
|
|
req: express.Request,
|
|
|
|
res: express.Response,
|
|
|
|
): Promise<GraphQLOptions> {
|
|
|
|
return super.graphQLServerOptions({ req, res });
|
|
|
|
}
|
|
|
|
|
|
|
|
protected supportsSubscriptions(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
public applyMiddleware({
|
|
|
|
app,
|
|
|
|
path,
|
|
|
|
cors,
|
|
|
|
bodyParserConfig,
|
|
|
|
disableHealthCheck,
|
|
|
|
gui,
|
|
|
|
onHealthCheck,
|
|
|
|
uploads,
|
|
|
|
}: ServerRegistration) {
|
|
|
|
if (!path) path = '/graphql';
|
|
|
|
|
|
|
|
if (!disableHealthCheck) {
|
|
|
|
//uses same path as engine proxy, but is generally useful.
|
|
|
|
app.use('/.well-known/apollo/server-health', (req, res) => {
|
|
|
|
//Response follows https://tools.ietf.org/html/draft-inadarei-api-health-check-01
|
|
|
|
res.type('application/health+json');
|
|
|
|
|
|
|
|
if (onHealthCheck) {
|
|
|
|
onHealthCheck(req)
|
|
|
|
.then(() => {
|
|
|
|
res.json({ status: 'pass' });
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
res.status(503).json({ status: 'fail' });
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.json({ status: 'pass' });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-05-22 21:58:37 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
let uploadsMiddleware;
|
|
|
|
if (uploads !== false) {
|
2018-06-14 11:48:59 -07:00
|
|
|
this.enhanceSchema(
|
|
|
|
makeExecutableSchema({
|
|
|
|
typeDefs: gql`
|
|
|
|
scalar Upload
|
|
|
|
`,
|
|
|
|
resolvers: { Upload: GraphQLUpload },
|
|
|
|
}),
|
|
|
|
);
|
2018-05-29 15:58:52 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
uploadsMiddleware = fileUploadMiddleware(
|
|
|
|
typeof uploads !== 'boolean' ? uploads : {},
|
|
|
|
this,
|
|
|
|
);
|
|
|
|
}
|
2018-05-29 15:58:52 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
// XXX multiple paths?
|
|
|
|
this.graphqlPath = path;
|
2018-05-01 06:09:48 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
// Note that we don't just pass all of these handlers to a single app.use call
|
|
|
|
// for 'connect' compatibility.
|
|
|
|
if (cors === true) {
|
|
|
|
app.use(path, corsMiddleware());
|
|
|
|
} else if (cors !== false) {
|
|
|
|
app.use(path, corsMiddleware(cors));
|
|
|
|
}
|
2018-06-13 15:19:08 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
if (bodyParserConfig === true) {
|
|
|
|
app.use(path, json());
|
|
|
|
} else if (bodyParserConfig !== false) {
|
|
|
|
app.use(path, json(bodyParserConfig));
|
|
|
|
}
|
2018-06-13 15:19:08 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
if (uploadsMiddleware) {
|
|
|
|
app.use(path, uploadsMiddleware);
|
|
|
|
}
|
2018-06-13 15:19:08 -07:00
|
|
|
|
2018-06-14 01:36:09 -07:00
|
|
|
// Note: if you enable a gui in production and expect to be able to see your
|
|
|
|
// schema, you'll need to manually specify `introspection: true` in the
|
|
|
|
// ApolloServer constructor; by default, the introspection query is only
|
|
|
|
// enabled in dev.
|
|
|
|
const guiEnabled =
|
|
|
|
!!gui || (gui === undefined && process.env.NODE_ENV !== 'production');
|
|
|
|
|
|
|
|
app.use(path, (req, res, next) => {
|
|
|
|
if (guiEnabled && req.method === 'GET') {
|
|
|
|
//perform more expensive content-type check only if necessary
|
|
|
|
const accept = accepts(req);
|
|
|
|
const types = accept.types() as string[];
|
|
|
|
const prefersHTML =
|
|
|
|
types.find(
|
|
|
|
(x: string) => x === 'text/html' || x === 'application/json',
|
|
|
|
) === 'text/html';
|
|
|
|
|
|
|
|
if (prefersHTML) {
|
|
|
|
const middlewareOptions = {
|
|
|
|
endpoint: path,
|
|
|
|
subscriptionEndpoint: this.subscriptionsPath,
|
|
|
|
...(typeof gui === 'boolean' ? {} : gui),
|
|
|
|
};
|
|
|
|
return playgroundMiddleware(middlewareOptions)(req, res, next);
|
|
|
|
}
|
2018-05-01 06:09:48 -07:00
|
|
|
}
|
2018-06-14 01:36:09 -07:00
|
|
|
return graphqlExpress(this.createGraphQLServerOptions.bind(this))(
|
|
|
|
req,
|
|
|
|
res,
|
|
|
|
next,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const registerServer = () => {
|
|
|
|
throw new Error(
|
|
|
|
'Please use server.applyMiddleware instead of registerServer. This warning will be removed in the next release',
|
|
|
|
);
|
2018-05-01 06:09:48 -07:00
|
|
|
};
|