Migrate to Apollo Server v4.

This commit is contained in:
Jayden Seric 2023-01-14 10:59:02 +11:00
parent 3a814a90db
commit a5da903448
5 changed files with 609 additions and 409 deletions

946
api/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -20,16 +20,21 @@
"npm": ">= 7" "npm": ">= 7"
}, },
"dependencies": { "dependencies": {
"apollo-server-koa": "^3.11.1", "@apollo/server": "^4.3.0",
"@as-integrations/koa": "^0.2.1",
"@koa/cors": "^4.0.0",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"graphql": "^16.6.0", "graphql": "^16.6.0",
"graphql-upload": "^16.0.2", "graphql-upload": "^16.0.2",
"koa": "^2.14.1", "koa": "^2.14.1",
"koa-bodyparser": "^4.3.0",
"make-dir": "^3.1.0", "make-dir": "^3.1.0",
"shortid": "^2.2.16" "shortid": "^2.2.16"
}, },
"devDependencies": { "devDependencies": {
"@types/koa": "^2.13.5", "@types/koa": "^2.13.5",
"@types/koa__cors": "^3.3.0",
"@types/koa-bodyparser": "^4.3.10",
"@types/node": "^18.11.18", "@types/node": "^18.11.18",
"eslint": "^8.31.0", "eslint": "^8.31.0",
"eslint-plugin-simple-import-sort": "^8.0.0", "eslint-plugin-simple-import-sort": "^8.0.0",

View File

@ -3,8 +3,8 @@
An example GraphQL API using: An example GraphQL API using:
- [`koa`](https://npm.im/koa) - [`koa`](https://npm.im/koa)
- [`apollo-server-koa`](https://npm.im/apollo-server-koa)
- [`graphql-upload`](https://npm.im/graphql-upload) - [`graphql-upload`](https://npm.im/graphql-upload)
- [`@as-integrations/koa`](https://npm.im/@as-integrations/koa)
## Installation ## Installation

View File

@ -2,39 +2,45 @@
import { fileURLToPath } from "node:url"; import { fileURLToPath } from "node:url";
import { ApolloServer } from "apollo-server-koa"; import { ApolloServer } from "@apollo/server";
import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
import { koaMiddleware as apolloServerKoa } from "@as-integrations/koa";
import corsKoa from "@koa/cors";
import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.mjs"; import graphqlUploadKoa from "graphql-upload/graphqlUploadKoa.mjs";
import http from "http";
import Koa from "koa"; import Koa from "koa";
import bodyParserKoa from "koa-bodyparser";
import makeDir from "make-dir"; import makeDir from "make-dir";
import UPLOAD_DIRECTORY_URL from "./config/UPLOAD_DIRECTORY_URL.mjs"; import UPLOAD_DIRECTORY_URL from "./config/UPLOAD_DIRECTORY_URL.mjs";
import schema from "./schema/index.mjs"; import schema from "./schema/index.mjs";
/** Starts the API server. */ // Ensure the upload directory exists.
async function startServer() { await makeDir(fileURLToPath(UPLOAD_DIRECTORY_URL));
// Ensure the upload directory exists.
await makeDir(fileURLToPath(UPLOAD_DIRECTORY_URL));
const apolloServer = new ApolloServer({ schema }); const app = new Koa();
const httpServer = http.createServer(app.callback());
const apolloServer = new ApolloServer({
schema,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await apolloServer.start(); await apolloServer.start();
new Koa() app.use(corsKoa());
.use( app.use(
graphqlUploadKoa({ graphqlUploadKoa({
// Limits here should be stricter than config for surrounding // Limits here should be stricter than config for surrounding infrastructure
// infrastructure such as Nginx so errors can be handled elegantly by // such as NGINX so errors can be handled elegantly by `graphql-upload`.
// `graphql-upload`. maxFileSize: 10000000, // 10 MB
maxFileSize: 10000000, // 10 MB maxFiles: 20,
maxFiles: 20, })
}) );
) app.use(bodyParserKoa());
.use(apolloServer.getMiddleware()) app.use(apolloServerKoa(apolloServer));
.listen(process.env.PORT, () => {
console.info(
`Serving http://localhost:${process.env.PORT} for ${process.env.NODE_ENV}.`
);
});
}
startServer(); httpServer.listen(process.env.PORT, () => {
console.info(
`Serving http://localhost:${process.env.PORT} for ${process.env.NODE_ENV}.`
);
});

View File

@ -29,7 +29,12 @@ const createApolloClient = (cache = {}) =>
new ApolloClient({ new ApolloClient({
ssrMode: typeof window === "undefined", ssrMode: typeof window === "undefined",
cache: new InMemoryCache().restore(cache), cache: new InMemoryCache().restore(cache),
link: createUploadLink({ uri: process.env.API_URI }), link: createUploadLink({
uri: process.env.API_URI,
headers: {
"Apollo-Require-Preflight": "true",
},
}),
}); });
/** /**