【问题标题】:Prisma Not Returning Created Related RecordsPrisma 不返回创建的相关记录
【发布时间】:2020-03-15 11:56:11
【问题描述】:

我想创建一个新的 graphql api,但我遇到了一个我正在努力解决的问题。

代码是开源的,可以在以下位置找到:https://github.com/glitr-io/glitr-api

我想创建一个突变以创建具有关系的记录...似乎该记录是正确创建的,具有所有预期的关系(直接检查到数据库时),但 create<YourTableName> 方法返回的值, 缺少所有关系。

...所以我在 api 上收到一个错误,因为“不能为不可为空的字段 Meme.author 返回 null。”。我无法弄清楚我的代码有什么问题。

解析器如下所示:

...
const newMeme = await ctx.prisma.createMeme({
                author:  {
                    connect: { id: userId },
                },
                memeItems: {
                    create: memeItems.map(({
                        type,
                        meta,
                        value,
                        style,
                        tags = []
                    }) => ({
                        type,
                        meta,
                        value,
                        style,
                        tags: {
                            create: tags.map(({ name = '' }) => (
                                {
                                    name
                                }
                            ))
                        }
                    }))
                },
                tags: {
                    create: tags.map(({ name = '' }) => (
                        {
                            name
                        }
                    ))
                }
            });

            console.log('newMeme', newMeme);
...

此处console.lognewMeme 的值(在此解析器中返回)是:

newMeme {
  id: 'ck351j0f9pqa90919f52fx67w',
  createdAt: '2019-11-18T23:08:46.437Z',
  updatedAt: '2019-11-18T23:08:46.437Z',
}

返回的那些字段是自动生成的字段。所以我得到了以下突变的错误,因为我试图让作者:

mutation{
  meme(
    memeItems: [{
      type: TEXT
      meta: "test1-meta"
      value: "test1-value"
      style: "test1-style"
    }, {
      type: TEXT
      meta: "test2-meta"
      value: "test2-value"
      style: "test2-style"
    }]
  ) {
    id,
    author {
      displayName
    }
  }
}

任何人都可以看到可能导致此问题的问题吗? (如前所述...当直接检查到数据库时,记录已成功创建,所有关系都符合预期)。

【问题讨论】:

    标签: graphql prisma prisma-graphql


    【解决方案1】:

    如 prisma 文档中所述,Prisma 客户端函数写入数据的承诺,例如 createMeme 函数,仅返回对象的标量字段:

    在数据库中创建新记录时,create-method 采用一个输入对象,该对象包装了记录的所有标量字段。 创建的。它还提供了一种为 模型,这可以使用嵌套对象写入来提供。

    每个方法调用都会为一个对象返回一个 Promise,该对象包含所有 刚刚创建的模型的标量字段。

    见:https://www.prisma.io/docs/prisma-client/basic-data-access/writing-data-JAVASCRIPT-rsc6/#creating-records

    要返回对象的关系,您需要使用信息片段或 fluent api 再次读取对象,请参阅:https://www.prisma.io/docs/prisma-client/basic-data-access/reading-data-JAVASCRIPT-rsc2/#relations

    【讨论】:

      猜你喜欢
      • 2022-01-04
      • 1970-01-01
      • 2022-12-02
      • 2016-05-17
      • 2022-01-16
      • 1970-01-01
      • 2019-06-27
      • 1970-01-01
      • 2021-03-07
      相关资源
      最近更新 更多