javascript - Nexus-prisma:订购嵌套连接

标签 javascript graphql prisma-graphql nexus-prisma

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

我的模式:

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/

相关文章:

javascript - 如何将 kineticjs 舞台复制到另一个 Canvas

javascript - 当前页面刷新时选择的 div?

javascript - 我可以将 JSNice 与 ES6 一起使用吗?

amazon-web-services - AWS AppSync 订阅参数

functional-programming - 如何摆脱记录构建的非大写键值?

mysql - docker 和 mysql : Got an error reading communication packets

javascript - JS 中使用高阶函数的困惑

json - 如何使用 GraphQL 在 VueJS/Gridsome 中迭代 JSON 文件中的数据?

javascript - 无法转换/规范化/修改 GraphQL 查询的响应 - Apollo 客户端

prisma-graphql - 如何删除prisma2中的关系项