diff --git a/packages/base/package.json b/packages/base/package.json index 3a7f0b563..460a9afc2 100644 --- a/packages/base/package.json +++ b/packages/base/package.json @@ -57,6 +57,7 @@ "apollo-cache-inmemory": "1.6.6", "apollo-server-caching": "0.5.2", "apollo-server-micro": "2.19.0", + "apollo-server-lambda": "2.19.0", "faker": "5.1.0", "faunadb": "4.0.0", "graphql": "15.4.0" diff --git a/packages/base/src/Bootstrap.ts b/packages/base/src/Bootstrap.ts index 59b9a46cb..b73b61755 100644 --- a/packages/base/src/Bootstrap.ts +++ b/packages/base/src/Bootstrap.ts @@ -61,7 +61,7 @@ export function collectPlugins(path = process.cwd()): Array { * * @param plugin */ -export async function importPlugin(plugin: string) { +export function importPlugin(plugin: string) { try { if (plugin[0] === "/") { let pluginPath; @@ -73,13 +73,13 @@ export async function importPlugin(plugin: string) { pluginPath = path.resolve(plugin, "dist/server/index.js"); } - return await import(pluginPath); + return import(pluginPath); } else { if (process.env.NODE_ENV === "test") { plugin = `${plugin}/server/index.ts`; } - return await import(plugin); + return import(plugin); } } catch (e) { throw new PluginLoadError(plugin, e); @@ -100,7 +100,7 @@ let schema: string; * Doing it this way temporarily due to us loosing caching functionality using * executableSchema() */ -export async function bootstrapSchema(hoisted = false): Promise { +export function bootstrapSchema(hoisted = false): string { if (schema) return schema; //We already have it /** @@ -128,7 +128,7 @@ export async function bootstrapSchema(hoisted = false): Promise { for (const plugin of loadManifest().plugins) { const isLocalPlugin = isAPlugin(); - const currentPlugin = await importPlugin(plugin); + const currentPlugin = importPlugin(plugin) as any; currentPlugin.default.schemas.forEach((schema: any) => { const schemaRootPath = isLocalPlugin diff --git a/packages/base/src/Server.ts b/packages/base/src/Server.ts index 1a7325dc1..efe2929d4 100644 --- a/packages/base/src/Server.ts +++ b/packages/base/src/Server.ts @@ -1,4 +1,5 @@ -import { ApolloServer, gql } from "apollo-server-micro"; +import { gql } from "apollo-server-micro"; +import { ApolloServer as LambdaApolloServer } from "apollo-server-lambda"; import { bootstrapSchema, importPlugin, loadManifest } from "./Bootstrap"; import { Resolvers } from "./resolvers"; import { ServerContext } from "./typings/Server"; @@ -36,14 +37,14 @@ export const getServerModels = () => { * We only have this extracted to allow us to bootstrap * the context within our integration tests. */ -export async function getServerContext({ req, res }): Promise { +export function getServerContext({ req, res }): ServerContext { let context = { req, res, models: getServerModels(), eventEmitter }; if (fs.existsSync(process.cwd() + "/resolvers.js")) { - const pluginsFile = await import(process.cwd() + "/resolvers.js"); + const pluginsFile = import(process.cwd() + "/resolvers.js") as any; const serverKeys = Object.keys(pluginsFile.server); for (const p of serverKeys) { - const res = await pluginsFile.server[p].getPluginContext({ req, models: context.models, eventEmitter }); + const res = pluginsFile.server[p].getPluginContext({ req, models: context.models, eventEmitter }); context = { ...context, ...res }; } return context; @@ -51,8 +52,8 @@ export async function getServerContext({ req, res }): Promise { //We need to merge all plugin context for any additional context items they add //TODO needs to get generated from cli for all the requires on plugins for (const plugin of loadManifest().plugins) { - const currentPlugin = await importPlugin(plugin); - const res = await currentPlugin.getPluginContext({ req, models: context.models, eventEmitter }); + const currentPlugin = importPlugin(plugin) as any; + const res = currentPlugin.getPluginContext({ req, models: context.models, eventEmitter }); context = { ...context, ...res }; if (currentPlugin.default.listens) { @@ -76,11 +77,11 @@ export async function getServerContext({ req, res }): Promise { * * @param context */ -export async function CorejamServer(context = ({ req, res }) => getServerContext({ req, res })): Promise { +export function CorejamServer(context = ({ req, res }) => getServerContext({ req, res })) { let resolvers = Object.values(Resolvers); if (fs.existsSync(process.cwd() + "/resolvers.js")) { - const pluginsFile = await import(process.cwd() + "/resolvers.js"); + const pluginsFile = import(process.cwd() + "/resolvers.js") as any; Object.keys(pluginsFile.server).map((p) => { if (pluginsFile.server[p].default.resolvers) { resolvers = Object.values({ @@ -92,7 +93,7 @@ export async function CorejamServer(context = ({ req, res }) => getServerContext } else { //We need to merge all plugin resolvers into our core for (const plugin of loadManifest().plugins) { - const currentPlugin = await importPlugin(plugin); + const currentPlugin = importPlugin(plugin) as any; if (currentPlugin.default.resolvers) { resolvers = Object.values({ @@ -118,9 +119,9 @@ export async function CorejamServer(context = ({ req, res }) => getServerContext } } - return new ApolloServer({ - typeDefs: gql(await bootstrapSchema()), + return { + typeDefs: gql(bootstrapSchema()), resolvers, context, - }); + }; } diff --git a/packages/base/src/client/ServerClient.ts b/packages/base/src/client/ServerClient.ts index 8b6c45d63..b1f9efbab 100644 --- a/packages/base/src/client/ServerClient.ts +++ b/packages/base/src/client/ServerClient.ts @@ -9,7 +9,7 @@ export type Variables = { [key: string]: any }; * if we dont do that we have an issue because we cant `await` top level in our hydrate * script. */ -const corejamServer: Promise = CorejamServer(); +const corejamServer = new ApolloServer(CorejamServer()); /** * This function is used for our Stencil Hydrate render process. diff --git a/yarn.lock b/yarn.lock index 9ab7e72f8..77ea1b6b5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1770,6 +1770,11 @@ dependencies: "@types/glob" "*" +"@types/aws-lambda@^8.10.31": + version "8.10.64" + resolved "https://registry.yarnpkg.com/@types/aws-lambda/-/aws-lambda-8.10.64.tgz#4bdcb725aef96bef0cb1decf19c7efff1df22fe7" + integrity sha512-LRKk2UQCSi7BsO5TlfSI8cTNpOGz+MH6+RXEWtuZmxJficQgxwEYJDiKVirzgyiHce0L0F4CqCVvKTwblAeOUw== + "@types/babel-types@*", "@types/babel-types@^7.0.0": version "7.0.9" resolved "https://registry.yarnpkg.com/@types/babel-types/-/babel-types-7.0.9.tgz#01d7b86949f455402a94c788883fe4ba574cad41" @@ -2841,6 +2846,18 @@ apollo-server-errors@^2.4.2: resolved "https://registry.yarnpkg.com/apollo-server-errors/-/apollo-server-errors-2.4.2.tgz#1128738a1d14da989f58420896d70524784eabe5" integrity sha512-FeGxW3Batn6sUtX3OVVUm7o56EgjxDlmgpTLNyWcLb0j6P8mw9oLNyAm3B+deHA4KNdNHO5BmHS2g1SJYjqPCQ== +apollo-server-lambda@2.19.0: + version "2.19.0" + resolved "https://registry.yarnpkg.com/apollo-server-lambda/-/apollo-server-lambda-2.19.0.tgz#63d114bd095ca6d2958e1d05f0c0ed9d23cfd61b" + integrity sha512-44W6QaHMnVsKKXtC0o04JR0U65hoUC7Pfj3ik1PThsGejHVByyaQi7Qfbas3vuMAxU0UGkF5TKycc7K6KFxGUw== + dependencies: + "@apollographql/graphql-playground-html" "1.6.26" + "@types/aws-lambda" "^8.10.31" + apollo-server-core "^2.19.0" + apollo-server-env "^2.4.5" + apollo-server-types "^0.6.1" + graphql-tools "^4.0.0" + apollo-server-micro@2.19.0: version "2.19.0" resolved "https://registry.yarnpkg.com/apollo-server-micro/-/apollo-server-micro-2.19.0.tgz#911a397d234340c7ab4c9abb242c74503897849e"