I'm trying to upload a file from my frontend app to my server but I keep getting this weird error and have no idea why it's happening.
Error is frontend:
Variable "$file" got invalid value {}; Expected type Upload. Upload value invalid.
Error in backend:
GraphQLError: Upload value invalid.
at GraphQLScalarType.parseValue (/app/backend/node_modules/graphql-upload/lib/GraphQLUpload.js:66:11)
this is how I added the Upload scalar
// 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";
},
});
this is how I made the uplaod client in my React/NextJS app
// 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],
},
});
}
}}
/>
even when trying to run the mutation using something other than my frontend app I get the same error, so I guess the problem is with my backend setup, even though I searched a lot for ways to do it none seems to work.