jaydenseric / graphql-upload

Middleware and an Upload scalar to add support for GraphQL multipart requests (file uploads via queries and mutations) to various Node.js GraphQL servers.
https://npm.im/graphql-upload
MIT License
1.43k stars 131 forks source link

Cannot import FileUpload from 'graphql-upload' in Nestjs #310

Closed vfa-locltb closed 2 years ago

vfa-locltb commented 2 years ago

I can't import FileUpload from 'graphql-upload' in my Nestjs application written with Typescript, please help

HuakunShen commented 2 years ago

Same problem.

HuakunShen commented 2 years ago

I found version 13.0.0 probably working. Still testing.

jaydenseric commented 2 years ago

As per the major changes documented in the v14.0.0 changelog entry, graphql-upload now has built in types via TypeScript JSDoc comments:

https://github.com/jaydenseric/graphql-upload/releases/tag/v14.0.0

If you wish to make use of them in your Node.js projects, consider using these TypeScript config options:

https://github.com/jaydenseric/graphql-upload/blob/e682fb9f3f64262ff1c762c145ec2713a72b1318/jsconfig.json#L3-L4

You will also need to use a TypeScript version that supports those options.

If you update to graphql-upload v14, you should uninstall @types/graphql-upload. Note that I implemented types from fresh, and they don't necessarily match what types the community had previously come up with in @types/graphql-upload.

In the case of the FileUpload, you need to import it from the module in which it's defined:

https://github.com/jaydenseric/graphql-upload/blob/e682fb9f3f64262ff1c762c145ec2713a72b1318/processRequest.js#L362-L377

You should be able to do that like this in a .ts or .cts or .mts file:

import type { FileUpload } from "graphql-upload/processRequest.js";

When importing within a JSDoc comment (e.g. within a .js, .cjs, or .mjs file):

/** @type {import("graphql-upload/processRequest.js").FileUpload} */

There is an example of this in the Apollo upload examples example API:

https://github.com/jaydenseric/apollo-upload-examples/blob/86e5609e60d86a92fd6060679641105beec715e5/api/storeUpload.mjs#L10-L12

This issue (kind of) duplicates https://github.com/jaydenseric/graphql-upload/issues/306 .