首页 > 解决方案 > 如何连接 Nest.js 和 NEO4j

问题描述

我是 Neo4j 的新手,希望它与 Nest.js 连接,我正在使用 Neo4j 版本 3.5.12 并按照教程进行操作。我使用 Graphql 连接 Nest.js 和 Neo4j,但我们无法获得结果并显示一些 JSON 类型和我也尝试不使用 Graphql,但 GET 和 POST 不起作用。

这是我的控制器

import { Controller, Get } from '@nestjs/common';

import {Neo4jService} from './neo4j.service';

@Controller('neo4j')
export class Neo4jController {
    constructor (private readonly retriveNodes: Neo4jService){}
@Get()
 async findAll() {
    console.log('retriving nodes as per given query in findAll() method. ');

 //return'Welcome To All';
 return this.retriveNodes.findAll();
 }
}

服务

import { Injectable, Inject } from '@nestjs/common';
import * as v1 from 'neo4j-driver';
@Injectable()
export class Neo4jService {
 constructor(@Inject("Neo4j") private readonly neo4j: v1.Driver) {}
async findAll(): Promise<any> {
 return this.neo4j.session().run('MATCH (n:Movie) RETURN n LIMIT 5');
 }
}

使用它,我得到 JSON 格式的结果。

输出

{
  "records": [
    {
      "keys": [
        "n"
      ],
      "length": 1,
      "_fields": [
        {
          "identity": {
            "low": 0,
            "high": 0
          },
          "labels": [
            "Movie"
          ],
          "properties": {
            "title": "The Martix",
            "year": {
              "low": 1999,
              "high": 0
            },
            "id": "82999192-ae57-406e-8c19-d7753d0d5748"
          }
        }
      ],
      "_fieldLookup": {
        "n": 0
      }
    },
    {
      "keys": [
        "n"
      ],
      "length": 1,
      "_fields": [
        {
          "identity": {
            "low": 1,
            "high": 0
          },
          "labels": [
            "Movie"
          ],
          "properties": {
            "title": "A Few Good Men",
            "year": {
              "low": 1992,
              "high": 0
            },
            "id": "c52c721c-c410-45a2-8d2e-824caa51847a"
          }
        }
      ],
      "_fieldLookup": {
        "n": 0
      }
    },
    {
      "keys": [
        "n"
      ],
      "length": 1,
      "_fields": [
        {
          "identity": {
            "low": 2,
            "high": 0
          },
          "labels": [
            "Movie"
          ],
          "properties": {
            "title": "Top Gun",
            "year": {
              "low": 1986,
              "high": 0
            },
            "id": "d121663a-597f-4963-8acc-c68021bee860"
          }
        }
      ],
      "_fieldLookup": {
        "n": 0
      }
    },
    {
      "keys": [
        "n"
      ],
      "length": 1,
      "_fields": [
        {
          "identity": {
            "low": 17,
            "high": 0
          },
          "labels": [
            "Movie"
          ],
          "properties": {
            "title": "The Martix",
            "year": {
              "low": 1999,
              "high": 0
            },
            "id": "453941ca-c309-419b-8cad-41d150e06b2a"
          }
        }
      ],
      "_fieldLookup": {
        "n": 0
      }
    },
    {
      "keys": [
        "n"
      ],
      "length": 1,
      "_fields": [
        {
          "identity": {
            "low": 18,
            "high": 0
          },
          "labels": [
            "Movie"
          ],
          "properties": {
            "title": "A Few Good Men",
            "year": {
              "low": 1992,
              "high": 0
            },
            "id": "79fc529c-bfd1-4f7d-9bae-7f00b6d7d540"
          }
        }
      ],
      "_fieldLookup": {
        "n": 0
      }
    }
  ],
  "summary": {
    "query": {
      "text": "MATCH (n:Movie) RETURN n LIMIT 5",
      "parameters": {

      }
    },
    "queryType": "r",
    "counters": {
      "_stats": {
        "nodesCreated": 0,
        "nodesDeleted": 0,
        "relationshipsCreated": 0,
        "relationshipsDeleted": 0,
        "propertiesSet": 0,
        "labelsAdded": 0,
        "labelsRemoved": 0,
        "indexesAdded": 0,
        "indexesRemoved": 0,
        "constraintsAdded": 0,
        "constraintsRemoved": 0
      },
      "_systemUpdates": 0
    },
    "updateStatistics": {
      "_stats": {
        "nodesCreated": 0,
        "nodesDeleted": 0,
        "relationshipsCreated": 0,
        "relationshipsDeleted": 0,
        "propertiesSet": 0,
        "labelsAdded": 0,
        "labelsRemoved": 0,
        "indexesAdded": 0,
        "indexesRemoved": 0,
        "constraintsAdded": 0,
        "constraintsRemoved": 0
      },
      "_systemUpdates": 0
    },
    "plan": false,
    "profile": false,
    "notifications": [

    ],
    "server": {
      "address": "localhost:7687",
      "version": "Neo4j/3.5.12"
    },
    "resultConsumedAfter": {
      "low": 191,
      "high": 0
    },
    "resultAvailableAfter": {
      "low": 437,
      "high": 0
    },
    "database": {
      "name": null
    }
  }
}

我想要 .run 和 .then 但在这里只有 .run 显示。没有 sugection 显示我想要上面代码中的字段、属性和 id

标签: neo4jgraphqlnestjs

解决方案


推荐阅读