Skip to content

Commit

Permalink
Upgrade to apollo v4 (#1051)
Browse files Browse the repository at this point in the history
* Upgrade to apollo v4

* enableTypes: json
  • Loading branch information
AaronMoat authored Jan 1, 2024
1 parent bfafa54 commit 6890c18
Show file tree
Hide file tree
Showing 9 changed files with 1,278 additions and 1,098 deletions.
6 changes: 6 additions & 0 deletions .changeset/short-months-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'wingman-be': minor
---

Update Apollo server to v4.
As a consequence, the following imports are removed: `ApolloError`, `AuthenticationError`, `ForbiddenError`, `UserInputError`.
12 changes: 7 additions & 5 deletions be/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
"test:ci": "skuba test --coverage"
},
"dependencies": {
"@apollo/server": "^4.9.5",
"@as-integrations/koa": "^1.1.1",
"@graphql-tools/utils": "^10.0.0",
"@graphql-tools/wrap": "^10.0.0",
"apollo-server-core": "^3.10.2",
"apollo-server-koa": "^3.6.1",
"graphql": "^16.8.1",
"koa": "^2.13.4",
"koa-bodyparser": "^4.3.0",
"koa": "^2.15.0",
"koa-bodyparser": "^4.4.1",
"koa-compose": "^4.1.0",
"lru-cache": "^10.0.0",
"node-fetch": "^2.6.6",
Expand All @@ -46,10 +46,12 @@
"@koa/cors": "5.0.0",
"@koa/router": "12.0.1",
"@types/koa": "2.13.12",
"@types/koa-bodyparser": "4.3.12",
"@types/koa__cors": "5.0.0",
"@types/koa__router": "12.0.4",
"@types/lru-cache": "7.10.10",
"@types/node-fetch": "2.6.10",
"@types/supertest": "2.0.16",
"@types/supertest": "6.0.2",
"nock": "13.4.0",
"skuba": "7.3.1",
"skuba-dive": "2.0.0",
Expand Down
7 changes: 0 additions & 7 deletions be/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
export {
ApolloError,
AuthenticationError,
ForbiddenError,
UserInputError,
} from 'apollo-server-koa';

export { createBrowserTokenMiddleware } from './browserToken/middleware';
export type { BrowserTokenMiddlewareOptions } from './browserToken/types';
export type { GetPartnerToken } from './getPartnerToken';
Expand Down
42 changes: 17 additions & 25 deletions be/src/seekGraph/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ describe('createContext', () => {
[
'extracts accept-language header',
{
ctx: {
request: {
header: {
authorization: 'Bearer in',
'accept-language': 'EN',
},
request: {
header: {
authorization: 'Bearer in',
'accept-language': 'EN',
},
},
},
Expand All @@ -22,11 +20,9 @@ describe('createContext', () => {
[
'extracts a sole authorization header',
{
ctx: {
request: {
header: {
authorization: 'Bearer in',
},
request: {
header: {
authorization: 'Bearer in',
},
},
},
Expand All @@ -37,17 +33,15 @@ describe('createContext', () => {
[
'extracts an authorization header while ignoring other cruft',
{
ctx: {
request: {
header: {
authorization: 'Bearer in',
'user-agent': 'my-service/1.2.3',
},
},
response: {
status: 404,
request: {
header: {
authorization: 'Bearer in',
'user-agent': 'my-service/1.2.3',
},
},
response: {
status: 404,
},
},
{
authorization: 'Bearer in',
Expand All @@ -56,11 +50,9 @@ describe('createContext', () => {
[
'ignores another header',
{
ctx: {
request: {
header: {
'user-agent': 'my-service/1.2.3',
},
request: {
header: {
'user-agent': 'my-service/1.2.3',
},
},
},
Expand Down
14 changes: 6 additions & 8 deletions be/src/seekGraph/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ import * as t from 'runtypes';
import type { RetrieveRequest } from '../getPartnerToken';

const IncomingContext = t.Record({
ctx: t.Record({
request: t.Record({
header: t.Record({
authorization: t.String,
'accept-language': t.String.optional(),
}),
request: t.Record({
header: t.Record({
authorization: t.String,
'accept-language': t.String.optional(),
}),
}),
});
Expand All @@ -22,8 +20,8 @@ export const createContext = (context: unknown): RetrieveRequest => {

if (result.success) {
return {
authorization: result.value.ctx.request.header.authorization,
'accept-language': result.value.ctx.request.header['accept-language'],
authorization: result.value.request.header.authorization,
'accept-language': result.value.request.header['accept-language'],
};
}

Expand Down
10 changes: 7 additions & 3 deletions be/src/seekGraph/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AuthenticationError } from 'apollo-server-koa';
import { GraphQLError } from 'graphql';
import Koa from 'koa';
import nock from 'nock';

Expand All @@ -23,7 +23,9 @@ describe('createSeekGraphMiddleware', () => {
userAgent: 'abc/1.2.3',
});

return new Koa().use(middleware);
const app = new Koa();
app.use(middleware);
return app;
});

beforeAll(agent.setup);
Expand Down Expand Up @@ -59,7 +61,9 @@ describe('createSeekGraphMiddleware', () => {
it('blocks an unauthorised request', async () => {
const message = 'no creds';

getPartnerToken.mockRejectedValue(new AuthenticationError(message));
getPartnerToken.mockRejectedValue(
new GraphQLError(message, { extensions: { code: 'UNAUTHENTICATED' } }),
);

const response = await agent()
.post('/custom')
Expand Down
30 changes: 19 additions & 11 deletions be/src/seekGraph/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable new-cap */

import {
ApolloServerPluginLandingPageDisabled,
ApolloServerPluginLandingPageGraphQLPlayground,
} from 'apollo-server-core';
import { ApolloServer } from 'apollo-server-koa';
import type { Middleware } from 'koa';
import { ApolloServer } from '@apollo/server';
import { ApolloServerPluginLandingPageDisabled } from '@apollo/server/plugin/disabled';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';
import { koaMiddleware } from '@as-integrations/koa';
import Router from '@koa/router';
import bodyParser from 'koa-bodyparser';

import { createContext } from './context';
import { createSchema } from './schema';
Expand All @@ -24,26 +24,34 @@ export const createSeekGraphMiddleware = async ({
path,
userAgent,
seekApiUrlOverride,
}: SeekGraphMiddlewareOptions): Promise<Middleware> => {
}: SeekGraphMiddlewareOptions): Promise<Router.Middleware> => {
const schema = await createSchema({
getPartnerToken,
userAgent,
seekApiUrlOverride,
});

const server = new ApolloServer({
context: createContext,
cache: 'bounded',
csrfPrevention: true,
introspection: debug,
debug,
plugins: [
debug
? ApolloServerPluginLandingPageGraphQLPlayground()
? ApolloServerPluginLandingPageLocalDefault()
: ApolloServerPluginLandingPageDisabled(),
],
schema,
});

await server.start();

return server.getMiddleware({ path });
const router = new Router().post(
path,
bodyParser({ enableTypes: ['json'] }),
koaMiddleware(server, {
context: ({ ctx }) => Promise.resolve(createContext(ctx)),
}),
);

return router.middleware();
};
2 changes: 1 addition & 1 deletion be/src/testing/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import request from 'supertest';
export const createAgent = (createApp: () => Koa | Promise<Koa>) => {
let server: Server;

let _agent: request.SuperTest<request.Test>;
let _agent: ReturnType<typeof request.agent>;

const agent = () => _agent;

Expand Down
Loading

0 comments on commit 6890c18

Please sign in to comment.