简体   繁体   English

Nexus-prisma:命令嵌套连接

[英]Nexus-prisma: order nested connections

What is the best way to keep order of nested objects in the schema. 在架构中保持嵌套对象顺序的最佳方法是什么?

My schema: 我的架构:

type Article {
  id: ID! @id
  pages: [Page!]!
}

type Page {
  id: ID! @id
}

This is how I'm trying to sort the pages(unsuccessfully): 这就是我试图对页面进行排序的方法(失败):

  updateArticle({
    variables: {
      aricle.id,
      data: {
        pages: {
          connect: reorderPages(aricle.pages)
        }
      }
    }

The resolver: 解析器:

 t.field("updateArticle", {
      type: "Article",
      args: {
        id: idArg(),
        data: t.prismaType.updateArticle.args.data
      },
      resolve: (_, { id, data }) => {
        return ctx.prisma.updateArticle({
          where: { id },
          data
        });
      }
    });

I understand why this approach is wrong. 我理解为什么这种方法是错误的。 I guess that the order should be written in the database by an order index in the connection table. 我想订单应该通过连接表中的订单索引写入数据库。 I don't know how to process that by GraphQL/Nexus/Prisma/MySQL. 我不知道如何通过GraphQL / Nexus / Prisma / MySQL处理它。

For N:M to relations the schema would look like this: 对于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!]!
}

And then query articles in a more "Relay" like fashion with edges & nodes 然后使用边缘和节点以更像“Relay”的方式查询文章

query {
  articles {
    items(orderBy: order_ASC) {
      item {
        title
      }
    }
  }
}

And if N:M isn't needed, you can update the schema definition like so: 如果不需要N:M,您可以更新架构定义,如下所示:

type Article {
  id: ID! @id
  items: [Item!]!
}

type Item {
  id: ID! @id
  article: Article! @relation(link: INLINE)
  order: Int!
}

^ this will turn the db tables into 1:N relation ship rather than n:m ^这会将db表转换为1:N关系而不是n:m

Then you can issue a query like so: 然后您可以发出如下查询:

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

Updating the value of the "order" should be straight forward so I'll omit it here. 更新“订单”的值应该是直截了当的,所以我在这里省略它。

Hope it answers your question! 希望它能回答你的问题!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM