How to filter on node labels in neo4J database using neo4j OGM

72 views Asked by At

In my node js application using @neo4j/graphql-ogm I am trying to filter the data based on additional node labels dynamically.

I am trying to make this line of code working

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

here is the complete code snip....

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 {
    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();
})();
1

There are 1 answers

1
cybersam On

Try making these changes:

  1. You have a typo in the typeDefs schema definition. Remove the stray "x" from the Query type.

  2. Make sure the neo4j server is actually running locally, and that you are connecting to it with the correct URL. Typically, the Bolt port is 7687, and SSL may not be enabled:

    const driver = neo4j.driver(
      'neo4j://localhost:7687',
      neo4j.auth.basic('abc', 'abc')
    );
    
  3. Make sure that 'abc' is the correct username and password, or change the auth info above.