API参考:expressMiddleware
此API参考文档了Apollo Server 4的 Express集成,the expressMiddleware
函数。
expressMiddleware
函数 expressMiddleware
允许您将 Apollo Server 连接到 Express 服务器。
函数 expressMiddleware
预期您为您的 веб 框架设置 HTTP 请求体解析和 CORS 头部。具体来说,您可以使用 Express 本地的 express.json()
函数(自 Express v4.16.0
开始)和 cors
包来设置您的 Express 应用程序,如下所示。
有关配置项目的 CORS 行为的指导,请参阅 配置 CORS。
函数 expressMiddleware
接受两个 参数。第一个 必需的 参数 是一个 ApolloServer
实例,该实例通过调用其 start
方法已启动:
import { ApolloServer } from '@apollo/server';import { expressMiddleware } from '@apollo/server/express4';import cors from 'cors';import express from 'express';const app = express();const server = new ApolloServer<MyContext>({typeDefs,resolvers,});// Note you must call `start()` on the `ApolloServer`// instance before passing the instance to `expressMiddleware`await server.start();// Specify the path where we'd like to mount our serverapp.use('/graphql', cors<cors.CorsRequest>(), express.json(), expressMiddleware(server));
import { ApolloServer } from '@apollo/server';import { expressMiddleware } from '@apollo/server/express4';import cors from 'cors';import express from 'express';const app = express();const server = new ApolloServer({typeDefs,resolvers,});// Note you must call `start()` on the `ApolloServer`// instance before passing the instance to `expressMiddleware`await server.start();// Specify the path where we'd like to mount our serverapp.use('/graphql', cors(), express.json(), expressMiddleware(server));
⚠️ 为确保您的服务器平稳关闭,我们建议您使用 ApolloServerPluginDrainHttpServer
插件。下面是一个示例。。
该 expressMiddleware
函数的第二个可选 对象 用于配置 ApolloServer
,它可以包含以下选项:
名称 / 类型 | 描述 |
---|---|
| 一个可选的异步 该 该 |
示例
下面是设置 expressMiddleware
的完整示例:
// npm install @apollo/server express graphql corsimport { ApolloServer } from '@apollo/server';import { expressMiddleware } from '@apollo/server/express4';import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';import express from 'express';import http from 'http';import cors from 'cors';import { typeDefs, resolvers } from './schema';interface MyContext {token?: string;}// Required logic for integrating with Expressconst app = express();// Our httpServer handles incoming requests to our Express app.// Below, we tell Apollo Server to "drain" this httpServer,// enabling our servers to shut down gracefully.const httpServer = http.createServer(app);// Same ApolloServer initialization as before, plus the drain plugin// for our httpServer.const server = new ApolloServer<MyContext>({typeDefs,resolvers,plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],});// Ensure we wait for our server to startawait server.start();// Set up our Express middleware to handle CORS, body parsing,// and our expressMiddleware function.app.use('/',cors<cors.CorsRequest>(),express.json(),// expressMiddleware accepts the same arguments:// an Apollo Server instance and optional configuration optionsexpressMiddleware(server, {context: async ({ req }) => ({ token: req.headers.token }),}),);// Modified server startupawait new Promise<void>((resolve) => httpServer.listen({ port: 4000 }, resolve));console.log(`🚀 Server ready at https://127.0.0.1:4000/`);
// npm install @apollo/server express graphql corsimport { ApolloServer } from '@apollo/server';import { expressMiddleware } from '@apollo/server/express4';import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer';import express from 'express';import http from 'http';import cors from 'cors';import { typeDefs, resolvers } from './schema';// Required logic for integrating with Expressconst app = express();// Our httpServer handles incoming requests to our Express app.// Below, we tell Apollo Server to "drain" this httpServer,// enabling our servers to shut down gracefully.const httpServer = http.createServer(app);// Same ApolloServer initialization as before, plus the drain plugin// for our httpServer.const server = new ApolloServer({typeDefs,resolvers,plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],});// Ensure we wait for our server to startawait server.start();// Set up our Express middleware to handle CORS, body parsing,// and our expressMiddleware function.app.use('/',cors(),express.json(),// expressMiddleware accepts the same arguments:// an Apollo Server instance and optional configuration optionsexpressMiddleware(server, {context: async ({ req }) => ({ token: req.headers.token }),}),);// Modified server startupawait new Promise((resolve) => httpServer.listen({ port: 4000 }, resolve));console.log(`🚀 Server ready at https://127.0.0.1:4000/`);