arangoclient.net
arangoclient.net copied to clipboard
AQL.document(id) function implementation
Is there implementation of AQL's DOCUMENT function which returns document by id?
Basically every time you're using graph operations such as GRAPH_NEIGHBORS, GRAPH_COMMON_NEIGHBORS
etc. you're getting list of handles back and in order to get the actual documents you need to perform join on id with vertex collections whereas AQL natively supports document function which returns document by handle.
Am I missing something?
Thanks
Example queries below:
FOR id IN GRAPH_NEIGHBORS('Test', 'doc/id', {direction:'outbound', maxDepth:1, edgeExamples:[{attr:'value'}]})
FOR u IN User
FILTER u._id == id
RETURN u
Instead below:
FOR id IN GRAPH_NEIGHBORS('Test', 'doc/id', {direction:'outbound', maxDepth:1, edgeExamples:[{attr:'value'}]})
RETURN document(id)
@drgraduss aql has Document
function you can look it at Miscellaneous functions, unfortunately i didn't add Miscellaneous functions
yet, but you can add them with the following code:
this is how you can add custom AQL functions too:
public class CustomAQL
{
[UserFunction(Name = "document")]
public static T Document<T>(string id)
{
throw new Exception("use custom AQL functions inside linq queries");
}
[UserFunction(Name = "document")]
public static IList<T> Document<T>(IList<string> ids)
{
throw new Exception("use custom AQL functions inside linq queries");
}
}
// and use them like:
var query1 = db.Query().Select(_ => CustomAQL.Document<Person>("Person/123"));
// output: return document("Person/123")
Console.WriteLine(query1.GetQueryData().QueryReplacedWithVariables(db));
var query2 = db.Query().Select(_ => CustomAQL.Document<Person>(new string[] { "Person/123" }));
// output: return document( ["Person/123"] )
Console.WriteLine(query2.GetQueryData().QueryReplacedWithVariables(db));
i will add Miscellaneous functions
in next release
A bit of context. Consider query below:
db.Query().For(x =>
AQL.GraphNeighbors<Vertex>(
"graph-name",
new { _key = "vertex-key" },
new
{
direction = EdgeDirection.Outbound,
maxDepth = 1,
edgeExamples = new[] { new { attr = "edge-attr-filter" } }
}));
This query will throw ArangoDB.Client.Common.Newtonsoft.Json.JsonSerializationException: cannot convert value 'string' to type 'Vertex'
. Using AQL.GraphNeighbors<Vertex>()
and AQL.GraphNeighbors<string>
will produce the same query:
for
generated_0in graph_neighbors( @P1 , @P2 , @P3 ) return
generated_0``
which works in the second example.
If you change query to
for
generated_0 in graph_neighbors( @P1 , @P2 , @P3 ) return document(
generated_0)
(by adding document
function) query works in first example as well.
That was my original issue. Sorry for being a bit long.
@drgraduss i'm not deep into graphs but if you look at the method signature:
public static IList<TVertexResult> GraphNeighbors<TVertexResult>(string graphName, object vertexExample, object options)
the reason that i named the generic parameter TVertexResult
is for includeData
option parameter which if set to true
result will be full documents instead of their ids. this may solve your problem
otherwise you can still write it like below, but includeData
would give you more performance(at least i think)
var query = db.Query().For(_ =>
AQL.GraphNeighbors<string>(
"graph-name",
new { _key = "vertex-key" },
new
{
direction = EdgeDirection.Outbound,
maxDepth = 1,
edgeExamples = new[] { new { attr = "edge-attr-filter" } }
})
.Select(id=> CustomAQL.Document<Vertex>(id)));
Thanks a lot, @ra0o0f. I completely missed includeData
option. CustomAQL works as well,
but I will stick with includeData
. It's just not obvious when it's legal to use document type as TVertexResult
and not a string
.
Thanks for your help!
@drgraduss you are right, there should be document for all of this but i can't provide it until next month. but the API is so much similar to the internal javascript API at the ArangoDB website, i hope it can help until i provide one
@ra0o0f Not a problem at all. Trial and error works best as usual :)
The attached patch allows db.Document<T>(id) to be used in LINQ statements. For example:
from user in db.Query<User>()
let dept = db.Document<Department>(user.dept_id)
where dept.name == "cars"
select user
This would previously throw an exception like "Method Document not being supported in ArangoLinqProvider."