Skip to content

Commit

Permalink
fix: process graphql-context plugins on schema creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Feb 11, 2020
1 parent 1a42134 commit 50ce005
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 75 deletions.
3 changes: 2 additions & 1 deletion packages/api-i18n/src/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const plugin: GraphQLContextPlugin<APIContext & I18NContext> = {
}

const { event } = context;

const self = {
__i18n: {
acceptLanguage: null,
Expand All @@ -39,7 +40,7 @@ const plugin: GraphQLContextPlugin<APIContext & I18NContext> = {
const allLocales = self.getLocales();
const acceptLanguage = acceptLanguageParser.pick(
allLocales.map(item => item.code),
event.headers["accept-language"]
event ? event.headers["accept-language"] : null
);

let currentLocale;
Expand Down
13 changes: 3 additions & 10 deletions packages/api-plugin-commodo-db-proxy/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DbProxyDriver, id, isId, withId } from "@webiny/commodo-fields-storage-db-proxy";
import {GraphQLBeforeSchemaPlugin, GraphQLContextPlugin} from "@webiny/api/types";
import { GraphQLContextPlugin } from "@webiny/api/types";

function apply(context, options) {
if (!context.commodo) {
Expand All @@ -17,19 +17,12 @@ function apply(context, options) {
context.commodo.driver = new DbProxyDriver({ dbProxyFunctionName: options.functionArn });
}

export default (options) => [
export default options => [
{
name: "graphql-context-commodo",
type: "graphql-context",
preApply(context) {
return apply(context, options);
}
} as GraphQLContextPlugin,
{
name: "before-schema-commodo",
type: "before-schema",
apply(context) {
return apply(context, options);
}
} as GraphQLBeforeSchemaPlugin
} as GraphQLContextPlugin
];
11 changes: 2 additions & 9 deletions packages/api-plugin-commodo-mongodb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MongoClient } from "mongodb";
import { MongoDbDriver, id } from "@commodo/fields-storage-mongodb";
import { GraphQLBeforeSchemaPlugin, GraphQLContextPlugin } from "@webiny/api/types";
import { GraphQLContextPlugin } from "@webiny/api/types";

let database = null;
let client = null;
Expand Down Expand Up @@ -66,13 +66,6 @@ export default (options: CommodoMongoDBFactoryOptions) => {
preApply(context) {
return setup(options, context);
}
} as GraphQLContextPlugin,
{
name: "before-schema-commodo-mongodb",
type: "before-schema",
apply(context) {
return setup(options, context);
}
} as GraphQLBeforeSchemaPlugin
} as GraphQLContextPlugin
];
};
21 changes: 2 additions & 19 deletions packages/api/src/createHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { GraphQLSchema } from "graphql";
import {
PluginsContainer,
GraphQLMiddlewarePlugin,
GraphQLContextPlugin,
CreateApolloHandlerPlugin,
GraphQLContext
} from "./types";
import { prepareSchema } from "./graphql/prepareSchema";
import { applyGraphQLContextPlugins } from "./utils/contextPlugins";

type CreateHandlerParams = {
plugins: PluginsContainer;
Expand Down Expand Up @@ -51,24 +51,7 @@ export const createSchema = async ({
delete info.operation["__runAtMostOnce"];

// Process `graphql-context` plugins
const ctxPlugins = plugins.byType<GraphQLContextPlugin>("graphql-context");
for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].preApply === "function") {
await ctxPlugins[i].preApply(context);
}
}

for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].apply === "function") {
await ctxPlugins[i].apply(context);
}
}

for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].postApply === "function") {
await ctxPlugins[i].postApply(context);
}
}
await applyGraphQLContextPlugins(context);
});

return { schema, context };
Expand Down
7 changes: 2 additions & 5 deletions packages/api/src/graphql/prepareSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import {
GraphQLSchemaPlugin,
GraphqlScalarPlugin,
SchemaDefinition,
GraphQLBeforeSchemaPlugin,
GraphQLContext
} from "../types";
import { applyGraphQLContextPlugins } from "@webiny/api/utils/contextPlugins";

type PrepareSchemaParams = { plugins: PluginsContainer };

Expand All @@ -20,10 +20,7 @@ type PrepareSchemaParams = { plugins: PluginsContainer };
*/
export async function prepareSchema({ plugins }: PrepareSchemaParams) {
const context: GraphQLContext = { plugins };
const beforeSchemaPlugins = plugins.byType<GraphQLBeforeSchemaPlugin>("before-schema");
for (let i = 0; i < beforeSchemaPlugins.length; i++) {
await beforeSchemaPlugins[i].apply(context);
}
await applyGraphQLContextPlugins(context);

// This allows developers to register more plugins dynamically, before the graphql schema is instantiated.
const gqlPlugins = plugins.byType<GraphQLSchemaPlugin>("graphql-schema");
Expand Down
21 changes: 2 additions & 19 deletions packages/api/src/testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSchema } from "../index";
import { PluginsContainer } from "@webiny/plugins/PluginsContainer";
import { GraphQLContextPlugin } from "@webiny/api/types";
import { applyGraphQLContextPlugins } from "@webiny/api/utils/contextPlugins";

export const setupSchema = async plugins => {
const pluginsContainer = new PluginsContainer([plugins]);
Expand All @@ -14,24 +14,7 @@ export const setupContext = async (plugins, baseContext = {}) => {
const context = { ...baseContext, plugins: pluginsContainer };

// Process `graphql-context` plugins
const ctxPlugins = pluginsContainer.byType<GraphQLContextPlugin>("graphql-context");
for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].preApply === "function") {
await ctxPlugins[i].preApply(context);
}
}

for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].apply === "function") {
await ctxPlugins[i].apply(context);
}
}

for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].postApply === "function") {
await ctxPlugins[i].postApply(context);
}
}
await applyGraphQLContextPlugins(context);

return context;
};
12 changes: 0 additions & 12 deletions packages/api/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,6 @@ export type GraphQLContextPlugin<T = GraphQLContext> = Plugin & {
postApply?: (context: T) => void | Promise<void>;
};

/**
* These plugins are processed before Schema construction begins.
* It allows you to generate dynamic schema plugins and prepare the system for Schema generation.
*/
export type GraphQLBeforeSchemaPlugin = Plugin & {
type: "before-schema";
/**
* Modify context before schema construction begins.
*/
apply(context: GraphQLContext): void | Promise<void>;
};

export type GraphQLMiddlewarePlugin = Plugin & {
middleware: (params: { plugins: PluginsContainer }) => Function[];
};
Expand Down
22 changes: 22 additions & 0 deletions packages/api/src/utils/contextPlugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { GraphQLContext, GraphQLContextPlugin } from "@webiny/api/types";

export const applyGraphQLContextPlugins = async (context: GraphQLContext) => {
const ctxPlugins = context.plugins.byType<GraphQLContextPlugin>("graphql-context");
for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].preApply === "function") {
await ctxPlugins[i].preApply(context);
}
}

for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].apply === "function") {
await ctxPlugins[i].apply(context);
}
}

for (let i = 0; i < ctxPlugins.length; i++) {
if (typeof ctxPlugins[i].postApply === "function") {
await ctxPlugins[i].postApply(context);
}
}
};

0 comments on commit 50ce005

Please sign in to comment.