如何使用neo4j OGM过滤neo4J数据库中的节点标签

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

在我使用 @neo4j/graphql-ogm 的 Node js 应用程序中,我尝试根据附加节点标签动态过滤数据。

我正在尝试使这行代码正常工作

 *const [brands] = await Brand.find({labels: { _in: nodeLabels }});*

这是完整的代码片段......

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const { Neo4jGraphQL } = require('@neo4j/graphql');
const neo4j = require('neo4j-driver');
const { OGM, cypher } = require("@neo4j/graphql-ogm");


// Connect to Neo4j database
const driver = neo4j.driver(
    'neo4j+s://localhost:80',
    neo4j.auth.basic('abc', 'abc')
);


// Define GraphQL schema
const typeDefs = gql`
  type Brand {
    name: String!
    url: String
    # Add fields for other node labels here
  }

  type Query {x
    brands(nodeLabels: [String!]!): [Brand!]!
  }
`;


// Initialize OGM (Object-Graph Mapping) for Neo4j
const ogm = new OGM({ typeDefs, driver });
const session = driver.session();

// Create resolvers
const resolvers = {
  Query: {
    brands: async (_parent, { nodeLabels }) => {

    await ogm.init();
    const Brand = ogm.model("Brand");

    const [brands] = await Brand.find({labels: { _in: nodeLabels }});
    console.log([brands]);
    return [brands];
    }
  }
};

// Create an ApolloServer instance with Neo4jGraphQL
const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req }) => ({ req, cypher: ogm }),
});

/**
 * Main program
 * 
 * @returns {boolean} success status
 */
const main = async () => {
    await server.start();
    const app = express();
    server.applyMiddleware({ app });
    
    // Start the server
    const PORT = process.env.PORT || 4000;
    app.listen(PORT, () => {
      console.log(`Server running on http://localhost:${PORT}${server.graphqlPath}`);
    });
};


(async () => {
    await main();
})();
node.js neo4j graphql neo4j-graphql-js
1个回答
0
投票

尝试进行以下更改:

  1. 您在

    typeDefs
    模式定义中存在拼写错误。从
    Query
    类型中删除杂散的“x”。

  2. 确保 neo4j 服务器实际上在本地运行,并且您使用正确的 URL 连接到它。通常,Bolt 端口为 7687,并且可能未启用 SSL:

    const driver = neo4j.driver(
      'neo4j://localhost:7687',
      neo4j.auth.basic('abc', 'abc')
    );
    
  3. 确保“abc”是正确的用户名和密码,或更改上面的身份验证信息。

© www.soinside.com 2019 - 2024. All rights reserved.