Check if a graph exists

We can check if a graph is stored in the catalog by looking up its name.

Syntax

Check if a graph exists in the catalog:
CALL gds.graph.exists(graphName: String) YIELD
  graphName: String,
  exists: Boolean
Table 1. Parameters
Name Type Optional Description

graphName

String

no

The name under which the graph is stored in the catalog.

Table 2. Results
Name Type Description

graphName

String

Name of the removed graph.

exists

Boolean

If the graph exists in the graph catalog.

Additionally, to the procedure, we provide a function which directly returns the exists field from the procedure.

Check if a graph exists in the catalog:
RETURN gds.graph.exists(graphName: String)::Boolean

Examples

All the examples below should be run in an empty database.

The examples use Cypher projections as the norm. Native projections will be deprecated in a future release.

In order to demonstrate the GDS Graph Exists capabilities we are going to create a small social network graph in Neo4j and project it into our graph catalog.

The following Cypher statement will create the example graph in the Neo4j database:
CREATE
  (florentin:Person { name: 'Florentin', age: 16 }),
  (adam:Person { name: 'Adam', age: 18 }),
  (veselin:Person { name: 'Veselin', age: 20 }),
  (florentin)-[:KNOWS { since: 2010 }]->(adam),
  (florentin)-[:KNOWS { since: 2018 }]->(veselin)
Project Person nodes and KNOWS relationships:
MATCH (n:Person)-[r:KNOWS]->(m:Person)
RETURN gds.graph.project('persons', n, m)

Procedure

Check if graphs exist in the catalog:
UNWIND ['persons', 'books'] AS graph
CALL gds.graph.exists(graph)
  YIELD graphName, exists
RETURN graphName, exists
Table 3. Results
graphName exists

"persons"

true

"books"

false

We can verify the projected persons graph exists while a books graph does not.

Function

As an alternative to the procedure, we can also use the corresponding function. Unlike procedures, functions can be inlined in other cypher-statements such as RETURN or WHERE.

Check if graphs exists in the catalog:
RETURN gds.graph.exists('persons') AS personsExists, gds.graph.exists('books') AS booksExists
Table 4. Results
personsExists booksExists

true

false

As before, we can verify the projected persons graph exists while a books graph does not.