如何为graphql类型的字段对象添加排序,引用不同的graphql类型?

问题描述 投票:0回答:1

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

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]
  }

我想要做的是在执行“人员”查询时以升序拉出字段朋友的数组列表(存在于“人员类型”中)。对于例如

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

我该怎么办?我研究了排序,但是当我们在neo4j中使用模式理解时,我没有找到关于数组对象排序的任何特定内容。任何建议都会非常有用!

neo4j graphql
1个回答
0
投票

我使用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;
      })
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.