Baby’s First IBM Graph App Using Node.js
A Node.js library for exploring nodes and edges
IBM Graph provides a REST API that makes it easy to interact with the service on Bluemix: send requests using your favorite HTTP library and interpret the JSON response, as shown in the official Node.js code examples.
To make it even easier, Mike Elsmore contributed an unofficial, shallow wrapper library for IBM Graph that encapsulates request and response handling in a basic abstraction layer named ibm-graph-client
.
This client exposes methods to manipulate and query graphs, vertices, edges and supporting assets such as schemas and indexes. To help you get quickly acquainted with the wrapper library, I’ve created a sample application that illustrates how to create, populate and query a graph instance.
Just follow the instructions in the README:
- provision an IBM Graph instance on Bluemix
- clone the GitHub repo
- review and run the app
Learning to walk
The sample application is based on the Music Festival sample data set that’s built into the IBM Graph service’s UI. It associates attendees, bands, and venues:
Sticking with the same data set means you can check the sample query examples in IBM Graph’s UI to give you more ideas on how to traverse the graph you just created from Node.js. Here’s a simple one to get you started:
def gt = graph.traversal(); gt.V().hasLabel("attendee").has("name", "Jane Doe").out("bought_ticket").values("name");
As an aside, the sample data set here is so simple, in fact, that you could model it in any database. (It’s to help you get started, after all.) Graph databases, in general, really shine when the complexity of relations is compounded by greater size and scale—say a route-planning application for a maps app or a recommendation engine for an e-commerce site. There’s a lot to work toward with graph DBs!
First words: “command line”
To keep things simple, the sample is a stand-alone application that you run from the command prompt, passing the IBM Graph service instance credentials as parameters. A Cloud Foundry application would typically obtain credentials for a bound service instance from the VCAP_SERVICES
environment variable, by inspecting the property IBM Graph
.
{
"IBM Graph": [
{
"credentials": {
"apiURL": "https://ibmgraph-alpha.ng.bluemix.net/.../g",
"apiURI": "https://ibmgraph-alpha.ng.bluemix.net/...",
"username": "e...9",
"password": "1...8"
},
"label": "IBM Graph",
"provider": null,
"plan": "Standard",
"name": "ibm-graph-sample",
...
}
]
}
Take a look at the source code and enable DEBUG
to inspect API interactions, and you’ll be ready to build your first simple graph project.
On that note, here’s more on IBM Graph fundamentals. If you have general questions about the IBM Graph service on Bluemix, head over to stackoverflow and tag your inquiry with ibm-graph. We have a Java version of the library too.
If you liked this article, please show it by hitting the ♡. Thanks!