Skip to content

Commit b105013

Browse files
authored
test: file-upload example (#1750)
1 parent b7aac32 commit b105013

File tree

3 files changed

+189
-44
lines changed

3 files changed

+189
-44
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import { yoga } from '../yoga'
2+
import { createServer, Server } from 'http'
3+
import { AddressInfo } from 'net'
4+
import { fetch, File, FormData } from '@whatwg-node/fetch'
5+
import * as fs from 'fs'
6+
import * as crypto from 'crypto'
7+
import * as path from 'path'
8+
9+
function md5File(path: string) {
10+
return new Promise((resolve, reject) => {
11+
const output = crypto.createHash('md5')
12+
const input = fs.createReadStream(path)
13+
14+
input.on('error', (err) => {
15+
reject(err)
16+
})
17+
18+
output.once('readable', () => {
19+
resolve(output.read().toString('hex'))
20+
})
21+
22+
input.pipe(output)
23+
})
24+
}
25+
26+
describe('graphql-auth example integration', () => {
27+
let server: Server
28+
let port: number
29+
30+
beforeAll(async () => {
31+
server = createServer(yoga)
32+
await new Promise<void>((resolve) => server.listen(0, resolve))
33+
port = (server.address() as AddressInfo).port
34+
})
35+
36+
afterAll(async () => {
37+
await new Promise((resolve) => server.close(resolve))
38+
})
39+
40+
it('should execute query', async () => {
41+
const response = await fetch(
42+
`http://localhost:${port}/graphql?query=query{hello}`,
43+
)
44+
const body = await response.json()
45+
expect(body.errors).toBeUndefined()
46+
expect(body.data).toEqual({
47+
hello: 'Hello World',
48+
})
49+
})
50+
51+
it('should save file', async () => {
52+
const sourceFilePath = path.join(
53+
__dirname,
54+
'..',
55+
'..',
56+
'..',
57+
'website',
58+
'public',
59+
'logo.png',
60+
)
61+
const sourceMd5 = await md5File(sourceFilePath)
62+
63+
const formData = new FormData()
64+
formData.set(
65+
'operations',
66+
JSON.stringify({
67+
query: /* GraphQL */ `
68+
mutation saveFile($file: File!) {
69+
saveFile(file: $file)
70+
}
71+
`,
72+
}),
73+
)
74+
formData.set('map', JSON.stringify({ 0: ['variables.file'] }))
75+
formData.set(
76+
'0',
77+
new File(
78+
[await fs.promises.readFile(sourceFilePath)],
79+
path.basename(sourceFilePath),
80+
{ type: 'image/png' },
81+
),
82+
)
83+
84+
const response = await fetch(`http://localhost:${port}/graphql`, {
85+
method: 'POST',
86+
body: formData,
87+
})
88+
89+
const body = await response.json()
90+
expect(body.errors).toBeUndefined()
91+
expect(body.data).toEqual({
92+
saveFile: true,
93+
})
94+
95+
const targetFilePath = path.join(__dirname, '..', 'logo.png')
96+
await fs.promises.stat(targetFilePath)
97+
const targetMd5 = await md5File(targetFilePath)
98+
expect(targetMd5).toEqual(sourceMd5)
99+
fs.promises.unlink(targetFilePath)
100+
expect(targetMd5).toBe(sourceMd5)
101+
})
102+
103+
it('should read file text', async () => {
104+
const sourceFilePath = path.join(
105+
__dirname,
106+
'..',
107+
'..',
108+
'..',
109+
'website',
110+
'public',
111+
'logo.png',
112+
)
113+
114+
const formData = new FormData()
115+
formData.set(
116+
'operations',
117+
JSON.stringify({
118+
query: /* GraphQL */ `
119+
mutation readTextFile($file: File!) {
120+
readTextFile(file: $file)
121+
}
122+
`,
123+
}),
124+
)
125+
formData.set('map', JSON.stringify({ 0: ['variables.file'] }))
126+
formData.set(
127+
'0',
128+
new File(
129+
[await fs.promises.readFile(sourceFilePath)],
130+
path.basename(sourceFilePath),
131+
{ type: 'image/png' },
132+
),
133+
)
134+
135+
const response = await fetch(`http://localhost:${port}/graphql`, {
136+
method: 'POST',
137+
body: formData,
138+
})
139+
140+
const body = await response.json()
141+
expect(body.errors).toBeUndefined()
142+
expect(body.data).toBeDefined()
143+
})
144+
})

examples/file-upload/index.ts

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,5 @@
1-
import { createYoga, createSchema } from 'graphql-yoga'
21
import http from 'http'
3-
import fs from 'fs'
4-
import path from 'path'
5-
6-
const yoga = createYoga({
7-
schema: createSchema({
8-
typeDefs: /* GraphQL */ `
9-
scalar File
10-
type Query {
11-
hello(name: String): String!
12-
}
13-
type Mutation {
14-
readTextFile(file: File!): String!
15-
saveFile(file: File!): Boolean!
16-
}
17-
`,
18-
resolvers: {
19-
Query: {
20-
hello: (_, { name }: { name: string }) => `Hello ${name || 'World'}`,
21-
},
22-
Mutation: {
23-
readTextFile: async (_, { file }: { file: File }) => {
24-
const fileContent = await file.text()
25-
return fileContent
26-
},
27-
saveFile: async (_, { file }: { file: File }) => {
28-
try {
29-
const fileStream = file.stream()
30-
await fs.promises.writeFile(
31-
path.join(__dirname, file.name),
32-
fileStream,
33-
)
34-
} catch (e) {
35-
return false
36-
}
37-
return true
38-
},
39-
},
40-
},
41-
}),
42-
graphiql: {
43-
title: 'Hello World',
44-
},
45-
})
2+
import { yoga } from './yoga'
463

474
const server = http.createServer(yoga)
485
server.listen(4000)

examples/file-upload/yoga.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createYoga, createSchema } from 'graphql-yoga'
2+
import fs from 'fs'
3+
import path from 'path'
4+
5+
export const yoga = createYoga({
6+
schema: createSchema({
7+
typeDefs: /* GraphQL */ `
8+
scalar File
9+
type Query {
10+
hello(name: String): String!
11+
}
12+
type Mutation {
13+
readTextFile(file: File!): String!
14+
saveFile(file: File!): Boolean!
15+
}
16+
`,
17+
resolvers: {
18+
Query: {
19+
hello: (_, { name }: { name: string }) => `Hello ${name || 'World'}`,
20+
},
21+
Mutation: {
22+
readTextFile: async (_, { file }: { file: File }) => {
23+
const fileContent = await file.text()
24+
return fileContent
25+
},
26+
saveFile: async (_, { file }: { file: File }) => {
27+
try {
28+
const fileStream = file.stream()
29+
await fs.promises.writeFile(
30+
path.join(__dirname, file.name),
31+
fileStream,
32+
)
33+
} catch (e) {
34+
return false
35+
}
36+
return true
37+
},
38+
},
39+
},
40+
}),
41+
graphiql: {
42+
title: 'Hello World',
43+
},
44+
})

0 commit comments

Comments
 (0)