为了账号安全,请及时绑定邮箱和手机立即绑定

Nexus-prisma:排序嵌套连接

Nexus-prisma:排序嵌套连接

江户川乱折腾 2021-06-08 13:09:52
在架构中保持嵌套对象顺序的最佳方法是什么。我的架构: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 处理它。
查看完整描述

1 回答

?
慕莱坞森

TA贡献1810条经验 获得超4个赞

对于 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

    }

  }

}

更新“订单”的值应该是直接的,所以我会在这里省略它。


希望它能回答你的问题!


查看完整回答
反对 回复 2021-06-11
  • 1 回答
  • 0 关注
  • 151 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信