首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Neo4j查询转换为GraphQL查询?

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

Stack Overflow用户
提问于 2016-04-18 14:43:44
回答 1查看 1.6K关注 0票数 4

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

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

筛选结果

关于笔记,我用:

有人有主意吗?

代码语言:javascript
复制
`
//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;

`

EN

回答 1

Stack Overflow用户

发布于 2017-04-05 22:01:18

所以我希望这个代码能帮上忙。一些纠正和注意点:

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

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

希望能帮上忙。我在这里用过它,结果就是我的名字。

代码语言:javascript
复制
//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;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36697234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档