Apollo§
要使用 Unit 运行 Apollo GraphQL 服务器
使用 unit-dev/unit-devel 包安装 Unit。接下来,安装 Unit 的 unit-http 包
# npm install -g --unsafe-perm unit-http
创建你的应用目录,安装 Apollo,并链接 unit-http
$ mkdir -p /path/to/app/ $ cd /path/to/app/ $ npm install @apollo/server graphql # npm link unit-http
创建 中间件 模块;我们将其存储为 /path/to/app/apollo.js。首先,初始化目录
$ cd /path/to/app/ $ npm init
接下来,添加以下代码
import { 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 bodyParser from 'body-parser'; //import { typeDefs, resolvers } from './schema'; const typeDefs = `#graphql type Query { hello: String } `; // A map of functions which return data for the schema. const resolvers = { Query: { hello: () => 'world', }, }; // Required logic for integrating with Express const 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 start await server.start(); // Set up our Express middleware to handle CORS, body parsing, // and our expressMiddleware function. app.use( '/', cors(), bodyParser.json(), // expressMiddleware accepts the same arguments: // an Apollo Server instance and optional configuration options expressMiddleware(server, { context: async ({ req }) => ({ token: req.headers.token }), }), ); // Modified server startup; port number is overridden by Unit config await new Promise((resolve) => httpServer.listen({ port: 80 }, resolve));
确保你的 package.json 类似于此(注意 “type”: “module”)
{ "name": "unit-apollo", "version": "1.0.0", "description": "Running Apollo over Express on Unit", "main": "index.js", "type": "module", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Unit Team", "license": "ISC", "dependencies": { "@apollo/server": "^4.7.5", "apollo-server": "^3.12.0", "body-parser": "^1.20.2", "cors": "^2.8.5", "express": "^4.18.2", "graphql": "^16.7.1", "unit-http": "^1.30.0" } }
运行以下命令,以便 Unit 可以访问 应用程序目录
# chown -R unit:unit /path/to/app/
有关包括权限在内的更多详细信息,请参阅安全检查清单。
接下来,准备Apollo配置以供Unit使用
{ "listeners": { "*:80": { "pass": "applications/apollo" } }, "applications": { "apollo": { "type": "external", "working_directory": "/path/to/app/", "executable": "/usr/bin/env", "arguments": [ "node", "--loader", "unit-http/loader.mjs", "--require", "unit-http/loader", "apollo.js" ] } } }
上传更新后的配置。假设上述JSON已添加到
config.json
# curl -X PUT --data-binary @config.json --unix-socket \ /path/to/control.unit.sock http://localhost/config/
更新成功后,您的应用应可在侦听器的IP地址和端口上使用