Skip to content

Commit

Permalink
fix: make handler and schema creation pluginable
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Mar 24, 2020
1 parent 87c6610 commit cab9340
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 35 deletions.
8 changes: 4 additions & 4 deletions packages/http-handler-apollo-server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { HttpHandlerApolloServerOptions } from "./types";
import apolloHandlerPlugin from "./apolloHandlerPlugin";
import createHandlerApolloServerPlugin from "./createHandlerApolloServerPlugin";
import { createSchema, createHandlerApolloServer, apolloHandler } from "./plugins";

export default (options: HttpHandlerApolloServerOptions = {}) => [
createHandlerApolloServerPlugin(options),
apolloHandlerPlugin
createHandlerApolloServer(options),
apolloHandler,
createSchema
];
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ApolloServer } from "apollo-server-lambda";
import { CreateApolloHandlerPlugin } from "./types";
import { CreateApolloHandlerPlugin } from "./../types";
import { boolean } from "boolean";
import { Plugin, PluginsContainer } from "@webiny/plugins/types";
import { GraphQLSchema } from "graphql";
import { CreateSchemaPlugin } from "@webiny/http-handler-apollo-server/types";

function normalizeEvent(event) {
// In AWS, when enabling binary support, received body gets base64 encoded. Did not find a way to solve this
Expand All @@ -13,16 +12,16 @@ function normalizeEvent(event) {
}
}

export type CreateSchemaPlugin = Plugin & {
name: "handler-apollo-server-create-schema";
type: "handler-apollo-server-create-schema";
create(params: { plugins: PluginsContainer }): { schema: GraphQLSchema };
};
let cache;

const plugin: CreateApolloHandlerPlugin = {
name: "handler-apollo-server-create",
type: "handler-apollo-server-create",
name: "handler-apollo-server-create-handler",
type: "handler-apollo-server-create-handler",
async create({ context, options }) {
if (cache) {
return cache;
}

const { server = {}, handler = {} } = options;

const createSchemaPlugin = context.plugins.byName<CreateSchemaPlugin>(
Expand Down Expand Up @@ -58,7 +57,7 @@ const plugin: CreateApolloHandlerPlugin = {
}
});

return {
cache = {
schema,
handler: (event, context) => {
normalizeEvent(event);
Expand All @@ -73,6 +72,8 @@ const plugin: CreateApolloHandlerPlugin = {
});
}
};

return cache;
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { createResponse } from "@webiny/http-handler";
import { HttpHandlerPlugin } from "@webiny/http-handler/types";
import { boolean } from "boolean";
import { CreateApolloHandlerPlugin, HttpHandlerApolloServerOptions } from "./types";

let apolloHandler;
import { CreateApolloHandlerPlugin, HttpHandlerApolloServerOptions } from "./../types";

export default (options: HttpHandlerApolloServerOptions = {}): HttpHandlerPlugin => ({
type: "handler",
Expand All @@ -15,25 +13,22 @@ export default (options: HttpHandlerApolloServerOptions = {}): HttpHandlerPlugin
async handle({ args, context }) {
const [event] = args;
try {
if (!apolloHandler) {
const createApolloHandlerPlugin = context.plugins.byName<CreateApolloHandlerPlugin>(
"handler-apollo-server-create"
);

if (!createApolloHandlerPlugin) {
throw Error(`"handler-apollo-server-create" plugin is not configured!`);
}

const { handler } = await createApolloHandlerPlugin.create({
context,
options
});
const createApolloHandlerPlugin = context.plugins.byName<CreateApolloHandlerPlugin>(
"handler-apollo-server-create-handler"
);

apolloHandler = handler;
if (!createApolloHandlerPlugin) {
throw Error(`"handler-apollo-server-create-handler" plugin is not configured!`);
}

const { handler } = await createApolloHandlerPlugin.create({
args,
context,
options
});

// Will return the complete response, including "statusCode", "headers", and "body" fields.
return await apolloHandler(event, context);
return await handler(event, context);
} catch (e) {
const { ...requestContext } = event.requestContext;
const report = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createSchema } from "@webiny/graphql";

export default {
type: "handler-apollo-server-create-schema",
name: "handler-apollo-server-create-schema",
create: createSchema
};
3 changes: 3 additions & 0 deletions packages/http-handler-apollo-server/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default as createSchema } from "./createSchema";
export { default as createHandlerApolloServer } from "./createHandlerApolloServer";
export { default as apolloHandler } from "./apolloHandler";
11 changes: 9 additions & 2 deletions packages/http-handler-apollo-server/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpHandlerContext } from "@webiny/http-handler/types";
import { GraphQLSchema } from "graphql";
import { Plugin } from "@webiny/plugins/types";
import { Plugin, PluginsContainer } from "@webiny/plugins/types";

export interface HttpHandlerApolloServerOptions {
debug?: boolean | string;
Expand All @@ -19,9 +19,16 @@ type CreateApolloHandlerPluginCreateResponse = {
};

export type CreateApolloHandlerPlugin = Plugin & {
type: "handler-apollo-server-create";
type: "handler-apollo-server-create-handler";
create(params: {
args: Array<{ [key: string]: any }>;
options: HttpHandlerApolloServerOptions;
context: HttpHandlerContext;
}): CreateApolloHandlerPluginCreateResponse | Promise<CreateApolloHandlerPluginCreateResponse>;
};

export type CreateSchemaPlugin = Plugin & {
name: "handler-apollo-server-create-schema";
type: "handler-apollo-server-create-schema";
create(params: { plugins: PluginsContainer }): { schema: GraphQLSchema };
};

0 comments on commit cab9340

Please sign in to comment.