【问题标题】:How to transform a Neo4j Query to a GraphQL query?如何将 Neo4j 查询转换为 GraphQL 查询?
【发布时间】:2016-04-18 14:43:44
【问题描述】:

我是堆栈溢出的新手,我真的认为你可以帮助我。

我几天前开始研究 NEO4JgraphQL。 在下面的代码中,我的 neo4j 请求可以正常工作。 关于graphQL,我想我犯了一个错误,因为graphQL请求后的结果为null。

Screen of the result

对于笔记,我正在使用:

有人有想法吗?

`
//exampleToNeo4j.js
var neo4j = require("node-neo4j");
var db = new neo4j("http://user:pass@localhost:7474");
class Examples {
    findAll = function(){
    const cypher = "MATCH (a:Article) RETURN a";
    var resultat = db.cypherQuery(cypher.function(err, result){
        if(err) throw err;  
        var resultInt = [];
        for (var i=0; i<result.data.lenght; i++){
            resultInt[i]=result.data[i]._id:
        }
        return resultInt
    }
    return resultat;
    }
}
Export default Examples;

//example.js
import {
GraphQLInterfaceType,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
GraphQLInt,
GraphQLFloat,
GraphQLList,
GraphQLNonNull} from 'graphql';
import Examples from '../../lib/exampleToNeo4j';
const examples = new Examples();

const exampleType = new GraphQLObjectType({
        name: 'exampleType',
        description: 'Example description',
        fields: () => ({
        id: {
            description: "Example ID",
            type: GraphQLInt
        }
    })
})
;

const getAllExample = {
        description: 'Have all nodes',
        type: new GraphQLList(exampleType),
        resolve: (root)=>{
            return examples.findAll();
        }

};

export const getAllExamples = getAllExample;

`

【问题讨论】:

  • 我刚刚注意到for (var i=0; i&lt;result.data.lenght; i++){ 中有一个拼写错误“lenght”,可以解决吗?
  • 如果您有好的解决方案,请告诉我 ;)

标签: javascript database neo4j request graphql


【解决方案1】:

所以,我希望这段代码有所帮助。 一些更正和注意点:

连接指向错误的默认端口,您必须使用“螺栓”连接到另一个“http”,这是您可以连接的两个端口,例如: Db = new neo4j('http://neo4j:neo4j@localhost:7474'); 或者 Var driver = neo4j.driver("bolt: // localhost: 7687")

还要小心回调,如果不是,您需要放置一个“setTimeout”才能正常工作; 方法中的另一件事 “Db.cypherQuery (cypher, getReturn)”它不会返回,因此它会将 getReturn 作为回调; 并且不要忘记 JS 是 异步;

我希望它有所帮助。我在这里使用了它,它只是给我打电话。

//example.js
var GraphQL = require('graphql')
var Examples  = require('./exampleToNeo4j.js')

var express = require('express')
var app = express()

const exampleType = new GraphQL.GraphQLObjectType({
        name: 'exampleType',
        description: 'Example description',
        fields: () => ({
        id: {
            description: "Example ID",
            type: GraphQLInt
        }
    })
})
const getAllExample = {
        description: 'Have all nodes',
        type: new GraphQL.GraphQLList(exampleType),
        resolve: (root)=>{
            return Examples.findAll(root)
        }
};

app.get('/', function(req, res) {

    getAllExample.resolve(function (data){
        console.log(data)
        res.send(data)
    })

})

app.listen(3000)
console.log('porta 3000')



//exampleToNeo4j.js
var neo4j = require("node-neo4j");
var db = new neo4j("http://localhost:7474");
var Examples = {
    findAll: function(call){
        const cypher = "MATCH (a:Article) RETURN n LIMIT 2"        
        db.cypherQuery(cypher, getReturn)        
        function getReturn(err, result){
            if(err) throw err;            
            var resultInt = [];
            for (var i=0; i<result.data.length; i++){
                resultInt[i]=result.data[i]._id
            }
            //console.log(result)
            call(resultInt)
        }
    }
}
module.exports = Examples;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-21
    • 2020-02-04
    • 2021-11-03
    • 2020-11-14
    • 2020-07-19
    • 2021-10-11
    • 2021-02-13
    • 2020-10-21
    相关资源
    最近更新 更多