v5.5.0 - Feedback wanted on ExecutableQuery
📣
#677
Replies: 2 comments 1 reply
-
The concept of the streamprocessor seems nice, but doing something more like the following would be preferred IMHO. var birthYears = await driver
.ExecutableQuery("MATCH (p:Person WHERE p.name = $name) RETURN p.born AS born")
.WithConfig(new (impersonatedUser: "someone-else"))
.WithParameters(new { name = "Tom Hanks" })
.ExecuteAsAsyncEnumerable(); //maybe just AsAsyncEnumerable()
await foreach (var birthYear in birthYears)
{
// some processing
yield return processingResult;
} Using this approach, the results still gets eagerly loaded, but without needing to allocate a delegate. Regarding the approach with parameters, this is way more just a personal preference than anything else, but I personally do prefer a string using the composite-syntax more concerning autocompletions, since it is supported in many IDEs already. |
Beta Was this translation helpful? Give feedback.
-
Trying to figure out how access neo4j data formatted as JSON (.Net driver). The following cypher returns JSON data. Need to capture and parse this JSON- MATCH p=(user:User {uid:"uniqueid2" })<-[r*]-(v) Trying to access data with- None of my next steps work- Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
-
In 5.5.0 we have introduced a new concept into the .net driver:
ExecutableQuery
. This is a simplified API for running queries against a database without needing to get into the detail of sessions and transactions. The new API is in theNeo4j.Driver.Experimental
namespace, and as such is subject to future changes.An example of very basic use is as follows:
This runs the cypher query and return an
EagerResult
object. This object contains the result summary, the list of keys present in the result, and the result itself (in theResult
property). Specifically, this will return anEagerResult<IReadOnlyList<IRecord>>
, as in, theResult
property will contain a read-only list ofIRecord
objects.To use a parameterised query is also easy:
An alternative syntax for setting the parameters would be:
It is possible to have the
EagerResult
object contain anything you like by using the.WithStreamProcessor
method. This method allows you to specify a lambda function that will be called, receiving anIAsyncEnumerable<IRecord>
as its parameter, which represents the stream of records as they are received from the database. The value you return from this method will be in theResult
property of theEagerResult
object returned from executing the query.For example, in this code, the lambda function uses methods in the
System.Linq.Async
package to only take 5 of the records, to convert each one to an anonymously-typed object, and then to turn them into a list of these objects. AnEagerResult<List<{string name, int born}>>
is returned from this method, and so it is possible to iterate over the list in theResult
property and access the objects.It is also possible to use
await foreach
in this lambda function, for example like this:In this instance a list of strings is returned, so the result of execution the query would be of type
EagerResult<List<string>>
.It is possible to return a scalar value from this lambda, for example:
In this case an
EagerResult<double>
is returned. Using this system it is possible to process all the records using the simplified API, without having to load them all into memory simultaneously.To configure the query execution further, you can use the
.WithConfig
method to pass aQueryConfig
object. For example to run the query while impersonating another user, you could do this:As you can see, this cuts down on the amount of boilerplate and complex coding needed to query the database.
A lot happens under the hood:
Moreover, it tracks bookmarks through a default
BookmarkManager
implementation so that subsequentExecuteQuery
reads can read the results of formerExecuteQuery
writes.Feedback
We're definitely looking for feedback on this feature. Any thoughts you have are gratefully received. Specific questions we would like to ask:
Keys
property needed in theEagerResult
?ExecuteAsync
that just returns the result of the query, rather than wrapping it in anEagerResult
, be valuable?Beta Was this translation helpful? Give feedback.
All reactions