如何将Neo4j查询转换为GraphQL查询?

时间:2016-04-18 14:43:45

标签: javascript database neo4j request graphql

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

我几天前就开始研究 NEO4J graphQL 。 在下面的代码中,我的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;

`

1 个答案:

答案 0 :(得分:1)

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

连接指向错误的默认端口,您必须使用&#34; bolt&#34;到另一个&#34; http&#34;,这些是你可以连接的两个端口,例如: Db = new neo4j(&#39; http:// neo4j:neo4j @ localhost:7474&#39;); 要么 Var driver = neo4j.driver(&#34; bolt:// localhost:7687&#34;)

另外要小心回调,否则你需要放一个&#34; setTimeout &#34;为了运作; 方法中的另一件事 &#34; Db.cypherQuery(cypher,getReturn)&#34;它没有返回所以它将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;