1

请举例说明如何使用 JSON 类型

generator prisma_client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model t {
  id  Int   @default(autoincrement()) @id
  val Json?
}

我需要突变代码。

我用的答案来自

带有 JSON 补丁的 GraphQL 突变

crud通过启用

import { use } from 'nexus'
import { prisma } from 'nexus-plugin-prisma'

use(prisma({features:{crud:true}}))

并发送这个突变:

mutation {
  createOnet(data: {
    val: "{ \"name\": \"michael\" }"
  }) {
    id
    val
  }
}

但我有回应:

{
  "error": [
    {
      "message": "Expected type Json, found \"{ \\\"name\\\": \\\"michael\\\" }\"; Cannot read property 'forEach' of undefined",
      "locations": [
        {
          "line": 2,
          "column": 26
        }
      ]
    }
  ]
}
4

1 回答 1

1

它应该如下:

mutation {
  createOnet(data: {
    val: { name: "michael" }
  }) {
    id
    val
  }
}

不需要转义,因为 Nexus 会自动为您处理。

于 2020-07-06T08:29:31.070 回答