Skip to content

Commit

Permalink
test: file-upload-nexus example (#1751)
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Sep 19, 2022
1 parent 0da0b59 commit b1e5397
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 38 deletions.
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,
})

0 comments on commit b1e5397

Please sign in to comment.