DEV Community

Alain Airom
Alain Airom

Posted on

What I learnt from reading a book on Graph Query Language (GQL)

Feedback on Packt book Getting Started with the Graph Query Language (GQL)

Introduction

This is my feedback on an early release book Packt Publication shared with me; “Getting Started with the Graph Query Language (GQL)”.

To begin with, I didn’t have any knowledge of GQL and didn’t know such a query lanuguage existed for querying database! 🫠

This is the first edition authored by Ricky Sun, Jason Zhang, and Yuri Simione. It serves as a guide for designing, querying, and managing graph databases with GQL, which is described as the second standardized database query language after SQL. The book is intended for professionals in various roles, including developers, data analysts, and database administrators, who work with data and aim to use graph-based systems.

Essential Points

Image from the Getting Started with the Graph Query Language (GQL) book

The book provides an introductory guide to GQL, a new international standard for database query languages.

It details the evolution of database systems, explains the foundational concepts of graph theory and GQL syntax, and offers practical examples and hands-on exercises. Key topics include writing queries, managing sessions and transactions, and advanced features such as graph traversals. The document highlights the significance of GQL as a standardized, vendor-neutral language that simplifies working with graph databases. It also includes a case study on anti-fraud to illustrate real-world application and mentions the book’s publication by Packt and its status as an early review copy.

Some advanced features: the book delves into advanced topics like graph traversals, named queries, and integration with machine learning and AI. It also discusses GQL conformance, covering required capabilities, optional features, and implementation-defined elements.

The book comes with a GitHub repository related to chapters of the book.

Hereafer some examples;

/* Ch12/Ch12-create_graph.gql */
CREATE GRAPH TransactionGraph {
  NODE Account ({ name string, balance double }),
  NODE Loan ({ payment_method string, amount double, time datetime }),
  NODE Corporation ({ name string }),
  EDGE Transfer ()-[{amount double, time datetime}]->(),
  EDGE Own ()-[{}]->(),
  EDGE Related ()-[{}]->(),
  EDGE Apply ()-[{}]->()
} PARTITION BY HASH(Crc32) SHARDS [1]

Enter fullscreen mode Exit fullscreen mode
// Ch11/Ch11-beyond_GQL.gql
// list graph
SHOW GRAPH

// or 
SHOW GRAPHS


// creating a graph with extended options
CREATE GRAPH userBehaviors {
    NODE Person (:Person {name string, age int32}),
    EDGE Follows ()-[:Follows]->()
}
EDGE KEY id string
ON SHARDS [1]
PARTITION BY CRC32
COMMENT 'a graph stores user behavior'


// alter graph name
ALTER GRAPH userBehaviors RENAME TO SocialNetwork

// alter graph comment
ALTER GRAPH SocialNetwork COMMENT 'a social network.'

// list node schemas/types
SHOW NODE SCHEMA

// list edge schemas/types
SHOW EDGE SCHEMA

// add new node schemas/types
ALTER GRAPH SocialNetwork
ADD NODE {
    User ({name string, age int32}),
    Product ({name string, price float})
}

// drop node schemas/types
ALTER GRAPH SocialNetwork DROP NODE User, Product

// rename node schemas/types
ALTER NODE Person RENAME TO User

// update the comment of node schema/type
ALTER NODE User COMMENT 'user details'

// list node properties
SHOW NODE PROPERTY

// add node properties
ALTER NODE User ADD PROPERTY {gender string, email string}

// remove node properties
ALTER NODE User DROP PROPERTY gender, email

// add node properties for all node schemas/types
ALTER NODE * ADD PROPERTY {rank float}

// remove node properties for all node schemas/types
ALTER NODE * DROP PROPERTY rank

// rename property
ALTER NODE User RENAME PROPERTY name TO username

// add NOT NULL contraint to username property on User node schema/type
ALTER NODE User ADD CONSTRAINT NOT NULL ON username

// list node CONSTRAINT
SHOW NODE CONSTRAINT

// list edge CONSTRAINT
SHOW EDGE CONSTRAINT

// remove NOT NULL CONSTRAINT
ALTER NODE User DROP CONSTRAINT NOT NULL ON username

// add EDGE KEY CONSTRAINT
ALTER EDGE * ADD CONSTRAINT EDGE KEY id string

