使用nexus prisma graphql处理文件上载
我正在尝试将文件从我的前端应用程序上载到我的服务器 但我一直在犯这个奇怪的错误,不知道为什么会发生这种情况 错误是前端:使用nexus prisma graphql处理文件上载,graphql,apollo,prisma,prisma-graphql,nexus-prisma,Graphql,Apollo,Prisma,Prisma Graphql,Nexus Prisma,我正在尝试将文件从我的前端应用程序上载到我的服务器 但我一直在犯这个奇怪的错误,不知道为什么会发生这种情况 错误是前端: Variable "$file" got invalid value {}; Expected type Upload. Upload value invalid. 后端错误: GraphQLError: Upload value invalid. at GraphQLScalarType.parseValue (/app/backend/node_modules/grap
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)
这就是我添加上传标量的方式
//标量/upload.ts
从“nexus/dist”导入{scalarType};
从“graphql upload”导入{GraphQLUpload};
export const Upload=scalarType({
名称:“上传”,
asNexusMethod:“upload”//如果需要,我们将其设置为以后用作't.upload()`的方法
description:graphqluload.description,
serialize:GraphQLUpload.serialize,
parseValue:GraphQLUpload.parseValue,
parseLiteral:GraphQLUpload.parseLiteral,
});
导出默认上传;
//标量/index.ts
从“/Upload”导出{default as Upload};
//索引
将*作为所有类型从“/resolvers”导入;
将*作为标量从“/scalars”导入;
const schema=makePrismaSchema({
类型:[标量,所有类型],
普里斯马:{
datamodelInfo,
客户:prisma,
},
...
//我的变异
t、 字符串(“上传文件”{
args:{
文件:arg({type:“Upload”,null:false}),
},
resolve:async({u:any,{file},{}:any)=>{
console.log(文件);
返回“ok”;
},
});
这就是我在react/nextjs应用程序中创建uplaod客户端的方法
//链接
常量链接=()=>{
返回createUploadLink({
uri:process.env.backend,
证书:“包括”,
取来
});
};
//突变(我使用graphql codegen)
突变测试上传($file:Upload!){
上载文件(文件:$file)
}
const[upload]=useTestUploadMutation();
{
const{files,validity}=e.target;
if(validity.valid&&files&&files[0]){
上传({
变量:{
文件:文件[0],
},
});
}
}}
/>
即使尝试使用我的前端应用程序以外的其他程序运行变异,我也会遇到同样的错误,因此我猜问题出在我的后端设置上,尽管我搜索了很多方法,但似乎都不起作用。我只是遇到了完全相同的问题。这解决了问题。因此基本上是手动定义上载,而不是使用GraphQLUpload
import { objectType, scalarType } from "@nexus/schema";
import { GraphQLError } from "graphql";
import * as FileType from "file-type";
export const Upload = scalarType({
name: "Upload",
asNexusMethod: "upload", // We set this to be used as a method later as `t.upload()` if needed
description: "desc",
serialize: () => {
throw new GraphQLError("Upload serialization unsupported.");
},
parseValue: async (value) => {
const upload = await value;
const stream = upload.createReadStream();
const fileType = await FileType.fromStream(stream);
if (fileType?.mime !== upload.mimetype)
throw new GraphQLError("Mime type does not match file content.");
return upload;
},
parseLiteral: (ast) => {
throw new GraphQLError("Upload literal unsupported.", ast);
},
});
export const File = objectType({
name: "File",
definition(t) {
t.id("id");
t.string("path");
t.string("filename");
t.string("mimetype");
t.string("encoding");
},
});
我只是遇到了完全相同的问题。这解决了它。所以基本上是手动定义上传,而不是使用GraphQLUpload
import { objectType, scalarType } from "@nexus/schema";
import { GraphQLError } from "graphql";
import * as FileType from "file-type";
export const Upload = scalarType({
name: "Upload",
asNexusMethod: "upload", // We set this to be used as a method later as `t.upload()` if needed
description: "desc",
serialize: () => {
throw new GraphQLError("Upload serialization unsupported.");
},
parseValue: async (value) => {
const upload = await value;
const stream = upload.createReadStream();
const fileType = await FileType.fromStream(stream);
if (fileType?.mime !== upload.mimetype)
throw new GraphQLError("Mime type does not match file content.");
return upload;
},
parseLiteral: (ast) => {
throw new GraphQLError("Upload literal unsupported.", ast);
},
});
export const File = objectType({
name: "File",
definition(t) {
t.id("id");
t.string("path");
t.string("filename");
t.string("mimetype");
t.string("encoding");
},
});