Apollo

ApolloBundle quickly bootstraps an Apollo Server within Kaviar's framework. It injects the container inside the context and allows you to easily create server-side routes.

Usage#

import { ApolloBundle } from "@kaviar/apollo-bundle";
kernel.addBundle(
new ApolloBundle({
// (optional) Apollo additional configuration
apollo: ApolloServerExpressConfig,
// (optional) Whether to install websocket handlers
enableSubscriptions: false,
// (optional) Express middlewares:
middlewares: [],
// (optional) Server Side Routes
// You can also add them from your bundle via `.addRoute()`
routes: [
{
type: "post", // "get", "put", "all"
path: "/api/payment-handler/:orderId",
handler: async (container, req, res) => {},
// These are optional and used for body-parsing
json: true,
urlencoded: true,
},
],
})
);

Loading API Definitions#

import { Bundle } from "@kaviar/core";
import { Loader } from "@kaviar/graphql-bundle";
class CoreBundle extends Bundle {
prepare() {
const loader = this.get<Loader>(Loader);
loader.load({
typeDefs: `
type Query {
sayHello: String
}
`,
resolvers: {
Query: {
sayHello: (_, args, ctx) => {
// You have access to the kernel container via: ctx.container
return "Hello world!";
},
},
},
contextReducers: async function storeUser(ctx) {
// Note that if you have subscriptions enabled
// You'll have to read from connectionParams rather than req.
return {
...ctx,
userId: "XXX",
};
},
});
}
}