Skip to content

Commit 32faabd

Browse files
authored
test: file-upload-nextjs-pothos example (#1752)
1 parent b1e5397 commit 32faabd

File tree

3 files changed

+115
-37
lines changed

3 files changed

+115
-37
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { createServer, Server } from 'http'
2+
import { AddressInfo } from 'net'
3+
import { fetch, File, FormData } from '@whatwg-node/fetch'
4+
import * as fs from 'fs'
5+
import * as path from 'path'
6+
import { createYoga } from 'graphql-yoga'
7+
import { schema } from '../schema'
8+
9+
const yoga = createYoga({ schema })
10+
11+
describe('file-upload-nextjs-pothos example integration', () => {
12+
let server: Server
13+
let port: number
14+
15+
beforeAll(async () => {
16+
server = createServer(yoga)
17+
await new Promise<void>((resolve) => server.listen(0, resolve))
18+
port = (server.address() as AddressInfo).port
19+
})
20+
21+
afterAll(async () => {
22+
await new Promise((resolve) => server.close(resolve))
23+
})
24+
25+
it('should execute query', async () => {
26+
const response = await fetch(
27+
`http://localhost:${port}/graphql?query=query{greetings}`,
28+
)
29+
const body = await response.json()
30+
expect(body.errors).toBeUndefined()
31+
expect(body.data).toEqual({
32+
greetings: 'Hello World',
33+
})
34+
})
35+
36+
it('should read file text', async () => {
37+
const sourceFilePath = path.join(
38+
__dirname,
39+
'..',
40+
'..',
41+
'..',
42+
'website',
43+
'public',
44+
'logo.png',
45+
)
46+
47+
const formData = new FormData()
48+
formData.set(
49+
'operations',
50+
JSON.stringify({
51+
query: /* GraphQL */ `
52+
mutation readTextFile($file: File!) {
53+
readTextFile(file: $file)
54+
}
55+
`,
56+
}),
57+
)
58+
formData.set('map', JSON.stringify({ 0: ['variables.file'] }))
59+
formData.set(
60+
'0',
61+
new File(
62+
[await fs.promises.readFile(sourceFilePath)],
63+
path.basename(sourceFilePath),
64+
{ type: 'image/png' },
65+
),
66+
)
67+
68+
const response = await fetch(`http://localhost:${port}/graphql`, {
69+
method: 'POST',
70+
body: formData,
71+
})
72+
73+
const body = await response.json()
74+
expect(body.errors).toBeUndefined()
75+
expect(body.data).toBeDefined()
76+
})
77+
})

examples/file-upload-nextjs-pothos/pages/api/graphql.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,6 @@
11
import { createYoga } from 'graphql-yoga'
2-
import SchemaBuilder from '@pothos/core'
32
import type { NextApiRequest, NextApiResponse } from 'next'
4-
5-
const builder = new SchemaBuilder<{
6-
Scalars: { File: { Input: File; Output: never } }
7-
}>({})
8-
9-
builder.scalarType('File', {
10-
serialize: () => {
11-
throw new Error('Uploads can only be used as input types')
12-
},
13-
})
14-
15-
builder.queryType({
16-
fields: (t) => ({
17-
greetings: t.string({ resolve: () => 'Hello World' }),
18-
}),
19-
})
20-
21-
builder.mutationType({
22-
fields: (t) => ({
23-
readTextFile: t.string({
24-
args: {
25-
file: t.arg({
26-
type: 'File',
27-
required: true,
28-
}),
29-
},
30-
resolve: async (_, { file }) => {
31-
const textContent = await file.text()
32-
33-
return textContent
34-
},
35-
}),
36-
}),
37-
})
38-
39-
const schema = builder.toSchema({})
3+
import { schema } from '../../schema'
404

415
export default createYoga<{
426
req: NextApiRequest
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import SchemaBuilder from '@pothos/core'
2+
3+
const builder = new SchemaBuilder<{
4+
Scalars: { File: { Input: File; Output: never } }
5+
}>({})
6+
7+
builder.scalarType('File', {
8+
serialize: () => {
9+
throw new Error('Uploads can only be used as input types')
10+
},
11+
})
12+
13+
builder.queryType({
14+
fields: (t) => ({
15+
greetings: t.string({ resolve: () => 'Hello World' }),
16+
}),
17+
})
18+
19+
builder.mutationType({
20+
fields: (t) => ({
21+
readTextFile: t.string({
22+
args: {
23+
file: t.arg({
24+
type: 'File',
25+
required: true,
26+
}),
27+
},
28+
resolve: async (_, { file }) => {
29+
const textContent = await file.text()
30+
31+
return textContent
32+
},
33+
}),
34+
}),
35+
})
36+
37+
export const schema = builder.toSchema({})

0 commit comments

Comments
 (0)