English | 简体中文
You can now happily integrate Apollo-Server and TypeGraphQL with Midway Serverless.
- Support most serverless configurations of
Apollo Server
andTypeGraphQL
. - Support
Serverless
application by usingApollo-Server
as interpreter and traditional Node application in a way of usingApollo-Server
as middlware,Koa
solution is available now. - Built-in out-of-the-box plugins. e.g. Query Complexity, Resolve Time, etc.
- Integrate with debug ability of
Midway Container
.(e.g. retrieving context andGraphQL Schema
fromGraphQLResponse#extensions
) - Based on Apollo Server V3,
Apollo Sandbox
is disabled by default, tryGraphQL Playground
instead. - Unit test coverage more than 90%.
To get started,you could try experimental-midway-sls-graphql and sample to get a glance at basic usage.
API Documentation is on its way 🐎...
See types.ts and preset-options.ts to check on supported options (Apollo、TypeGraphQL、Built-In Plugin)。
Use apollo-server-midway in Serverless:
npm install apollo-server-midway --save
yarn add apollo-server-midway --save
pnpm install apollo-server-midway --save
import {
Provide,
Inject,
ServerlessTrigger,
ServerlessFunction,
ServerlessTriggerType,
App,
} from "@midwayjs/decorator";
import { Context, IMidwayFaaSApplication } from "@midwayjs/faas";
import { createApolloServerHandler } from "apollo-server-midway";
import path from "path";
const apolloHandlerFuncName = "apollo-handler";
const APOLLO_SERVER_MIDWAY_PATH = "/apollo";
@Provide()
export class HelloHTTPService {
@Inject()
ctx: Context;
@App()
app: IMidwayFaaSApplication;
@ServerlessFunction({
functionName: apolloHandlerFuncName,
})
@ServerlessTrigger(ServerlessTriggerType.HTTP, {
path: APOLLO_SERVER_MIDWAY_PATH,
method: "get",
})
@ServerlessTrigger(ServerlessTriggerType.HTTP, {
path: APOLLO_SERVER_MIDWAY_PATH,
method: "post",
})
async apolloHandler() {
return await createApolloServerHandler({
path: "/",
app: this.app,
context: this.ctx,
// NOTE: schema 是必须的, 使用 schema.resolvers 或者 apollp.schema 来指定
// 一旦 apollo.schema 被指定,schema.resolvers 就将被忽略
schema: {
resolvers: [path.resolve(this.app.getBaseDir(), "resolvers/*")],
},
});
}
}
In the example above, function apollo-handler
will be deployed to SLS_DOMAIN/SERVICE/apollo-handler
. You can invoke it by visiting SLS_DOMAIN/SERVICE/apollo-handler/
. (Note: the tailing slash /
is needed).
Use apollo-server-midway in traditional Node app:
Refer to koa-app-sample for more details.
// config.default.ts
import { CreateGraphQLMiddlewareOption } from "apollo-server-midway";
export const graphql: CreateGraphQLMiddlewareOption = {};
// configuration.ts
import { Configuration, App } from "@midwayjs/decorator";
import { ILifeCycle } from "@midwayjs/core";
import { IMidwayKoaApplication } from "@midwayjs/koa";
import * as GraphQL from "apollo-server-midway";
@Configuration({
imports: [GraphQL],
importConfigs: ["./config"],
})
export class ContainerConfiguration implements ILifeCycle {
@App()
app: IMidwayKoaApplication;
async onReady(): Promise<void> {
// Component Namespace:Framework-Specified Middleware Identifier
// graphql:GraphQLExpressMiddleware
this.app.use(
await this.app.generateMiddleware("graphql:GraphQLKoaMiddleware")
);
}
}