reactjs - Prisma:查询子关系的最高效方式
问题描述
我想查询帖子和每个帖子,我不只是想从当前用户那里获得。
我有 2 个解决方案,并且都在工作。但是哪一个更好呢?还是有更好的解决方案?
- 前端:React + ApolloClient
- 后端:Primsa2 + Graphql Nexus
棱镜架构:
model Post {
id String @id @default(cuid())
likes Like[]
}
model Like {
id String @id @default(cuid())
post Post? @relation(fields: [postId], references: [id])
postId String?
user User @relation(fields: [userId], references: [id])
userId String
}
model User {
id String @id @default(cuid())
likes Like[]
}
两种解决方案的 queryField 相同:
export const posts = queryField('posts', {
type: 'Post',
list: true,
resolve: async (_parent, args: any, { prisma, request }, info) => {
opArgs={
//unimportant
}
return prisma.post.findMany(opArgs)
},
})
- 解决方案:我在使用 apollo 客户端的查询中直接使用 where 条件发出请求
前端:ApolloClient
const POSTS_USER = gql`
query posts(
$currentUserId: String
) {
posts {
id
likes(where: { user: { id: { equals: $currentUserId } } }) { // where condition!
id
}
}
}
`;
const POSTS_NO_USER = gql`
query posts(
$currentUserId: String
) {
posts {
id
}
}
`;
const { data, loading } = useQuery(user? POSTS_USER: POSTS_NO_USER, {
variables: {
currentUserId: user ? user.id : "",
},
});
后端 Nexus 对象:
const Post = objectType({
name: 'Post',
definition(t) {
t.model.id()
t.model.likes({
filtering: {
user: true,
},
})
},
})
- 解决方案:我为 objectType Post 设置了 like 的字段条件。
前端:ApolloClient
const POSTS = gql`
query posts {
posts {
id
likes { // NO where condition!
id
}
}
}
`;
const { data, loading } = useQuery(POSTS);
带有 Nexus 的后端:
const Post = objectType({
name: 'Post',
definition(t) {
t.model.id()
t.model.likes()
t.list.field('likes', {
type: 'Like',
list: true,
resolve: (parent, args, { prisma, request }) => {
const user = getUser(request, false)
if (!user) {
return []
} else {
return prisma.like.findMany({
where: {
postId: parent.id,
userId: user.id,
},
})
}
},
})
},
})
解决方案
第一种方法更好,为什么?
当您进行第一次查询时,prisma 将在 1 个 DB 查询中进行查询。当您进行第二次查询时,您将有数据库查询,因为您有帖子编号
使用我的PrismaSelect插件 Prisma Select的最佳方式采用info: GraphQLResolveInfo
通用 graphql 参数(父、参数、上下文、信息)中的对象来选择prisma client
. 该方法可以提供更好的性能,因为您将只使用一个解析器来检索您的所有请求。通过这样做,它也消除了这个N + 1
问题。
但是您还需要放弃使用 nexus-plugin-prisma 并使用我的pal.js CLI自动生成您的 CRUD
推荐阅读
- spring - how to stop default query on id when using @cacheable annotation
- reactjs - React redux props undefined
- javascript - Javascript prevent decrement past 0
- python - 您如何处理在 Python 中生成许多变量或对象的任务?
- c++ - 什么是“#include
“? - python-3.x - 在特定小时之间每 10 秒运行一次 bash 脚本
- c# - Handle Linq queries outputs
- python - 在python中使用for循环时如何更新列表
- javascript - 动态创建的字段/ID 中的 foreach 解决方案
- javascript - 如何让nodejs在继续之前等待mongodb连接(以阻塞方式调用异步)