I'm trying to make a generic code that can filter on object connected to an user. These objects can be of different types, with different properties etc.
Basically I want to implement this method:
public string GetRelatedObjects(string sourceObject, string userId){
var resQuery = GraphDB.Cypher
.Match("(src:" + sourceObject + ")--(usr:User { Id:{userId} })")
.WithParam("userId", userId)
.Return(src => src.As<object>());
var result = await resQuery.ResultsAsync;
return JsonConvert.SerializeObject(result);
}
The issue is when I use .As<object>()
I get back an empty item.
When I put a concrete type, such as .As<User>()
I get back the results I expect. Is there a way to get what I'm trying to get with Neo4JClient or do I have to go lower level somehow?
So under the hood Neo4jClient uses Json.NET to deserialize the output from Neo4j into the class you specify. As you specify object
which has no properties you get no properties. Neither Neo4jClient nor Json.NET know what your actually after, so you can only get the object
back. Now, solution time - you can use dynamic
instead.
public async Task<string> GetRelatedObjectsAsJson(string sourceObject, string userId)
{
var resQuery = GraphDB.Cypher
.Match(string.Format("(src:{0})--(usr:User {{ Id:{{userId}} }})", sourceObject))
.WithParam("userId", userId)
.Return(src => src.As<Node<string>>());
var result = await resQuery.ResultsAsync;
var output = result.Select(node => JsonConvert.DeserializeObject<dynamic>(node.Data)).ToList();
return JsonConvert.SerializeObject(output);
}
I think this will get what you want, if I were you, I would probably return an Task<IEnumerable<dynamic>>
instead of reserializing to a string
, but I don't know what you want to use the results for, so maybe not appropriate.
Please see Casting nodes of an unknown type for more info.