在 Neo4j GraphQL 中使用 onConnectOrCreate 时,如何向新节点添加属性?

发布于 2025-01-10 21:10:21 字数 1181 浏览 0 评论 0原文

我正在使用 Neo4j 的 GraphQL 库,尝试使用 connectOrCreate 来创建新图像,其定义如下:

type Image {
    id: ID! @id(autogenerate: true),
    url: String! @unique, 
    creator: User! @relationship(type: "CREATED_BY", direction: OUT),
}

并且根据 Neo4j 网站上的文档,我应该能够说如果未找到具有 url 字段的节点,然后创建一个并设置一些属性。像这样:

mutation NEW_POST {
  createPosts(input: { images: { 
    connectOrCreate: [{ 
      where: { node: { url: "https://picsum.photos/600" }}
      onCreate: { where: { node: { creator: { node: { id: "userId"}}}}},
    }]}}){
  posts {
    id
    }
  }
}

但是,对于 onCreate Field 输入,我唯一的选项是 idurl。意思是,我实际上无法像这里那样添加创建者属性。

“Field \”creator \”不是由类型\“ImageOnCreateInput \”定义的。”,

您认为这是有意的,还是知道我应该在哪里寻找指导? 我正在查看此文档: https://neo4j.com/docs /graphql-manual/current/mutations/create/ 显示 onCreate: { node: { title: "Forrest Gump" } } 实现了我希望实现的目标。

输入图片此处描述

I'm using Neo4j's GraphQL library, trying to use connectOrCreate for a new Image, which is defined like this:

type Image {
    id: ID! @id(autogenerate: true),
    url: String! @unique, 
    creator: User! @relationship(type: "CREATED_BY", direction: OUT),
}

And based on the documentation on the Neo4j website, I should be able to say if a node with the url field is not found, then create one and set some properties. Like this:

mutation NEW_POST {
  createPosts(input: { images: { 
    connectOrCreate: [{ 
      where: { node: { url: "https://picsum.photos/600" }}
      onCreate: { where: { node: { creator: { node: { id: "userId"}}}}},
    }]}}){
  posts {
    id
    }
  }
}

However, for the onCreate Field Input, my only options are id and url. Meaning, I can't actually add the creator property like I do here.

"Field \"creator\" is not defined by type \"ImageOnCreateInput\".",

Do you think this is intended, or any idea where I should look for guidance?
I'm looking at this documentation: https://neo4j.com/docs/graphql-manual/current/mutations/create/ which shows onCreate: { node: { title: "Forrest Gump" } } achieving what I hope to achieve.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朦胧时间 2025-01-17 21:10:21

如果 url 为 https://picsum.photos/600 的节点不存在,则将使用突变的 onCreate 部分中提供的信息创建一个新节点。创建的。此“onCreate”节点(使用ImageOnCreateInput)将自动连接指定为“锚点”的节点。请参阅我在您提供的稍作修改的突变中的评论:

mutation NEW_POST {
  createPosts(
    input: {
      id: "userId"     // Note this 'id' here. This is the 'anchor' node.
      images: {
        connectOrCreate: [
          {
            where: { node: { url: "https://picsum.photos/600" } }
            onCreate: { node: { name: "the name" } }       // If the node with "https://picsum.photos/600" does not exist, this will create a Post with "the name" and also connect it to the anchor node with id "userId".
          }
        ]
      }
    }
  ) {
    posts {
      id
    }
  }
}

您可以尝试和使用文档中提供的 typeDefs 和突变(https://neo4j.com/docs/graphql-manual/current/mutations/create/#_connectorcreate_relationships)来查看 connectOrCreate 关系突变是如何运行的。

If the node with the url https://picsum.photos/600 does not exist, a new node node with the information provided in the onCreate part of the mutation will be created. This "onCreate" node (with ImageOnCreateInput) will automatically connect the node that is specified as the "anchor". See my comments in the slightly modified mutation that you provided:

mutation NEW_POST {
  createPosts(
    input: {
      id: "userId"     // Note this 'id' here. This is the 'anchor' node.
      images: {
        connectOrCreate: [
          {
            where: { node: { url: "https://picsum.photos/600" } }
            onCreate: { node: { name: "the name" } }       // If the node with "https://picsum.photos/600" does not exist, this will create a Post with "the name" and also connect it to the anchor node with id "userId".
          }
        ]
      }
    }
  ) {
    posts {
      id
    }
  }
}

You can experiment and play with the typeDefs and mutation provided in the documentation (https://neo4j.com/docs/graphql-manual/current/mutations/create/#_connectorcreate_relationships) to see how the connectOrCreate relationship mutation is operating.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文