// remove EDGE KEY
ALTER EDGE * DROP CONSTRAINT EDGE KEY

// create a node string index with fixed length
CREATE INDEX name ON NODE User ( username(10) )

// create a node string index with default length
CREATE INDEX name ON NODE User ( username )

// create compound index
CREATE INDEX nameWithAge ON NODE User ( username, age )

// crete FULLTEXT index
CREATE FULLTEXT name ON NODE User ( username )

// create vector index
CREATE VECTOR INDEX factor ON NODE User (embedding) OPTIONS {
    dimensions: 128,
    similarity_function: "COSINE"
}

// list node index
SHOW NODE INDEX

// list FULLTEXT index
SHOW NODE FULLTEXT

// remove a node index
DROP NODE INDEX name

// list HDC Graphs
SHOW HDC GRAPHS

// create a HDC Graph
CREATE HDC GRAPH HDCSocialGraph ON "hdc-1"
OPTIONS {
    nodes: {
        User: "*"
    },
    edges: "*"
}

// create HDC graph by queries, /may not supported yet
CREATE HDC GRAPH HDCSocialGraph ON "hdc-1"
OPTIONS {
    queries: ["MATCH (source:User)-[edge]->(target:User) RETURN source, edge, target"]
}

// create a dynamic HDC graph
CREATE HDC GRAPH HDCSocialGraph ON "hdc-1"
OPTIONS {
    nodes: {
        User: "*"
    },
    edges: "*",
    update: "sync"
}

// list Jobs
SHOW JOB

// DROP HDC GRAPH
DROP HDC GRAPH HDCSocialGraph

// run algorithm 
CALL algo.degree.stream("HDCSocialGraph ", {
    direction: "out",
    order: "desc",
    limit: 10
}) YIELD topDegree
MATCH (n WHERE n._uuid = topDegree._uuid)
RETURN n, topDegree

// list users
SHOW USERS

// create user
CREATE USER user001 WITH PASSWORD '@passwor6'

// delete user
DROP USER user001

// rename user
ALTER USER user001 RENAME TO user1

// reset password
ALTER USER user1 SET PASSWORD 'New@Passwor5'

// grant privileges to user
GRANT ['CREATE_USER', 'DROP_USER'] TO user1

GRANT * TO user1

// revoke privileges from user
REVOKE ['CREATE_USER', 'DROP_USER'] FROM user1

// grant privileges on graph to a user
GRANT ['INSERT', 'DELETE'] ON SocialNetwork TO user1

GRANT * ON SocialNetwork TO user1

GRANT * ON * TO user1


// revoke privileges on graph from user
REVOKE * ON SocialNetwork FROM user1

// set property privileges
GRANT ['READ'] ON NODE User(name, age) TO user1

GRANT * ON NODE User * TO user1

GRANT * ON NODE * * TO user1

REVOKE ['WRITE'] ON NODE User * FROM user1

// list roles
SHOW ROLES

// create role
CREATE ROLE readOnly

// remove role
DROP ROLE readOnly

// grant privileges to role
GRANT ['SHOW_GRAPH','SHOW_USER'] TO ROLE readOnly

// grant privileges on graph to role
GRANT ['READ','SHOW_SCHEMA'] ON SocialNetwork TO ROLE readOnly

// grant property privileges to role
GRANT ['READ', 'WRITE'] ON NODE User * TO ROLE readOnly

// assign role to user
GRANT ROLE readOnly TO user1

// assign role to role
GRANT ROLE readOnly TO ROLE admin

// STOP JOB
STOP JOB 1

// DELETE JOB
DELETE JOB 1

// Truncate graph
TRUNCATE GRAPH SocialNetwork

// or
TRUNCATE SocialNetwork

// truncate all node
TRUNCATE NODE * ON SocialNetwork

// truncate a type of node
TRUNCATE NODE User ON SocialNetwork
Enter fullscreen mode Exit fullscreen mode

Conclusion

In conclusion;

  • I have no commercial ties to Packt Publishing 🤑
  • I believe this book is a valuable resource for anyone who, like me, wants to understand GQL without prior expertise.
  • However, in my personal opinion, the initial chapters that cover foundational topics could be more concise.

Top comments (1)

Collapse
 
suvrajeet profile image
Suvrajeet Banerjee

GQL discovered! 🔗