tombatron / NRedisGraph

28 stars 8 forks source link

Support map as parameter #32

Open cidthecoatrack opened 1 year ago

cidthecoatrack commented 1 year ago

I am running the following code:

var assignments = new (long ScopeId, long RoleId, long UserId)[]
{
    (123, 234, 345),
    (321, 432, 543),
}
var assignmentMaps = assignments.Select(a => new { scopeId = a.ScopeId, roleId = a.RoleId, userId = a.UserId }).ToArray();

await graph.QueryAsync(AuthGraphKey,
    query: "UNWIND $assignments AS assignment " +
            "MATCH (s:Scope { id: assignment.scopeId }), (r:Role { id: assignment.roleId }) " +
            "CREATE (s)-[:ASSIGNED_TO { user_id: assignment.userId }]->(r)",
    parameters: new Dictionary<string, object> {
            { "assignments", assignmentMaps },
    });

However, I get an exception from RedisGraph:

Exception: StackExchange.Redis.RedisServerException: errMsg: Invalid input 'a': expected ';', ':', a statement option, a query hint, a clause or a schema command line: 1, column: 1, offset: 0 errCtx: assignments=[{ scopeId = 1044, roleId = 1, userId = 12291 }, { scopeId = 1045... errCtxOffset: 0 at NRedisGraph.RedisGraph.QueryAsync(String graphId, String query) at GlobalEditAPI.Authorization.Core.Infrastructure.Data.IO.RedisGraphAdapter.QueryAsync(String graphId, String query, IDictionary2 parameters)`

From its formatting, I assume this is because when you provide a "map" type as a parameter, the utility calls .ToString() instead of serializing as json. We should do that serialization, so we can support map types.

tombatron commented 1 year ago

So I'm tinkering around with this, and I THINK the issue is a limitation either with the Cypher query language or the implementation of the Cypher query language that ships with RedisGraph.

Let's try this:

cidthecoatrack commented 1 year ago

Alright, just pushed up changes. These are good catches, thanks for poiting it out. GraphQL doesn't use standard JSON formatting, so had to modify the configuration of the serializer. The "good" query (with the UNWIND) that I use in the integration test is one I tested against RedisInsights, and it works correctly.