保持架构中嵌套对象顺序的最佳方法是什么。
我的模式:
type Article {
id: ID! @id
pages: [Page!]!
}
type Page {
id: ID! @id
}
这就是我尝试对页面进行排序的方式(未成功):
updateArticle({
variables: {
aricle.id,
data: {
pages: {
connect: reorderPages(aricle.pages)
}
}
}
解析器:
t.field("updateArticle", {
type: "Article",
args: {
id: idArg(),
data: t.prismaType.updateArticle.args.data
},
resolve: (_, { id, data }) => {
return ctx.prisma.updateArticle({
where: { id },
data
});
}
});
我理解为什么这种方法是错误的。我猜想应该通过连接表中的订单索引将订单写入数据库。我不知道如何通过 GraphQL/Nexus/Prisma/MySQL 处理它。
最佳答案
对于 N:M 到关系的模式将如下所示:
type Article {
id: ID! @id
title: String!
items: [ArticleItemEdge!]!
}
type ArticleItemEdge {
id: ID! @id
article: Article! @relation(link: INLINE)
item: Item! @relation(link: INLINE)
order: Int!
}
type Item {
id: ID! @id
title: String!
articles: [ArticleItemEdge!]!
}
然后以更“接力”的方式查询文章,比如带有边和节点的时尚
query {
articles {
items(orderBy: order_ASC) {
item {
title
}
}
}
}
如果不需要 N:M,您可以像这样更新架构定义:
type Article {
id: ID! @id
items: [Item!]!
}
type Item {
id: ID! @id
article: Article! @relation(link: INLINE)
order: Int!
}
^ 这会将数据库表变成 1:N 关系而不是 n:m
然后你可以发出这样的查询:
query {
articles {
id
items(orderBy: order_ASC) {
id
}
}
}
更新“订单”的值应该是直截了当的,所以我会在这里省略它。
希望它能回答您的问题!
关于javascript - Nexus-prisma:订购嵌套连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56322284/