Custom Queries and Procedures
Custom Queries
Graph portal user can write custom queries (e.g, recent nodes, popular nodes etc) and use this query to build sections of web pages. The parent and child boxes used in the Graph Web application in the node detail page can be used as examples to build similar GUI components.
Parameterized Query
Custom queries are improved to add parameters. The parameters are specified in the query body by prefixing a colon (e.g., :person). You can specify multiple parameters in the query. Each of the parameter will be replaced with the value passed. So if the value needs to be under quotes, use the quotes around the parameters too (e.g., nameField = ‘:name’)
How to call parameterized query?
The custom query can be called using the following rest endpoint
<host>:<port>/graph-web/resource/graph/custom-nodes/<queryName>?param=<paramValues>
where paramValues is a comma separated list of parameters and values
e.g.,
The parameters are optional. The result of the query will be a list of list of key value pairs, as shown in the sample JSON response below:
[
{
"result": {
"entry": [
{
"key": "field1",
"value": "value for field1"
},
{
"key": "field2",
"value": "value for field2"
}
]
}
},
{
"result": {
"entry": [
{
"key": "field1",
"value": "Guest"
},
{
"key": "field2",
"value": "value for field2"
}
]
}
}
]
Procedures
Graph Portal allows user to create procedures (similar to stored procedures in databases) written in Groovy programming language. Procedure are then saved to the database, similar to custom queries. These procedures can be called using the rest service endpoint. Just like queries, the procedures support parameters.
<host>:<port>/graph-web/resource/graph/procedure/<procedureName>?param=<paramValues>
The parameters are optional and the syntax is same as that of query. The return value of a procedure is a JSON string.
APIs and variables available inside procedures
Graph portal provides following APIs for use within the procedure body written in Groovy.
setResult (Object obj)
This method will set the value of obj to an internal variable. At the end of the procedure, the value of this variable is the return value of the procedure. The value will be returned as a JSON string. If you have multiple setResult in the procedure body, only the last one in the execution path will be effective.
execSql(String sql)
This api will execute the sql and set the results of the execution to an internal variable. This will overwrite any previous setResult() call.
Example
Here is a sample procedure body that executes an sql with a parameter.
execSql("select * from Node where name = ':name'")
Custom nodes in the database
You can create any custom node type (Node types that are not inherited from the default node type) in the backend database and access the contents using a query.
Create a custom node type (Create newClassin OrientDb)
Create a query through Graph Portal (e.g., select fromMyNewClass). The query can have optional parameters.
Access the query using the rest service endpoint for queries
This query is treated just like any other query. The query results can be retrieved using a rest service call. The returned value is in an array of map.