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"
},
"dependencies": {
"apollo-server-express": "^3.13.0",
"@apollo/server": "^4.9.5",
"graphql": "^16.8.1",
"graphql-scalars": "^1.22.4",
"reflect-metadata": "^0.1.13",
@ -34,7 +34,6 @@
},
"devDependencies": {
"@eslint-community/eslint-plugin-eslint-comments": "^4.1.0",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.10",
"@types/node": "^20.10.1",
"@typescript-eslint/eslint-plugin": "^6.13.1",
@ -66,7 +65,8 @@
},
"imports": {
"#graphql/*": "./src/graphql/*",
"#types/*": "./src/graphql/types/*",
"#test/*": "./test/*"
"#src/*": "./src/*",
"#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 () => {
const testEnv = await testEnvironment()
query = testEnv.query
testServer = await createServer()
})
describe('HelloResolver', () => {
it('return "Hello World!"', async () => {
await expect(query({ query: '{ hello { hello } }' })).resolves.toMatchObject({
data: {
hello: {
hello: 'Hello world!',
const response = await testServer.executeOperation({
query: '{ hello { hello } }',
})
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 { buildSchema } from 'type-graphql'

View File

@ -3,9 +3,9 @@ import 'reflect-metadata'
import { listen } from './server/server'
export async function main() {
await listen(4000)
const url = await listen(4000)
// 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) => {

View File

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

View File

@ -1,18 +1,18 @@
import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { ApolloServer } from '@apollo/server'
import { startStandaloneServer } from '@apollo/server/standalone'
import { schema } from '#graphql/schema'
export async function listen(port: number) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const app: any = express()
const server = new ApolloServer({
export const createServer = async (): Promise<ApolloServer> => {
return new ApolloServer({
schema: await schema(),
})
await server.start()
server.applyMiddleware({ app, path: '/' })
return app.listen(port)
}
export async function listen(port: number) {
const { url } = await startStandaloneServer(await createServer(), {
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"],
"paths": {
"#graphql/*": ["./src/graphql/*"],
"#types/*": ["./src/graphql/types/*"],
"#test/*": ["./test/*"]
"#src/*": ["./src/*"],
"#test/*": ["./test/*"],
"#types/*": ["./src/graphql/types/*"]
},
"outDir": "./build/src",
"esModuleInterop": true,