- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将文件从我的前端应用程序上传到我的服务器但我一直收到这个奇怪的错误,并且不知道为什么会这样。
错误是前端:
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],
},
});
}
}}
/>
即使尝试使用我的前端应用程序以外的其他东西来运行突变,我也会遇到同样的错误,所以我猜问题出在我的后端设置上,尽管我搜索了很多方法似乎都没有用。
最佳答案
我刚刚遇到了完全相同的问题。这修复了它。所以基本上手动定义上传而不是使用 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");
},
});
关于graphql - 使用 nexus-prisma graphql 处理文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61910558/
我想更改 prisma 文件夹的名称和位置。目前它叫做 prisma,它位于我的 nextjs 项目的根文件夹中。我想将它命名为 db 并将其放在 src 文件夹中。那可能吗?如果是,怎么办? 最佳答
我正在评估 Prisma,我是一个完整的菜鸟...... 我正在使用 Postgresql 我有以下模型定义 model Sth { id Int @d
我正在使用 Prisma 我有一个要搜索的 postgres 表,只查找匹配 where (id 和 accessKey) 或 (id 和 ownerId) 的记录 如果 accessKey 或 ow
上面一个是我在Vercel上部署MongoDB(Atlas URL)后端和Prisma作为ORM时遇到的问题。。我观察到部署时缺少@prisma/client实例。我已经创建了单独的文件Prisma对
上面一个是我在Vercel上部署MongoDB(Atlas URL)后端和Prisma作为ORM时遇到的问题。。我观察到部署时缺少@prisma/client实例。我已经创建了单独的文件Prisma对
我是 Prisma 的新手,虽然到目前为止它非常容易上手,但我遇到了一个我似乎无法找到好的答案的问题。我已经阅读了 docs about relation queries ,但据我了解,Prisma
我找不到任何示例来说明如何使用整数执行此操作,并且我的选项不起作用( 我的表包含 2 列整数,我需要使用 prisma 原始查询向其中添加多行。 如果我只添加 1 行并使用 Prisma.join()
以 Prisma 文档中的架构为例,我想查询 Users 以查找没有任何 posts 的任何用户。我可以像这样破解它,以便它检索所有帖子的 ID 都不大于 0 的每个用户,但这不是很优雅。有更好的方法
这是我的 datamodel.prisma 文件的相关部分。 type Driver { id: ID! @unique zones: [Zone!] @relation(name: "Dri
我正在使用 Prisma 1.9 和 Postgres。 如何重置所有内容?我已经尝试过 prisma local nuke ,但此命令会添加 MySQL 容器(以某种方式),然后抛出端口 4466
以 Prisma 文档中的架构为例,我想查询 Users 以查找没有任何 posts 的任何用户。我可以像这样破解它,以便它检索所有帖子的 ID 都不大于 0 的每个用户,但这不是很优雅。有更好的方法
这是我的 datamodel.prisma 文件的相关部分。 type Driver { id: ID! @unique zones: [Zone!] @relation(name: "Dri
我有以下查询,它给出了所有帖子和所有评论的计数。现在,我想计算已批准字段设置为 true 的帖子的所有评论数。我似乎无法弄清楚这一点。 prisma.post.findMany({ inclu
我发现您现在使用的是 MySQL 和 PostgreSQL,它们支持地理空间类型,我该如何对我的 prisma 执行地理空间查询。 假设我想获取纽约市附近的所有事件? 最佳答案 我使用 Prisma
运行prisma init 不会生成文件。它不会生成下面的 3 个文件。 datamodel.graphql docker-compose.yml 棱镜.yml 最终收到此错误:- { "e
我开始使用这个惊人的工具包,并注意到当我生成迁移文件时,Prisma 还会在新模式旁边创建一个 README 文件。该文件的副标题中恰好有我的个人姓名:This migration has been
我是 Prisma 和 Nodejs 的新手 我不小心创建了许多 Prisma Client 实例,这些实例一直显示警告 warn(prisma-client) 已经有 10 个 Prisma Cli
在 this example ,我们看到: 名称字符串? 不 💯 ? 在这里表示什么...... 最佳答案 这意味着在数据库中。该字段可以为空。这是可选的。 关于prisma-graphql - 定
我正在尝试计算 Prisma2 中的 self 关系(关注者)(使用 PostgreSQL) 型号: model User { id String @id @default(cui
嗨 - 我一直在关注 GraphQL/Prisma 教程 ( https://www.howtographql.com/graphql-js/6-authentication/ ),我想知道为什么要在
我是一名优秀的程序员,十分优秀!