0

这是一个简化的示例,但我正在尝试对我的架构进行更改,以向后兼容旧版本的应用程序。我正在添加一个externalUrls参数:

CreatePost(body: String!, userId: ID!, mentions: [String!], linkUrl: String, externalUrls: [String]): Post @isAuthenticated
    @cypher(
      statement: """
        ...
        UNWIND $externalUrls as urlText
        CREATE (link:Link {
            id: apoc.create.uuid(),
            path: urlText,
            createdAt: datetime(),
            updatedAt: datetime()
        })
        CREATE (link)-[:LINKS_TO {createdAt: datetime()}]->(post)
        RETURN post
    """)

在那之前有一些密码,但我的想法是我只想运行这段代码:

UNWIND $externalUrls as urlText
CREATE (link:Link {
    id: apoc.create.uuid(), 
    path: urlText,
    createdAt: datetime(),
    updatedAt: datetime()        
})
CREATE (link)-[:LINKS_TO {createdAt: datetime()}]->(post)

如果 $externalUrls 存在/不为空。如果旧版本的应用调用

mutation CreatePost($body: String!, $userId: ID!, $mentions: [String!], $linkUrl: String) {
  CreatePost(body: $body, userId: $userId, mentions: $mentions, linkUrl: $linkUrl) {
    id
  }
}

$externalUrls: [String]甚至无法传入的地方,UNWIND 行会中断。我尝试使用CALL apoc.do.whenandFOREACH来检查是否EXISTS($externalUrls)NOT $externalUrls IS NULL两者都失败,即使它甚至没有作为参数传入。是否有另一种方法来检查参数是否具有任何值或存在?我的感觉是我总是会遇到Expected parameter(s): externalUrls,但我很想找到一种方法来避免这种情况。

4

1 回答 1

0

If you use a parameter in the query, then the parameter is required.

The best way to mimic optional parameters is to pass a map parameter, and use the entries in the map.

For example, if you pass in:

$myParams = {externalUrls:null, someOtherEntry:1, etc:true}

Then you can reference: $myParams.externalUrls, or use a WHERE clause on it, or a CASE, to take some action on it for whether it's null or not.

于 2021-04-17T00:58:04.273 回答