Warm tip: This article is reproduced from stackoverflow.com, please click
graphql graphql-js prisma prisma-graphql

Is any way to work arrays with nested objects in Prisma?

发布于 2020-04-15 09:50:57

I'm new to Prisma, I have read the documentation and have done some small projects.

Now I'm in a big project of an Architectural Application, but I am having issues with the schema of it. As you can see below, I have a type called GraphicColumn, there I save the data of some columns of an Architectural model, but the amount of columns can vary in every kind of model, and that columns have multiple data entries to save, so I'm trying to do the next thing:

type GraphicColumn {
id: ID! @id
data: [{
  column: Int!
  columnData: [Float]
}]
}

As you see, in the object, it will be the number of the column specified and the data values of that column, so it's an object for the column 1 and an amount of Int values for the data of it, and so on for the column 2, column 3...

May vary between 4 and 6 colums per model, but I'm getting errors with the data definition whem running prisma deploy. The error message is the following:

ERROR: Syntax error while parsing GraphQL query. Invalid input "{\n  id: ID! @id\n  name: String!g\n}", expected IgnoredNoComment, ImplementsInterfaces, DirectivesConst or FieldDefinitions (line 1, column 11):

type User {
        ^

{
"data": {
  "deploy": null
},
"errors": [
  {
    "locations": [
      {
        "line": 2,
        "column": 9
      }
    ],
    "path": [
      "deploy"
    ],
    "code": 3017,
    "message": "Syntax error while parsing GraphQL query. Invalid input \"{\\n  id: ID! @id\\n  name: String!g\\n}\", expected IgnoredNoComment, ImplementsInterfaces, DirectivesConst or FieldDefinitions (line 1, column 11):\ntype User {\n          ^",
    "requestId": "local:ck2sd5dbg000i0773z3pkpwx9"
  }
],
"status": 200
}

Do you know how can I replace that to some definition that works?

Questioner
Elias Ramirez
Viewed
49
realAlexBarge 2020-02-03 00:59

What you are trying to do is to link a GraphicColumn type to one or more ColumnData types. This is a relation and has a certain syntax

A relation defines the semantics of a connection between two types. Two types in a relation are connected via a relation field.

The nested data you described with relational syntax could look something like this:

type GraphicColumn {
   id: ID! @id
   data: [ColumnData!]! @relation(link: TABLE, name: "GraphicColumnData")
}

type ColumnData {
   id: ID! @id
   column: Int!
   columnData: [Float]
}

For more information regarding data modeling and schema design you can check out: https://www.prisma.io/docs/datamodel-and-migrations/datamodel-MYSQL-knul/#relations