【问题标题】:Nexus-prisma: order nested connectionsNexus-prisma:排序嵌套连接
【发布时间】:2019-10-12 19:20:56
【问题描述】:

保持架构中嵌套对象顺序的最佳方法是什么。

我的架构:

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 来处理它。

【问题讨论】:

  • 你为什么不使用 orderBy 参数?
  • 那么您的意思是用订单索引更新所有嵌套对象,然后按 orderBy 排序吗?

标签: javascript graphql prisma-graphql nexus-prisma


【解决方案1】:

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

^ 这会将 db 表转换为 1:N 关系而不是 n:m

然后你可以发出这样的查询:

query {
  articles {
    id
    items(orderBy: order_ASC) {
      id
    }
  }
}

更新“订单”的值应该是直截了当的,所以我将在此处省略。

希望它能回答你的问题!

【讨论】:

    猜你喜欢
    • 2016-07-27
    • 2019-06-18
    • 2018-03-25
    • 2021-10-05
    • 2021-09-30
    • 2019-09-13
    • 2020-02-03
    • 2021-01-19
    • 1970-01-01
    相关资源
    最近更新 更多