Skip to content

Commit

Permalink
apollo-server-hapi: enable noImplicitAny and strictNullChecks
Browse files Browse the repository at this point in the history
As in #5177, the easiest way is to inline hapiApollo (AS1 API) into
ApolloServer.ts.

Also remove some unnecessary `await`s.

Fixes #5170.
  • Loading branch information
glasser committed May 5, 2021
1 parent bc382e6 commit a8eb916
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 116 deletions.
81 changes: 64 additions & 17 deletions packages/apollo-server-hapi/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
RenderPageOptions as PlaygroundRenderPageOptions,
} from '@apollographql/graphql-playground-html';

import { plugin as graphqlHapi, HapiPluginOptions } from './hapiApollo';

export { GraphQLOptions } from 'apollo-server-core';
import {
ApolloServerBase,
convertNodeHttpToRequest,
GraphQLOptions,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import Boom from '@hapi/boom';

export class ApolloServer extends ApolloServerBase {
// This translates the arguments from the middleware into graphQL options It
Expand Down Expand Up @@ -39,7 +41,7 @@ export class ApolloServer extends ApolloServerBase {

if (!path) path = '/graphql';

await app.ext({
app.ext({
type: 'onRequest',
method: async (request, h) => {
if (request.path !== path) {
Expand Down Expand Up @@ -72,13 +74,13 @@ export class ApolloServer extends ApolloServerBase {
});

if (!disableHealthCheck) {
await app.route({
app.route({
method: '*',
path: '/.well-known/apollo/server-health',
options: {
cors: cors !== undefined ? cors : true,
},
handler: async function(request, h) {
handler: async function (request, h) {
if (onHealthCheck) {
try {
await onHealthCheck(request);
Expand All @@ -96,17 +98,62 @@ export class ApolloServer extends ApolloServerBase {
});
}

await app.register<HapiPluginOptions>({
plugin: graphqlHapi,
options: {
path,
graphqlOptions: this.createGraphQLServerOptions.bind(this),
route:
route !== undefined
? route
: {
cors: cors !== undefined ? cors : true,
},
app.route({
method: ['GET', 'POST'],
path,
options: route ?? {
cors: cors ?? true,
},
handler: async (request, h) => {
try {
const { graphqlResponse, responseInit } = await runHttpQuery(
[request, h],
{
method: request.method.toUpperCase(),
options: () => this.createGraphQLServerOptions(request, h),
query:
request.method === 'post'
? // TODO type payload as string or Record
(request.payload as any)
: request.query,
request: convertNodeHttpToRequest(request.raw.req),
},
);

const response = h.response(graphqlResponse);
if (responseInit.headers) {
Object.entries(
responseInit.headers,
).forEach(([headerName, value]) =>
response.header(headerName, value),
);
}
return response;
} catch (e: unknown) {
const error = e as HttpQueryError;
if ('HttpQueryError' !== error.name) {
throw Boom.boomify(error);
}

if (true === error.isGraphQLError) {
const response = h.response(error.message);
response.code(error.statusCode);
response.type('application/json');
return response;
}

const err = new Boom.Boom(error.message, {
statusCode: error.statusCode,
});
if (error.headers) {
Object.entries(error.headers).forEach(([headerName, value]) => {
err.output.headers[headerName] = value;
});
}
// Boom hides the error when status code is 500
err.output.payload.message = error.message;
throw err;
}
},
});

Expand All @@ -115,7 +162,7 @@ export class ApolloServer extends ApolloServerBase {
}

export interface ServerRegistration {
app?: hapi.Server;
app: hapi.Server;
path?: string;
cors?: boolean | hapi.RouteOptionsCors;
route?: hapi.RouteOptions;
Expand Down
96 changes: 0 additions & 96 deletions packages/apollo-server-hapi/src/hapiApollo.ts

This file was deleted.

4 changes: 1 addition & 3 deletions packages/apollo-server-hapi/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
"extends": "../../tsconfig.base",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
"noImplicitAny": false,
"strictNullChecks": false
"outDir": "./dist"
},
"include": ["src/**/*"],
"exclude": ["**/__tests__"],
Expand Down

0 comments on commit a8eb916

Please sign in to comment.