Theo 4.0 Release: The Swift Driver for Neo4j
Theo 4.0 Release: The Swift Driver for Neo4j
Today, we take an in-depth look at the latest Neo4j Swift driver, Theo, how it works, and what it brings to the table.
Join the DZone community and get the full member experience.Join For Free
Last week, I wrote about Graph Gopher, the Neo4j client for iPhone. I mentioned that it was built alongside version 4.0 of Theo, the Swift language driver for Neo4j. Today, we'll explore the Theo 4.0 update in more detail.
But before we dive into the Theo update, let's have a look at what Theo looks like with a few common code examples:
Instantiating TheoCreating a Node and Getting the Newly Created Node Back, Complete With Error Handling
Looking Up a Node by ID, Including Error Handling and Handling if the Node Was Not FoundPerforming a Cypher Query and Getting the ResultsPerforming a Cypher Query Multiple Times With Different Parameters as Part of a Transaction, Then Rolling It Back
As you can see, it is very much in line with how you would expect Swift code to read, and it integrates with Neo4j very much how you would expect a Neo4j integration to be. So no hard learning curves, meaning you can start being productive right away.
What's New in Theo 4.0
Now for the update story:
Theo 4.0 had a few goals:
- Make a results-oriented API
- Support Swift 4.0
- Remove REST support
Theo 3.1 was our first version to support Bolt, and while it has matured since then, it turned out to be very stable, memory-efficient and fast right out of the gate.
We learned from using Theo 3 that a completion-block-based API that could throw exceptions, while easy to reason about, could be rather verbose, especially for doing many tasks in a transaction. For version 4, we explored - and ultimately decided upon - a Result type-based API.
That means that a request would still include a completion block, but it would be called with a Result type that would contain either the values successfully queried for, or an error describing the failure.
Theo 3 having a throwing function with a regular completion block.Theo 4, same example, but now with a Result type in the completion block instead.
This allowed us to add parsing that matched each query directly, and thus the code using the driver could delete the result parsing. For our example project, Theo-example, the result was a lot of less code. That means less code to debug and maintain.
Theo-example connection screen.Theo-example main screen.
Theo 3.2 added Swift 4 support, in addition to Swift 3. In Theo 4, the main purpose of this release - other than to incorporate the improvements done on the Bolt implementation - was that Theo 4 would remove the REST client that by 3.2 was marked as deprecated.
Having Theo 3.2 compatible with Swift 4 meant that projects using the REST client could use this as a target for a while going forward, giving them plenty of time to update. We committed to keeping this branch alive until Swift 5 arrived.
The main reason to remove the REST client was that the legacy Cypher HTTP endpoint it was using has been deprecated. This was the endpoint Theo 1 had been built around. Bolt is the preferred way for drivers, and hence it made little sense to adapt the REST client to the transactional Cypher HTTP endpoint that succeeds the legacy Cypher HTTP endpoint.
The result of these changes is an API that is really powerful, yet easy to use. The developer feedback we've gotten so far has been very positive. Theo 4 was in beta for a very long time and is now mature enough that we use it in our own products, such as Graph Gopher.
Going forward with Theo 4, the main plan is bugfixes, ensure support for new Neo4j versions, and minor improvements based on community input.
Looking Forward to Theo 5.0
The next exciting part will be Theo 5, which will start taking shape when Swift 5 is nearing ready.
The next major API change will be when Swift updates its concurrency model so that the API will stay close to the recommended Swift style. Specifically, we are hoping that Swift 5 will bring an async-await style concurrency model that we would then adapt to Theo. But it may very well be that this will have to wait until later Swift versions.
Other Ways to Connect to Neo4j Using the Swift Programming Language
If you think Theo is holding your hands too much, you can use Bolt directly through the Bolt-Swift project. The API is fairly straightforward to use, and hey, if you need an example project you can always browse the Theo source code.
Another interesting project to come out of Theo and Bolt support is PackStream-Swift. PackStream is the format that Bolt uses to serialize objects, in a way similar to the more popular MessagePack protocol. So if you simply need a way to archive your data or communicate them across another protocol than Bolt, perhaps PackStream will fit your needs.
Give Us Your Feedback!
If you find issues, we'd love a pull request with a proposed solution. But even if you do not have a solution, please file an issue on the GitHub page.
We hope you enjoy using Theo 4.0!
Published at DZone with permission of Bryce Merkl Sasaki , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.