我正在尝试将文件从我的前端应用程序上传到我的服务器,但我不断收到这个奇怪的错误,不知道为什么会这样。
错误是前端:
Variable "$file" got invalid value {}; Expected type Upload. Upload value invalid.
后端错误:
GraphQLError: Upload value invalid.
at GraphQLScalarType.parseValue (/app/backend/node_modules/graphql-upload/lib/GraphQLUpload.js:66:11)
这就是我添加上传标量的方式
// scalars/upload.ts
import { scalarType } from "nexus/dist";
import { GraphQLUpload } from "graphql-upload";
export const Upload = scalarType({
name: "Upload",
asNexusMethod: "upload", // We set this to be used as a method later as `t.upload()` if needed
description: GraphQLUpload.description,
serialize: GraphQLUpload.serialize,
parseValue: GraphQLUpload.parseValue,
parseLiteral: GraphQLUpload.parseLiteral,
});
export default Upload;
// scalar/index.ts
export { default as Upload } from "./Upload";
// index.ts
import * as allTypes from "./resolvers";
import * as scalars from "./scalars";
const schema = makePrismaSchema({
types: [scalars, allTypes],
prisma: {
datamodelInfo,
client: prisma,
},
...
// my mutation
t.string("uploadFile", {
args: {
file: arg({ type: "Upload", nullable: false }),
},
resolve: async (_: any, { file }, { }: any) => {
console.log(file);
return "ok";
},
});
这就是我在我的 React/NextJS 应用程序中制作 uplaod 客户端的方式
// the link
const link = () => {
return createUploadLink({
uri: process.env.backend,
credentials: "include",
fetch,
});
};
// the mutation ( I use graphql codegen )
mutation TestUpload($file: Upload!) {
uploadFile(file: $file)
}
const [upload] = useTestUploadMutation();
<input
type="file"
onChange={(e) => {
const { files, validity } = e.target;
if (validity.valid && files && files[0]) {
upload({
variables: {
file: files[0],
},
});
}
}}
/>
即使尝试使用我的前端应用程序以外的其他东西来运行突变,我也会遇到同样的错误,所以我想问题出在我的后端设置上,即使我搜索了很多方法来做到这一点,但似乎没有一个工作。