Skip to content

Commit 97c1402

Browse files
committed
test: migrate server to typescript
1 parent f1ebe70 commit 97c1402

File tree

9 files changed

+136
-45
lines changed

9 files changed

+136
-45
lines changed

packages/test-server/bin.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env node
22
'use strict'
33

4-
import './src/index.mjs'
4+
import './dist/index.js'

packages/test-server/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"bin": {
66
"test-server": "./bin.mjs"
77
},
8+
"type": "module",
9+
"scripts": {
10+
"build": "tsc -d",
11+
"prepare": "pnpm run build"
12+
},
813
"dependencies": {
914
"@apollo/server": "^4.7.3",
1015
"@graphql-tools/schema": "^10.0.0",
@@ -19,6 +24,11 @@
1924
"ws": "^8.13.0"
2025
},
2126
"devDependencies": {
22-
"@types/shortid": "^0.0.29"
27+
"@types/body-parser": "^1.19.2",
28+
"@types/cors": "^2.8.13",
29+
"@types/express": "^4.17.17",
30+
"@types/shortid": "^0.0.29",
31+
"@types/ws": "^8.5.5",
32+
"typescript": "^4.7.4"
2333
}
2434
}

packages/test-server/src/data.mjs

Lines changed: 0 additions & 18 deletions
This file was deleted.

packages/test-server/src/data.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export interface Channel {
2+
id: string
3+
label: string
4+
messages: Message[]
5+
}
6+
7+
export interface Message {
8+
id: string
9+
channel: Channel
10+
text: string
11+
}
12+
13+
export let channels: Channel[] = []
14+
15+
export function resetDatabase (): void {
16+
channels = [
17+
{
18+
id: 'general',
19+
label: 'General',
20+
messages: [],
21+
},
22+
{
23+
id: 'random',
24+
label: 'Random',
25+
messages: [],
26+
},
27+
]
28+
}
29+
30+
resetDatabase()

packages/test-server/src/index.mjs renamed to packages/test-server/src/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import { ApolloServer } from '@apollo/server'
66
import { expressMiddleware } from '@apollo/server/express4'
77
import { WebSocketServer } from 'ws'
88
import { useServer } from 'graphql-ws/lib/use/ws'
9-
import { schema } from './schema.mjs'
10-
import { resetDatabase } from './data.mjs'
9+
import { schema } from './schema.js'
10+
import { resetDatabase } from './data.js'
11+
import { simulateLatency } from './util.js'
1112

1213
const app = express()
1314

14-
app.use(cors('*'))
15+
app.use(cors({
16+
origin: '*',
17+
}))
1518

1619
app.use(bodyParser.json())
1720

@@ -22,9 +25,6 @@ app.get('/_reset', (req, res) => {
2225

2326
const server = new ApolloServer({
2427
schema,
25-
context: () => new Promise(resolve => {
26-
setTimeout(() => resolve({}), 50)
27-
}),
2828
plugins: [
2929
// Proper shutdown for the WebSocket server.
3030
{
@@ -42,7 +42,12 @@ const server = new ApolloServer({
4242

4343
await server.start()
4444

45-
app.use('/graphql', expressMiddleware(server))
45+
app.use('/graphql', expressMiddleware(server, {
46+
context: async () => {
47+
await simulateLatency()
48+
return {}
49+
},
50+
}))
4651

4752
const httpServer = createServer(app)
4853

packages/test-server/src/schema.mjs renamed to packages/test-server/src/schema.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { makeExecutableSchema } from '@graphql-tools/schema'
22
import { PubSub, withFilter } from 'graphql-subscriptions'
33
import shortid from 'shortid'
44
import gql from 'graphql-tag'
5-
import { channels } from './data.mjs'
6-
import { simulateLatency, GraphQLErrorWithCode } from './util.mjs'
5+
import { channels } from './data.js'
6+
import { GraphQLErrorWithCode } from './util.js'
77

88
const pubsub = new PubSub()
99

@@ -51,21 +51,31 @@ type Subscription {
5151
}
5252
`
5353

54+
interface AddMessageInput {
55+
channelId: string
56+
text: string
57+
}
58+
59+
interface UpdateMessageInput {
60+
id: string
61+
channelId: string
62+
text: string
63+
}
64+
5465
const resolvers = {
5566
Query: {
56-
hello: () => simulateLatency().then(() => 'Hello world!'),
57-
channels: () => simulateLatency().then(() => channels),
58-
channel: (root, { id }) => simulateLatency().then(() => channels.find(c => c.id === id)),
59-
list: () => simulateLatency().then(() => ['a', 'b', 'c']),
60-
good: () => simulateLatency().then(() => 'good'),
67+
hello: () => 'Hello world!',
68+
channels: () => channels,
69+
channel: (root: any, { id }: { id: string }) => channels.find(c => c.id === id),
70+
list: () => ['a', 'b', 'c'],
71+
good: () => 'good',
6172
bad: async () => {
62-
await simulateLatency()
6373
throw new Error('An error')
6474
},
6575
},
6676

6777
Mutation: {
68-
addMessage: (root, { input }) => {
78+
addMessage: (root: any, { input }: { input: AddMessageInput }) => {
6979
const channel = channels.find(c => c.id === input.channelId)
7080
if (!channel) {
7181
throw new GraphQLErrorWithCode(`Channel ${input.channelId} not found`, 'not-found')
@@ -80,12 +90,15 @@ const resolvers = {
8090
return message
8191
},
8292

83-
updateMessage: (root, { input }) => {
93+
updateMessage: (root: any, { input }: { input: UpdateMessageInput }) => {
8494
const channel = channels.find(c => c.id === input.channelId)
8595
if (!channel) {
8696
throw new GraphQLErrorWithCode(`Channel ${input.channelId} not found`, 'not-found')
8797
}
8898
const message = channel.messages.find(m => m.id === input.id)
99+
if (!message) {
100+
throw new GraphQLErrorWithCode(`Message ${input.id} not found`, 'not-found')
101+
}
89102
Object.assign(message, {
90103
text: input.text,
91104
})

packages/test-server/src/util.mjs renamed to packages/test-server/src/util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLError } from 'graphql'
1+
import { GraphQLError, GraphQLErrorExtensions } from 'graphql'
22

33
const shouldSimulateLatency = process.argv.includes('--simulate-latency')
44

@@ -11,7 +11,7 @@ if (shouldSimulateLatency) {
1111
}
1212

1313
export function simulateLatency () {
14-
return new Promise(resolve => {
14+
return new Promise<void>(resolve => {
1515
if (shouldSimulateLatency) {
1616
setTimeout(resolve, latency)
1717
} else {
@@ -21,8 +21,8 @@ export function simulateLatency () {
2121
}
2222

2323
export class GraphQLErrorWithCode extends GraphQLError {
24-
constructor (message, code, extensions) {
25-
super(message, {
24+
constructor (message: string, code: string, extensions?: GraphQLErrorExtensions) {
25+
super(message, null, null, null, null, null, {
2626
extensions: {
2727
code,
2828
...extensions,

packages/test-server/tsconfig.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"module": "esnext",
5+
"strict": true,
6+
"importHelpers": true,
7+
"moduleResolution": "node",
8+
"skipLibCheck": true,
9+
"esModuleInterop": true,
10+
"allowSyntheticDefaultImports": true,
11+
"sourceMap": true,
12+
"lib": [
13+
"esnext"
14+
],
15+
"outDir": "dist",
16+
},
17+
"include": [
18+
"src/**/*.ts",
19+
],
20+
"exclude": [
21+
"node_modules"
22+
]
23+
}

pnpm-lock.yaml

Lines changed: 32 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)