我想要获得所有的数据,如参与者,建议,用户。
产品包括建议。提议包括参与者。
如果我使用查询,我想要获取所有数据,测试查询只显示userId,但是我想要所有关于用户测试查询信息只显示proposeId,但是我想要所有关于提议的信息
prisma模型
model Product {
id Int @id @default(autoincrement())
prodName String
prodCode String
posts Post[]
holdings Holding[]
proposes Propose[]
}
model Propose {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int
product Product @relation(fields: [productId], references: [id])
productId Int
title String
content String
totalAmt Int
participants Participant[]
createdAt DateTime @default(now())
}
model Participant {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int
propose Propose @relation(fields: [proposeId], references: [id])
proposeId Int
amt Int
participatedAt DateTime @default(now())
}类型
type Participant {
id: Int!
user: User
propose: Propose
amt: Int
participatedAt: String
}
type seeParticipantResult {
ok: Boolean!
participants: [Participant]
error: String
}
type Query {
seeParticipant: seeParticipantResult
}查询
export default {
Query: {
seeParticipant: async (_, __, { loggedInUser }) => {
try {
const participants = await client.participant.findMany({
where: {
userId: loggedInUser.id,
},
});
return {
ok: true,
participants,
};
} catch (e) {
return {
ok: false,
error: e,
};
}
},
},
};测试查询
query Query {
seeParticipant {
ok
participants {
id
user {
username
}
propose {
product {
prodName
prodCode
}
title
}
}
}
}结果
"data": {
"seeParticipant": {
"ok": true,
"participants": [
{
"id": 1,
"user": null,
"propose": null
}
]
}
}
}它没有显示propose和user。
发布于 2021-10-26 07:37:32
Prisma不会在查询时自动返回relation对象。在使用GraphQL时,您应该为任何关系字段(如GraphQL模式中的Participant.user )创建单独的解析器函数。
这是Participant.user的解析器可能的样子
Participant: {
user: (parent, args, context) => {
return client.participant.findUnique({where: {id: parent.id}}).user()
}
}我使用Prisma fluent API从user的findUnique查询中获取连接的participant。
请注意,您需要对Propose执行类似的操作。
https://stackoverflow.com/questions/69696017
复制相似问题