upgrade to apollo server 4

This commit is contained in:
Moriz Wahl 2023-12-05 19:46:03 +01:00
parent 7c838d598e
commit 69c6b3e768
9 changed files with 350 additions and 561 deletions

815
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@
"update": "npx npm-check-update" "update": "npx npm-check-update"
}, },
"dependencies": { "dependencies": {
"apollo-server-express": "^3.13.0", "@apollo/server": "^4.9.5",
"graphql": "^16.8.1", "graphql": "^16.8.1",
"graphql-scalars": "^1.22.4", "graphql-scalars": "^1.22.4",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
@ -34,7 +34,6 @@
}, },
"devDependencies": { "devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.1.0", "@eslint-community/eslint-plugin-eslint-comments": "^4.1.0",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.10", "@types/jest": "^29.5.10",
"@types/node": "^20.10.1", "@types/node": "^20.10.1",
"@typescript-eslint/eslint-plugin": "^6.13.1", "@typescript-eslint/eslint-plugin": "^6.13.1",
@ -66,7 +65,8 @@
}, },
"imports": { "imports": {
"#graphql/*": "./src/graphql/*", "#graphql/*": "./src/graphql/*",
"#types/*": "./src/graphql/types/*", "#src/*": "./src/*",
"#test/*": "./test/*" "#test/*": "./test/*",
"#types/*": "./src/graphql/types/*"
} }
} }

View File

@ -1,20 +1,25 @@
import { ApolloServerTestClient } from 'apollo-server-testing' import { ApolloServer } from '@apollo/server'
import { testEnvironment } from '#test/helpers' import { createServer } from '#src/server/server'
let query: ApolloServerTestClient['query'] let testServer: ApolloServer
beforeAll(async () => { beforeAll(async () => {
const testEnv = await testEnvironment() testServer = await createServer()
query = testEnv.query
}) })
describe('HelloResolver', () => { describe('HelloResolver', () => {
it('return "Hello World!"', async () => { it('return "Hello World!"', async () => {
await expect(query({ query: '{ hello { hello } }' })).resolves.toMatchObject({ const response = await testServer.executeOperation({
data: { query: '{ hello { hello } }',
hello: { })
hello: 'Hello world!', expect(response.body).toMatchObject({
kind: 'single',
singleResult: {
data: {
hello: {
hello: 'Hello world!',
},
}, },
}, },
}) })

View File

@ -1,5 +1,3 @@
import path from 'path'
import { GraphQLSchema } from 'graphql' import { GraphQLSchema } from 'graphql'
import { buildSchema } from 'type-graphql' import { buildSchema } from 'type-graphql'

View File

@ -3,9 +3,9 @@ import 'reflect-metadata'
import { listen } from './server/server' import { listen } from './server/server'
export async function main() { export async function main() {
await listen(4000) const url = await listen(4000)
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log('🚀 Server is ready at http://localhost:4000/graphql') console.log(`🚀 Server is ready at ${url}`)
} }
main().catch((e) => { main().catch((e) => {

View File

@ -1,24 +1,20 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars import { startStandaloneServer } from '@apollo/server/standalone'
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { listen } from './server' import { listen } from './server'
jest.mock('express', () => { jest.mock('@apollo/server/standalone', () => {
const originalModule = jest.requireActual('express') const originalModule = jest.requireActual('@apollo/server/standalone')
return { return {
__esModule: true, __esModule: true,
...originalModule, ...originalModule,
default: jest.fn(() => { startStandaloneServer: jest.fn(() => {
return { return {
listen: jest.fn(), url: 'url',
} }
}), }),
} }
}) })
jest.mock('apollo-server-express')
describe('server', () => { describe('server', () => {
describe('listen', () => { describe('listen', () => {
beforeEach(async () => { beforeEach(async () => {
@ -26,8 +22,8 @@ describe('server', () => {
await listen(4000) await listen(4000)
}) })
it('calls express', () => { it('calls startStandaloneServer', () => {
expect(express).toBeCalled() expect(startStandaloneServer).toBeCalled()
}) })
}) })
}) })

View File

@ -1,18 +1,18 @@
import { ApolloServer } from 'apollo-server-express' import { ApolloServer } from '@apollo/server'
import express from 'express' import { startStandaloneServer } from '@apollo/server/standalone'
import { schema } from '#graphql/schema' import { schema } from '#graphql/schema'
export async function listen(port: number) { export const createServer = async (): Promise<ApolloServer> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any return new ApolloServer({
const app: any = express()
const server = new ApolloServer({
schema: await schema(), schema: await schema(),
}) })
await server.start() }
server.applyMiddleware({ app, path: '/' }) export async function listen(port: number) {
const { url } = await startStandaloneServer(await createServer(), {
return app.listen(port) listen: { port },
})
return url
} }

View File

@ -1,12 +0,0 @@
import { ApolloServer } from 'apollo-server-express'
import { createTestClient } from 'apollo-server-testing'
import { schema } from '#graphql/schema'
export const testEnvironment = async () => {
const server = new ApolloServer({
schema: await schema(),
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return createTestClient(server as any)
}

View File

@ -9,8 +9,9 @@
"lib": ["ESNext"], "lib": ["ESNext"],
"paths": { "paths": {
"#graphql/*": ["./src/graphql/*"], "#graphql/*": ["./src/graphql/*"],
"#types/*": ["./src/graphql/types/*"], "#src/*": ["./src/*"],
"#test/*": ["./test/*"] "#test/*": ["./test/*"],
"#types/*": ["./src/graphql/types/*"]
}, },
"outDir": "./build/src", "outDir": "./build/src",
"esModuleInterop": true, "esModuleInterop": true,