-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: file-upload-nexus example (#1751)
- Loading branch information
Showing
3 changed files
with
113 additions
and
38 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
examples/file-upload-nexus/__integration-tests__/file-upload-nexus.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { yoga } from '../yoga' | ||
import { createServer, Server } from 'http' | ||
import { AddressInfo } from 'net' | ||
import { fetch, File, FormData } from '@whatwg-node/fetch' | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
describe('file-upload-nexus example integration', () => { | ||
let server: Server | ||
let port: number | ||
|
||
beforeAll(async () => { | ||
server = createServer(yoga) | ||
await new Promise<void>((resolve) => server.listen(0, resolve)) | ||
port = (server.address() as AddressInfo).port | ||
}) | ||
|
||
afterAll(async () => { | ||
await new Promise((resolve) => server.close(resolve)) | ||
}) | ||
|
||
it('should execute query', async () => { | ||
const response = await fetch( | ||
`http://localhost:${port}/graphql?query=query{greetings}`, | ||
) | ||
const body = await response.json() | ||
expect(body.errors).toBeUndefined() | ||
expect(body.data).toEqual({ | ||
greetings: 'Hello World!', | ||
}) | ||
}) | ||
|
||
it('should read file text', async () => { | ||
const sourceFilePath = path.join( | ||
__dirname, | ||
'..', | ||
'..', | ||
'..', | ||
'website', | ||
'public', | ||
'logo.png', | ||
) | ||
|
||
const formData = new FormData() | ||
formData.set( | ||
'operations', | ||
JSON.stringify({ | ||
query: /* GraphQL */ ` | ||
mutation readTextFile($file: File!) { | ||
readTextFile(file: $file) | ||
} | ||
`, | ||
}), | ||
) | ||
formData.set('map', JSON.stringify({ 0: ['variables.file'] })) | ||
formData.set( | ||
'0', | ||
new File( | ||
[await fs.promises.readFile(sourceFilePath)], | ||
path.basename(sourceFilePath), | ||
{ type: 'image/png' }, | ||
), | ||
) | ||
|
||
const response = await fetch(`http://localhost:${port}/graphql`, { | ||
method: 'POST', | ||
body: formData, | ||
}) | ||
|
||
const body = await response.json() | ||
expect(body.errors).toBeUndefined() | ||
expect(body.data).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { | ||
makeSchema, | ||
scalarType, | ||
mutationField, | ||
queryField, | ||
arg, | ||
nonNull, | ||
} from 'nexus' | ||
import { createYoga } from 'graphql-yoga' | ||
|
||
const FileScalar = scalarType({ | ||
name: 'File', | ||
asNexusMethod: 'file', | ||
description: 'The `File` scalar type represents a file upload.', | ||
sourceType: 'File', | ||
}) | ||
|
||
const greetings = queryField('greetings', { | ||
type: 'String', | ||
resolve: () => 'Hello World!', | ||
}) | ||
|
||
const readTextFile = mutationField('readTextFile', { | ||
type: 'String', | ||
args: { file: nonNull(arg({ type: 'File' })) }, | ||
resolve: async (parent, { file }) => { | ||
const textContent = await file.text() | ||
return textContent | ||
}, | ||
}) | ||
|
||
const schema = makeSchema({ | ||
types: [FileScalar, greetings, readTextFile], | ||
}) | ||
|
||
export const yoga = createYoga({ | ||
schema, | ||
}) |