在我使用 @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();
})();
尝试进行以下更改:
您在
typeDefs
模式定义中存在拼写错误。从 Query
类型中删除杂散的“x”。
确保 neo4j 服务器实际上在本地运行,并且您使用正确的 URL 连接到它。通常,Bolt 端口为 7687,并且可能未启用 SSL:
const driver = neo4j.driver(
'neo4j://localhost:7687',
neo4j.auth.basic('abc', 'abc')
);
确保“abc”是正确的用户名和密码,或更改上面的身份验证信息。