Skip to content
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
89 changes: 72 additions & 17 deletions packages/wabe/src/file/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,52 @@ describe('File upload', () => {
const mockBeforeUpload = mock()

beforeAll(async () => {
const setup = await setupTests([
{
name: 'Test3',
fields: {
file: { type: 'File' },
},
permissions: {
read: {
requireAuthentication: false,
},
create: {
requireAuthentication: false,
const setup = await setupTests(
[
{
name: 'Test3',
fields: {
file: { type: 'File' },
},
update: {
requireAuthentication: false,
permissions: {
read: {
requireAuthentication: false,
},
create: {
requireAuthentication: false,
},
update: {
requireAuthentication: false,
},
delete: {
requireAuthentication: false,
},
},
delete: {
requireAuthentication: false,
},
],
{
resolvers: {
mutations: {
uploadFileCustom: {
required: true,
type: 'String',
args: {
input: {
attachment: { type: 'File', required: true },
},
},
resolve: async (
_parent: unknown,
args: { input: { attachment: { file: File } } },
) => {
const file = args.input.attachment.file
return `${file.name}|${await file.text()}`
},
},
},
},
},
])
)
wabe = setup.wabe
port = setup.port

Expand Down Expand Up @@ -301,6 +325,37 @@ describe('File upload', () => {
expect(await fileArg2?.text()).toEqual('b')
})

it('should accept multipart file upload on a custom mutation File input field', async () => {
const formData = new FormData()

formData.append(
'operations',
JSON.stringify({
query:
'mutation ($file: File!) { uploadFileCustom(input: { attachment: { file: $file } }) }',
variables: { file: null },
}),
)

formData.append('map', JSON.stringify({ 0: ['variables.file'] }))
formData.append(
'0',
new File(['custom-mutation-payload'], 'custom-doc.txt', { type: 'text/plain' }),
)

const res = await fetch(`http://127.0.0.1:${port}/graphql`, {
method: 'POST',
body: formData,
})

const jsonRes = await res.json()

expect(jsonRes.errors).toBeUndefined()
expect(jsonRes.data.uploadFileCustom).toEqual('custom-doc.txt|custom-mutation-payload')

expect(spyFileDevAdapterUploadFile).not.toHaveBeenCalled()
})

it('should upload a file on request on type File on create request', async () => {
const formData = new FormData()

Expand Down
3 changes: 2 additions & 1 deletion packages/wabe/src/graphql/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,8 @@ export const GraphqlParser: GraphqlParserConstructor =

if (
graphqlObjectType === 'CreateFieldsInput' ||
graphqlObjectType === 'UpdateFieldsInput'
graphqlObjectType === 'UpdateFieldsInput' ||
graphqlObjectType === 'InputObject'
) {
acc[key] = {
type: currentField.required
Expand Down
3 changes: 3 additions & 0 deletions packages/wabe/src/utils/testHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { v4 as uuid } from 'uuid'
import type { RateLimitOptions } from 'wobe'
import { type ClassInterface, EmailDevAdapter, FileDevAdapter } from '..'
import type { TypeResolver } from '../schema/Schema'
import { Wabe } from '../server'
import type { DevWabeTypes } from './helper'
import getPort from 'get-port'
Expand All @@ -26,6 +27,7 @@ export const setupTests = async (
rateLimit?: RateLimitOptions
maxWhereRecursionDepth?: number
security?: { maxWhereRecursionDepth?: number }
resolvers?: TypeResolver<DevWabeTypes>
} = {},
) => {
const maxWhereRecursionDepth =
Expand Down Expand Up @@ -122,6 +124,7 @@ export const setupTests = async (
description: 'Test scalar',
},
],
...(options.resolvers && { resolvers: options.resolvers }),
},
})

Expand Down
Loading