【问题标题】:Neo4j: Relating a node to a specific node with the same nameNeo4j:将节点与具有相同名称的特定节点相关联
【发布时间】:2020-03-15 01:58:35
【问题描述】:

我有一份美国所有州和城镇的列表。各州可以有相同的城镇名称,因此我正在检查以确保每个城镇都与其受人尊敬的州相连。

我的活动连接到一个城镇,但问题是我的活动连接到不同州的多个城镇。如何将我的新活动与某个州内的城镇联系起来? 这是我的架构。

type State {
  stateName: String!
  stateAbbreviation: String!
  towns: [Town] @relation(name: "STATE_OUT", direction: "OUT")
}

type Town {
  state: String!
  name: String!
  events: [CraftShowEvent]
}

type Event {
  name: String!
  day: String!
  month: String!
  time: String!
  town: String!
  state: String!
}

【问题讨论】:

    标签: neo4j graphql grandstack


    【解决方案1】:

    在创建图形数据库时,请确保在 "Town" 节点和 "Event" 节点之间有此数据模型

    EVENT -> [] -> Town
    WHERE 
      EVENT.town = Town.Name 
    AND
      EVENT.state = Town.state
    

    【讨论】:

      【解决方案2】:

      我只是想更新我的问题,以防其他人遇到这个问题。我使用的是 neo4j 和 graphql,它们会根据我的 graphql 模式自动生成查询和突变。

      相反,我进行了修改并使用了@cypher 指令。我需要更多地控制我的数据的保存方式。

      type Mutation {
        setBatchTown(listOfTowns: [BatchTown]): Town
          @cypher(statement:"""
            UNWIND $listOfTowns AS el
            MERGE (t:Town {name: el.name, state: el.state})
            WITH t
            MATCH (s:State)
            WHERE t.state = s.name
            CREATE (t)-[:STATE_OUT]->(s)
          """)
      
        setEvent(event: InputEvent): Event
          @cypher(statement:"""
            MERGE (e:Event { name: $event.name, day: $event.day, month: $event.month, time: $event.time, town: $event.town, state: $event.state })
            WITH e
            MATCH (s:State), (t:Town {name: e.town, state: e.state})
            WHERE e.state = s.name AND e.town = t.name
            MERGE (e)-[:TOWN_OF]->(t)
            RETURN t
          """)
       }
      

      然后当我提出请求时,我可以做这样的事情。

              client.mutate({ variables: { 
                  listOfTowns: [{
                    name: 'town1'
                    state: 'VA'
                  },
                  {
                    name: 'town2'
                    state: 'WA'
                  }] 
              }, mutation: CREATE_STATE_MUTATION })
                .then(() => resolve())
                .catch(err => reject(err))
              })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-31
        相关资源
        最近更新 更多