Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: file-upload-nexus example #1751

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
})
39 changes: 1 addition & 38 deletions examples/file-upload-nexus/index.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,5 @@
import {
makeSchema,
scalarType,
mutationField,
queryField,
arg,
nonNull,
} from 'nexus'
import { createYoga } from 'graphql-yoga'
import { createServer } from 'http'

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],
})

const yoga = createYoga({
schema,
})
import { yoga } from './yoga'

const server = createServer(yoga)

Expand Down
38 changes: 38 additions & 0 deletions examples/file-upload-nexus/yoga.ts
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,
})