[go: up one dir, main page]

DEV Community

Cover image for How to Become a GQLAlchemist?
Memgraph for Memgraph

Posted on • Originally published at memgraph.com

How to Become a GQLAlchemist?

Introduction

Greetings, future GQLAlchemists!
You are probably wondering what a GQLAlchemist is? Well lucky for you, you have come to the right place to find out. GQLAlchemists are powerful and wise users of the GQLAlchemy library in Python, and let me tell you how you can join the ranks of those elite users. As you initiation step, all you have to do is run:
pip install gqlalchemy
and you are already halfway there.

Start mixing ingredients

I hope that your initiation process went painlessly, now we can do a few experiments to test out just how powerful we truly are when using this library. But before we begin meddling with the forbidden open source knowledge, let us remember the fundamentals of alchemy, the Equivalent Exchange, you cannot create the best product without using the best ingredients, and that's why we will use Memgraph as our main ingredient for storing and manipulating anything graph-like.

If you need help starting Memgraph, check out the installation guide.
Once we have Memgraph running, let's mix together Python and GQLAlchemy:

from gqlalchemy import Memgraph

memgraph = Memgraph("127.0.0.1", 7687)
memgraph.execute_query(
    "CREATE (:Ingredient {name: 'Python'}), (:Ingredient {name: 'GQLAlchemy'}), (:Product {name: 'Memgraph'})"
)

memgraph.execute_query(
    """
    MATCH (n {name: 'Python'}), (m {name: 'GQLAlchemy'}), (p {name: 'Memgraph'})
    MERGE (n)-[:TO {version: 3.9}]->(p)
    MERGE (m)-[:TO {version: 1.02}]->(p)
    """
)
Enter fullscreen mode Exit fullscreen mode

Amazing job, GQLAlchemists! We have created three connected nodes inside Memgraph. We have two Ingredient nodes Python and GQLAlchemy, and one Product node Memgraph.

Let's try querying it now using one of the tools from GQLAlchemy, a small query builder Match:

from gqlalchemy import Match

ingredients = (
    Match()
    .node("Ingredient", variable="ingredient")
    .to()
    .node("Product", name="Memgraph")
    .execute()
)

for ingredient in ingredients:
    print(ingredient["ingredient"].properties["name"])
Enter fullscreen mode Exit fullscreen mode

Wow, GQLAlchemists! You have successfully used the query builder to easily construct a Cypher query and execute it on Memgraph. What we have done is simply fetched all elements that have a variable set, and are connected to the Memgraph node. You can easily and more intuitively construct simple queries, but for more complex ones use execute_and_fetch from Memgraph.

Since you have mastered the fundamentals of GQLAlchemy, head on to use it on your projects, play around with it, and feel free to tell us about the amazing projects that you will build with GQLAlchemy.

If you think there is something crucial that is missing or are even willing the try out your expertise in Python and graphs, check out our GitHub repository and feel free to contribute.

Conclusion

GQLAlchemy is meant for all developers who will use Memgraph or any other products that support GQL.
And just remember, a good GQLAlchemist doesn't mix nodes and edges!

Read more about GQLAlchemy on memgraph.com

Top comments (0)