Discussion Forum

Python Client - commit/put schema file

Hi,
is there a way to commit a complete schema file (*.gql) to grakn within Python?
I know that I can commit single entities, attributes, roles and so on via the put_xxx methods, but I have already defined a schema file and want to have it as my single source for the schema definition.

Thanks a lot

hi @markusgl

If your schema contains one single define query, here is how it can be done:

with open('path/to/schema.gql', 'r') as schema:
    define_query = schema.read()

    with GraknClient(uri="localhost:48555") as client:
        with client.session("keyspace_name") as session:
            with session.transaction().write() as transaction:
                transaction.query(define_query)
                transaction.commit()

If your .gql file contains multiple queries, then you can either:

  • use Python’s subprocess to run the grakn console -k <keyspace_name> -f <path/to/file.gql> command, or
  • read through the .gql file and extract out the queries based on a predefined delimiter and then run them using transaction.query().
1 Like

The code snippet worked as I use a single define query, thanks a lot.
Didn’t see that the schema definition can be handled like a single query, great!