Graphcool模型中的嵌套类型

时间:2019-03-27 20:04:47

标签: graphql graphcool

我正在尝试在Graph.cool中的模型中添加嵌套数据结构,但是如果我想出了办法,我将一辈子都没用。

本质上,这是我要实现的目标:

type Invoice @model {
  id: ID! @isUnique
  user: Int!
  createdAt: DateTime!
  items: [LineItem!]!
}

type LineItem {
  # Product is defined elsewhere and is unrelated to the problem
  product: Product! @relation(name: "InvoiceProduct")!
  amount: Int!
}

上面的代码曾经可以工作(请参见此SO答案以获取参考https://stackoverflow.com/a/50784501/641755),但是当我通过命令行工具进行部署时,会出现此错误 $ The model 'LineItem' is missing the @model directive. Please add it. See: https://github.com/graphcool/framework/issues/817 ...那个github链接已死。

但是,我发现了这个https://github.com/prisma/prisma/issues/817。现在,所有类型都需要@model。这意味着类型必须具有ID,并且我需要在InvoiceLineItem之间建立关系。好吧,这意味着我们最终到达:

type Invoice @model {
  id: ID! @isUnique
  createdAt: DateTime!
  user: Int!
  items: [LineItem!]! @relation(name: "InvoiceLineItem")
}

type LineItem @model {
  id: ID! @isUnique
  product: Product! @relation(name: "InvoiceProduct")!
  amount: Int!
  invoice: Invoice! @relation(name: "InvoiceLineItem")
}

现在,由于多种原因,这是一个问题

  1. 如果我的发票包含50个订单项,则需要先创建所有订单项。我不能只是将订单项的部分订单项发送给Invoice变更。
  2. 我在数据库中得到了一个LineItem表,而我绝对没有用。

我想要的就是能够发送以下有效载荷:

// Invoice
{
  user: 123,
  items: [
    {
      product: "*id*"
      amount: 3
    },
    {
      product: "*id*"
      amount: 5
    }
  ]
}

我可以看到Graph.cool有一个Json type,所以我想我可以手动JSON.stringify()发票项目并以这种方式存储它们,但这意味着我没有对字段进行服务器端验证根本没有,我真的不希望最终得到那种解决方案。

任何想法,建议或技巧都将受到高度赞赏。

0 个答案:

没有答案
相关问题