我想查询帖子和每个帖子,我不只是想从当前用户那里获得。
我有 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,
},
})
}
},
})
},
})