【问题标题】:How to add sorting for field object of graphql type which refers to different graphql type?如何为引用不同graphql类型的graphql类型的字段对象添加排序?
【发布时间】:2018-03-05 17:51:17
【问题描述】:

我正在使用 Neo4j dB 并使用模式理解来返回值。我有两种类型的人和朋友: (p:Person)-[:FRIEND_WITH]->(f:Friend)

Type Person{
  id: String
  name: String
  friends: [Friend]
}

 Type Friend{
  id: String
  name: String

}   

 type Query {
    persons( limit:Int = 10): [Person]
    friends( limit:Int = 10): [Friend]
  }

我想要做的是在“persons”查询执行时按升序拉取字段 friends 的数组列表(存在于 Person Type 中)。例如

  {
  "data": {
   "persons": {
     "id": "1",
     "name": "Timothy",
     "friends": [
       {
        "id": "c3ef473",
        "name": "Adam",     
       },
       {
        "id": "ef4e373",
        "name": "Bryan",     
       }, 
       (  
        "id": "e373ln45",
        "name": "Craig",     
       },    

我应该怎么做?我研究了排序,但是当我们在 neo4j 中使用模式理解时,我没有发现任何关于数组对象排序的具体内容。任何建议都会非常有帮助!

【问题讨论】:

    标签: neo4j graphql


    【解决方案1】:

    我使用了lodash的sortBy函数将结果按升序返回。 这是 graphql 解析器查询:

        persons(_, params) {
           let query = `MATCH (p:Person)
                        RETURN p{
                       .id,
                       .name,
                       friends: [(p)-[:FRIEND_WITH]->(f:Friend)) | f{.*}]
                       }
    
                       LIMIT $limit;`;
    
          return dbSession().run(query, params)
         .then(result => {
           return result.records.map(record => {
             let item = record.get("p");
                 item.friends = sortBy(item.friends, [function(i) {
                 return i.name;
            }]);
              return item;
          })
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-15
      • 2020-05-29
      • 2018-04-07
      • 2019-10-15
      • 2019-06-24
      • 2020-07-24
      • 2018-06-29
      • 2021-06-19
      • 2021-06-13
      相关资源
      最近更新 更多