Skip to content

Commit

Permalink
feat: createSchema now returns both schema and context.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel910 committed Feb 8, 2020
1 parent 331627c commit 6a568d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
14 changes: 9 additions & 5 deletions packages/api/src/createHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
PluginsContainer,
GraphQLMiddlewarePlugin,
GraphQLContextPlugin,
CreateApolloHandlerPlugin
CreateApolloHandlerPlugin,
GraphQLContext
} from "./types";
import { prepareSchema } from "./graphql/prepareSchema";

Expand All @@ -18,8 +19,11 @@ type CreateHandlerParams = {
* @param plugins
* @returns {Promise<void>}
*/
export const createSchema = async ({ plugins }: CreateHandlerParams): Promise<GraphQLSchema> => {
let schema = await prepareSchema({ plugins });
export const createSchema = async ({
plugins
}: CreateHandlerParams): Promise<{ schema: GraphQLSchema; context: GraphQLContext }> => {
// eslint-disable-next-line prefer-const
let { schema, context } = await prepareSchema({ plugins });

const registeredMiddleware = [];

Expand Down Expand Up @@ -67,14 +71,14 @@ export const createSchema = async ({ plugins }: CreateHandlerParams): Promise<Gr
}
});

return schema;
return { schema, context };
};

/**
* Create Apollo handler
*/
export const createHandler = async ({ plugins }: CreateHandlerParams) => {
const schema = await createSchema({ plugins });
const { schema } = await createSchema({ plugins });

const plugin = plugins.byName<CreateApolloHandlerPlugin>("create-apollo-handler");

Expand Down
6 changes: 3 additions & 3 deletions packages/api/src/graphql/prepareSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
GraphQLSchemaPlugin,
GraphqlScalarPlugin,
SchemaDefinition,
GraphQLBeforeSchemaPlugin, GraphQLContext
GraphQLBeforeSchemaPlugin,
GraphQLContext
} from "../types";

type PrepareSchemaParams = { plugins: PluginsContainer };
Expand Down Expand Up @@ -69,6 +70,5 @@ export async function prepareSchema({ plugins }: PrepareSchemaParams) {
}
}

// @ts-ignore
return buildFederatedSchema([...schemaDefs]);
return { schema: buildFederatedSchema([...schemaDefs]), context };
}
9 changes: 1 addition & 8 deletions packages/api/src/testing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import { GraphQLContextPlugin } from "@webiny/api/types";
export const setupSchema = async plugins => {
const pluginsContainer = new PluginsContainer([plugins]);

const schema = await createSchema({ plugins: pluginsContainer });

const context = { plugins: pluginsContainer };

return {
schema,
context
};
return await createSchema({ plugins: pluginsContainer });
};

export const setupContext = async (plugins, baseContext = {}) => {
Expand Down

0 comments on commit 6a568d0

Please sign in to comment.