Skip to content
This repository has been archived by the owner on Aug 14, 2021. It is now read-only.

Commit

Permalink
first pass at universal output for all packages #221
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias von Bargen committed Nov 26, 2020
1 parent d595c5b commit af58269
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions packages/base/src/Bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function collectPlugins(path = process.cwd()): Array<string> {
*
* @param plugin
*/
export async function importPlugin(plugin: string) {
export function importPlugin(plugin: string) {
try {
if (plugin[0] === "/") {
let pluginPath;
Expand All @@ -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);
Expand All @@ -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<string> {
export function bootstrapSchema(hoisted = false): string {
if (schema) return schema; //We already have it

/**
Expand Down Expand Up @@ -128,7 +128,7 @@ export async function bootstrapSchema(hoisted = false): Promise<string> {

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
Expand Down
25 changes: 13 additions & 12 deletions packages/base/src/Server.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -36,23 +37,23 @@ 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<ServerContext> {
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;
} else {
//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) {
Expand All @@ -76,11 +77,11 @@ export async function getServerContext({ req, res }): Promise<ServerContext> {
*
* @param context
*/
export async function CorejamServer(context = ({ req, res }) => getServerContext({ req, res })): Promise<ApolloServer> {
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({
Expand All @@ -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({
Expand All @@ -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,
});
};
}
2 changes: 1 addition & 1 deletion packages/base/src/client/ServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApolloServer> = CorejamServer();
const corejamServer = new ApolloServer(CorejamServer());

/**
* This function is used for our Stencil Hydrate render process.
Expand Down
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit af58269

Please sign in to comment.