Exploring Graphs with Elixir by @tonyhammond: P.181 — Ch 5 > Setting Up a Graph Service > API Demo
After starting iex in the umbrella project, then importing PropertyGraph, we are told to call:
iex> default_graph = read_graph("default.cypher")
#GraphCommons.Graph<type: property, file: "...", data: "CREATE (a)-[:EX]...">
However, read_graph hasn’t been defined yet.
We need to first add the following to the PropertyGraph module:
use GraphCommons.Graph, graph_type: :property, graph_module: __MODULE__
From that read_graph call, I don’t recall creating nor seeing the “default.cypher” in the source codebase:
graph_commons/priv/storage/property/graphs/default.cypher
I believe perhaps a cypher file with the following should work:
CREATE (a)-[:EX]->(b)
Shortly later, we call:
iex> graph_info
%GraphCommons.Service.GraphInfo{
labels: [],
num_edges: 1,
num_nodes: 2,
type: :property
}
For me, this returned an undefined function error for PropertyGraph.new_query/1
So I looked at the definition of graph_info/0 in the book’s previous section on p.182
def graph_info() do
{:ok, [stats]} =
@cypher_info
|> PropertyGraph.new_query()
|> query_graph
%GraphCommons.Service.GraphInfo{
type: :property,
num_nodes: stats["nodeCount"],
num_edges: stats["relCount"],
labels: Map.keys(stats["labels"])
}
end
I noticed
1:
Although GraphCommons.Query hasn’t been formally defined, by duplicating GraphCommons.Graph to GraphCommons.Query
and changing “graph” to “query” references and checking the module’s source codebase, we should have a new_query/3 function.
However, PropertyGraph.new_query is called here with only 2 out of 3 arguments: @cypher_info and (implicitly) the query_type
Is there a missing argument for query_file ?
I couldn’t figure this out.
2:
And for graph_info/0 to return a %GraphCommons.Service.GraphInfo{} struct, I believe we need to first define the module which I didn’t see in the source codebase. So I made one here:
graph_commons/lib/graph_commons/service/graph_info.ex
Searching the source codebase for GraphInfo structs, I found the following attributes variously used, and so defined the module as follows:
defmodule GraphCommons.Service.GraphInfo do
defstruct ~w[type num_nodes num_edges labels density file]a
end