Ahrengot
Ahrengot

Reputation: 1589

Nested types in a Graphcool model

I'm trying to add a nested data structure to a model in Graph.cool, but I can't for the life if me figure out how.

Essentially, this is what I want to achieve:

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

The code above used to work (see this SO answer for reference https://stackoverflow.com/a/50784501/641755) but when I deploy via the command line tool, this error appears $ The model 'LineItem' is missing the @model directive. Please add it. See: https://github.com/graphcool/framework/issues/817... That github link is dead.

However, I found this https://github.com/prisma/prisma/issues/817. @model is now required on all types. That means a type has to have an ID and I need to set up a relationship between the Invoice and the LineItem. Alright, that means we end up at:

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")
}

Now, this is a problem because of multiple reasons

  1. If my invoice contains 50 line items I need to create all of those first. I can't just send the line items part of the request payload for the Invoice mutation.
  2. I get a LineItem table in the database that I have absolutely no use for.

All I want, is being able to send the following payload:

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

I can see that Graph.cool has a Json type so I guess I could manually JSON.stringify() the invoice items and store them that way, but that means I get no server-side validation of the fields at all and I would really rather not end up with that solution.

Any ideas, suggestions or tips will be highly appreciated.

Upvotes: 1

Views: 42

Answers (0)

Related Questions