-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: multipart upload support Co-authored-by: https://github.com/AlecAivazis and https://github.com/524c * comment in a header filter * add single and multi upload support to api * add store tests for single and multi upload files * remove comment about lib 'formdata-node' * fix test titles * add changeset * add upload files guide * unnecessary await Co-authored-by: Gogs <[email protected]> Co-authored-by: Alec Aivazis <[email protected]>
- Loading branch information
1 parent
3b44a6e
commit 08b3d10
Showing
16 changed files
with
367 additions
and
4 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'houdini': patch | ||
--- | ||
|
||
Add support for multipart file uploads |
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
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 |
---|---|---|
|
@@ -16,6 +16,9 @@ const config = { | |
marshal(val) { | ||
return val.getTime(); | ||
} | ||
}, | ||
File: { | ||
type: 'File' | ||
} | ||
}, | ||
plugins: { | ||
|
3 changes: 3 additions & 0 deletions
3
e2e/sveltekit/src/lib/graphql/operations/MUTATION.MultipleUpload.gql
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,3 @@ | ||
mutation multipleUpload($files: [File!]!) { | ||
multipleUpload(files: $files) | ||
} |
3 changes: 3 additions & 0 deletions
3
e2e/sveltekit/src/lib/graphql/operations/MUTATION.SingleUpload.gql
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,3 @@ | ||
mutation singleUpload($file: File!) { | ||
singleUpload(file: $file) | ||
} |
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
19 changes: 19 additions & 0 deletions
19
e2e/sveltekit/src/routes/stores/mutation-scalar-multi-upload/+page.svelte
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,19 @@ | ||
<script lang="ts"> | ||
import { GQL_multipleUpload } from '$houdini'; | ||
async function upload() { | ||
const file = new File(['Houdini'], 'foo.txt', { | ||
type: 'text/plain' | ||
}); | ||
GQL_multipleUpload.mutate({ files: [file, file] }); | ||
} | ||
</script> | ||
|
||
<h1>Mutation multi upload</h1> | ||
|
||
<button id="mutate" on:click={upload}>Upload files</button> | ||
|
||
<div id="result"> | ||
{JSON.stringify($GQL_multipleUpload.data)} | ||
</div> |
15 changes: 15 additions & 0 deletions
15
e2e/sveltekit/src/routes/stores/mutation-scalar-multi-upload/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,15 @@ | ||
import { routes } from '../../../lib/utils/routes.js'; | ||
import { expect_1_gql, goto, expectToBe } from '../../../lib/utils/testsHelper.js'; | ||
import { test } from '@playwright/test'; | ||
|
||
test.describe('mutation store upload', function () { | ||
test('multiple files', async function ({ page }) { | ||
await goto(page, routes.Stores_Mutation_Scalar_Multi_Upload); | ||
|
||
// trigger the mutation and wait for a response | ||
await expect_1_gql(page, 'button[id=mutate]'); | ||
|
||
// make sure that the return data is equal file content (["Houdini","Houdini"]) | ||
await expectToBe(page, '{"multipleUpload":["Houdini","Houdini"]}'); | ||
}); | ||
}); |
19 changes: 19 additions & 0 deletions
19
e2e/sveltekit/src/routes/stores/mutation-scalar-single-upload/+page.svelte
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,19 @@ | ||
<script lang="ts"> | ||
import { GQL_singleUpload } from '$houdini'; | ||
async function upload() { | ||
const file = new File(['Houdini'], 'foo.txt', { | ||
type: 'text/plain' | ||
}); | ||
GQL_singleUpload.mutate({ file: file }); | ||
} | ||
</script> | ||
|
||
<h1>Mutation single upload</h1> | ||
|
||
<button id="mutate" on:click={upload}>Upload file</button> | ||
|
||
<div id="result"> | ||
{JSON.stringify($GQL_singleUpload.data)} | ||
</div> |
15 changes: 15 additions & 0 deletions
15
e2e/sveltekit/src/routes/stores/mutation-scalar-single-upload/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,15 @@ | ||
import { routes } from '../../../lib/utils/routes.js'; | ||
import { expect_1_gql, goto, expectToBe } from '../../../lib/utils/testsHelper.js'; | ||
import { test } from '@playwright/test'; | ||
|
||
test.describe('mutation store upload', function () { | ||
test('single files', async function ({ page }) { | ||
await goto(page, routes.Stores_Mutation_Scalar_Single_Upload); | ||
|
||
// trigger the mutation and wait for a response | ||
await expect_1_gql(page, 'button[id=mutate]'); | ||
|
||
// make sure that the return data is equal file content ("Houdini") | ||
await expectToBe(page, '{"singleUpload":"Houdini"}'); | ||
}); | ||
}); |
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,151 @@ | ||
/// This file contains a modified version, made by AlecAivazis, of the functions found here: https://github.com/jaydenseric/extract-files/blob/master/extractFiles.mjs | ||
/// The associated license is at the end of the file (per the project's license agreement) | ||
|
||
export function isExtractableFile(value: any): value is ExtractableFile { | ||
return ( | ||
(typeof File !== 'undefined' && value instanceof File) || | ||
(typeof Blob !== 'undefined' && value instanceof Blob) | ||
) | ||
} | ||
|
||
type ExtractableFile = File | Blob | ||
|
||
/** @typedef {import("./isExtractableFile.mjs").default} isExtractableFile */ | ||
|
||
export function extractFiles(value: any) { | ||
if (!arguments.length) throw new TypeError('Argument 1 `value` is required.') | ||
|
||
/** | ||
* Deeply clonable value. | ||
* @typedef {Array<unknown> | FileList | Record<PropertyKey, unknown>} Cloneable | ||
*/ | ||
|
||
/** | ||
* Clone of a {@link Cloneable deeply cloneable value}. | ||
* @typedef {Exclude<Cloneable, FileList>} Clone | ||
*/ | ||
|
||
/** | ||
* Map of values recursed within the input value and their clones, for reusing | ||
* clones of values that are referenced multiple times within the input value. | ||
* @type {Map<Cloneable, Clone>} | ||
*/ | ||
const clones = new Map() | ||
|
||
/** | ||
* Extracted files and their object paths within the input value. | ||
* @type {Extraction<Extractable>["files"]} | ||
*/ | ||
const files = new Map() | ||
|
||
/** | ||
* Recursively clones the value, extracting files. | ||
*/ | ||
function recurse(value: any, path: string | string[], recursed: Set<any>) { | ||
if (isExtractableFile(value)) { | ||
const filePaths = files.get(value) | ||
|
||
filePaths ? filePaths.push(path) : files.set(value, [path]) | ||
|
||
return null | ||
} | ||
|
||
const valueIsList = | ||
Array.isArray(value) || (typeof FileList !== 'undefined' && value instanceof FileList) | ||
const valueIsPlainObject = isPlainObject(value) | ||
|
||
if (valueIsList || valueIsPlainObject) { | ||
let clone = clones.get(value) | ||
|
||
const uncloned = !clone | ||
|
||
if (uncloned) { | ||
clone = valueIsList | ||
? [] | ||
: // Replicate if the plain object is an `Object` instance. | ||
value instanceof /** @type {any} */ Object | ||
? {} | ||
: Object.create(null) | ||
|
||
clones.set(value, /** @type {Clone} */ clone) | ||
} | ||
|
||
if (!recursed.has(value)) { | ||
const pathPrefix = path ? `${path}.` : '' | ||
const recursedDeeper = new Set(recursed).add(value) | ||
|
||
if (valueIsList) { | ||
let index = 0 | ||
|
||
// @ts-ignore | ||
for (const item of value) { | ||
const itemClone = recurse(item, pathPrefix + index++, recursedDeeper) | ||
|
||
if (uncloned) /** @type {Array<unknown>} */ clone.push(itemClone) | ||
} | ||
} else | ||
for (const key in value) { | ||
const propertyClone = recurse(value[key], pathPrefix + key, recursedDeeper) | ||
|
||
if (uncloned) | ||
/** @type {Record<PropertyKey, unknown>} */ clone[key] = propertyClone | ||
} | ||
} | ||
|
||
return clone | ||
} | ||
|
||
return value | ||
} | ||
|
||
return { | ||
clone: recurse(value, '', new Set()), | ||
files, | ||
} | ||
} | ||
|
||
/** | ||
* An extraction result. | ||
* @template [Extractable=unknown] Extractable file type. | ||
* @typedef {object} Extraction | ||
* @prop {unknown} clone Clone of the original value with extracted files | ||
* recursively replaced with `null`. | ||
* @prop {Map<Extractable, Array<ObjectPath>>} files Extracted files and their | ||
* object paths within the original value. | ||
*/ | ||
|
||
/** | ||
* String notation for the path to a node in an object tree. | ||
* @typedef {string} ObjectPath | ||
* @see [`object-path` on npm](https://npm.im/object-path). | ||
* @example | ||
* An object path for object property `a`, array index `0`, object property `b`: | ||
* | ||
* ``` | ||
* a.0.b | ||
* ``` | ||
*/ | ||
|
||
function isPlainObject(value: any) { | ||
if (typeof value !== 'object' || value === null) { | ||
return false | ||
} | ||
|
||
const prototype = Object.getPrototypeOf(value) | ||
return ( | ||
(prototype === null || | ||
prototype === Object.prototype || | ||
Object.getPrototypeOf(prototype) === null) && | ||
!(Symbol.toStringTag in value) && | ||
!(Symbol.iterator in value) | ||
) | ||
} | ||
|
||
// MIT License | ||
// Copyright Jayden Seric | ||
|
||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Oops, something went wrong.
08b3d10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs-next – ./site
docs-next-kohl.vercel.app
docs-next-houdinigraphql.vercel.app
docs-next-git-main-houdinigraphql.vercel.app
08b3d10
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs – ./site
docs-houdinigraphql.vercel.app
docs-git-main-houdinigraphql.vercel.app
www.houdinigraphql.com
docs-phi-fawn.vercel.app
houdinigraphql.com