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

Update File Upload example #2645

Merged
merged 1 commit into from
Apr 5, 2023
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
14 changes: 3 additions & 11 deletions examples/file-upload/__integration-tests__/file-upload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,7 @@ describe('graphql-auth example integration', () => {
})

it('should save file', async () => {
const sourceFilePath = path.join(
__dirname,
'..',
'..',
'..',
'website',
'public',
'logo.png',
)
const sourceFilePath = path.join(__dirname, 'fixtures', 'image.jpg')
const sourceMd5 = await md5File(sourceFilePath)

const formData = new FormData()
Expand All @@ -77,7 +69,7 @@ describe('graphql-auth example integration', () => {
new File(
[await fs.promises.readFile(sourceFilePath)],
path.basename(sourceFilePath),
{ type: 'image/png' },
{ type: 'image/jpg' },
),
)

Expand All @@ -92,7 +84,7 @@ describe('graphql-auth example integration', () => {
saveFile: true,
})

const targetFilePath = path.join(__dirname, '..', 'logo.png')
const targetFilePath = path.join(__dirname, '..', 'image.jpg')
await fs.promises.stat(targetFilePath)
const targetMd5 = await md5File(targetFilePath)
expect(targetMd5).toEqual(sourceMd5)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions examples/file-upload/yoga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const yoga = createYoga({
},
saveFile: async (_, { file }: { file: File }) => {
try {
const fileStream = file.stream()
const fileArrayBuffer = await file.arrayBuffer()
await fs.promises.writeFile(
path.join(__dirname, file.name),
fileStream,
Buffer.from(fileArrayBuffer),
)
} catch (e) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ describe('incremental delivery', () => {

const fakeIterator: AsyncIterableIterator<ExecutionResult> = {
[Symbol.asyncIterator]: () => fakeIterator,
// eslint-disable-next-line @typescript-eslint/require-await
async next() {
counter++
return {
Expand Down
9 changes: 6 additions & 3 deletions website/src/pages/v2/features/file-uploads.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ You only need to add a scalar type definition for file uploads in your schema. T
Let's use `File` in this example:

```ts filename="file-upload-example.ts"
import fs from 'node:fs/promises'
import fs from 'node:fs'
import path from 'node:path'
import { createServer } from '@graphql-yoga/node'

Expand Down Expand Up @@ -57,8 +57,11 @@ const server = createServer({
},
async saveFile(_, { file }: { file: File }) {
try {
const fileStream = file.stream()
await fs.writeFile(path.join(__dirname, file.name), fileStream)
const fileArrayBuffer = await file.arrayBuffer()
await fs.promises.writeFile(
path.join(__dirname, file.name),
Buffer.from(fileArrayBuffer)
)
return true
} catch {
return false
Expand Down