villadora / cayley.js

Nodejs Client for Cayley
Other
57 stars 14 forks source link

Basic querying doesn't seem to work #9

Open davidascher opened 8 years ago

davidascher commented 8 years ago

Using this test:

var log = require('winston');
var cayley = require('cayley');
var client = cayley("http://localhost:64210/", "v1");
var g = graph = client.graph;

client.write([{
  subject: 'subject',
  predicate: 'predicate',
  object: 'object'}
], function(err, body, res) {
  if (err) {
    log.info("error writing", err);
  } else {
    log.log('debug', 'Wrote');
  }
});

g.V('subject').Out('predicate').Tag('obj').All(function(err, result) {
  console.log("err", err);
  console.log("result", result);
})

I get null results in the last function, but the triple gets written to the database as visible using the HTTP user interface.

Maybe something in the latest cayley db broke this library?

davidascher commented 8 years ago

After some wire sniffing, it seems that the request module by default (now?) sends the data as json with a Content-Type of application/json, which seems to confuse cayley.

Changing the content-type in client.js to text fixes my test.

I don't think that's the right content type, but I'm not sure what is.

Maybe @barakmich knows.

jinMaoHuiHui commented 7 years ago

I add the headers content-type: text/plain and many other values, the query result is still null. And then I write a test use request module

var request = require('request');
request.post({
  uri: 'http://localhost:64210/api/v1/query/gremlin',
  body: 'g.V("Subject1").All()',
  json: true
},  function(err, res, body) {
  console.log('test---',res.request, '-----test')
})
//  requst with param json: true, I get the result null
Request {
  // ....
  body: '"g.V(\\"Subject1\\").All()"',
  // ...
}

There is a pair of superfluous commas before g.V... So I comment the json: true in cayley/lib/v1/gremlin.js line15, and add body = JSON.parse(body) in callback line 17, It worked. Thank you @davidascher !

jinMaoHuiHui commented 7 years ago

With the String Template literals of ES2015, finaly I found maybe I can query easily without cayley.js , and send gremlin js code block as post body...

    query(text) {
      return new Promise((resolve, reject) => {
        this.ctx.curl('http://localhost:64210/api/v1/query/gremlin', {
          method: 'POST',
          contentType: 'json',
          data: text 
        })
        .then((res) => {
          if (typeof res == 'object' && res.data) {
            try {
              var result = JSON.parse(res.data.toString()).result
              if (result) { 
                resolve(result) 
              } else {
                resolve([])
                //  [cayley]: node may not exist
              }
            } catch(err) {
              reject(err)
            }
          } else {
            reject(new Error('[cayley]: response data error, type error'))
          }
        })
        .catch((error) => {
          console.error(error)
        })
      });
    }