Skip to content

Commit b1e5397

Browse files
authored
test: file-upload-nexus example (#1751)
1 parent 0da0b59 commit b1e5397

File tree

3 files changed

+113
-38
lines changed

3 files changed

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

examples/file-upload-nexus/index.ts

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,5 @@
1-
import {
2-
makeSchema,
3-
scalarType,
4-
mutationField,
5-
queryField,
6-
arg,
7-
nonNull,
8-
} from 'nexus'
9-
import { createYoga } from 'graphql-yoga'
101
import { createServer } from 'http'
11-
12-
const FileScalar = scalarType({
13-
name: 'File',
14-
asNexusMethod: 'file',
15-
description: 'The `File` scalar type represents a file upload.',
16-
sourceType: 'File',
17-
})
18-
19-
const greetings = queryField('greetings', {
20-
type: 'String',
21-
resolve: () => 'Hello World!',
22-
})
23-
24-
const readTextFile = mutationField('readTextFile', {
25-
type: 'String',
26-
args: { file: nonNull(arg({ type: 'File' })) },
27-
resolve: async (parent, { file }) => {
28-
const textContent = await file.text()
29-
return textContent
30-
},
31-
})
32-
33-
const schema = makeSchema({
34-
types: [FileScalar, greetings, readTextFile],
35-
})
36-
37-
const yoga = createYoga({
38-
schema,
39-
})
2+
import { yoga } from './yoga'
403

414
const server = createServer(yoga)
425

examples/file-upload-nexus/yoga.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
makeSchema,
3+
scalarType,
4+
mutationField,
5+
queryField,
6+
arg,
7+
nonNull,
8+
} from 'nexus'
9+
import { createYoga } from 'graphql-yoga'
10+
11+
const FileScalar = scalarType({
12+
name: 'File',
13+
asNexusMethod: 'file',
14+
description: 'The `File` scalar type represents a file upload.',
15+
sourceType: 'File',
16+
})
17+
18+
const greetings = queryField('greetings', {
19+
type: 'String',
20+
resolve: () => 'Hello World!',
21+
})
22+
23+
const readTextFile = mutationField('readTextFile', {
24+
type: 'String',
25+
args: { file: nonNull(arg({ type: 'File' })) },
26+
resolve: async (parent, { file }) => {
27+
const textContent = await file.text()
28+
return textContent
29+
},
30+
})
31+
32+
const schema = makeSchema({
33+
types: [FileScalar, greetings, readTextFile],
34+
})
35+
36+
export const yoga = createYoga({
37+
schema,
38+
})

0 commit comments

Comments
 (0)