2016-04-18 7 views
3

私はスタックオーバーフローで新しく、本当にあなたが私を助けることができると思います。Neo4jクエリをGraphQLクエリに変換する方法は?

私はのNeo4jgraphQL上で動作するように数日前に開始しました。 次のコードでは、私のneo4j要求は問題なく動作します。 graphQLについて、私は間違いをしました。なぜなら、graphQLリクエストの後の結果がnullであるからです。注意事項については

Screen of the result

、私が使用しています:

誰もがアイデアを持っていますか?

` 
//exampleToNeo4j.js 
var neo4j = require("node-neo4j"); 
var db = new neo4j("http://user:[email protected]: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; 

`

+0

for(var i = 0; i

+0

良い解決策があれば教えてください;) – Aleksandrenko

答えて

1

だから、私はこのコードができます願っています。訂正と注意の いくつかのポイント: :接続が間違っているデフォルトのポートを指している

、あなたは他の「HTTP」に「ボルト」を使用する必要があり、これらを使用すると、たとえば接続できる2つのポートですDb = new neo4j( 'http:// neo4j:neo4j @ localhost:7474'); または ヴァールドライバ= neo4j.driver( "ボルト:// 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; 
関連する問題