SwiftSparql 0.12.0

SwiftSparql 0.12.0

Maintained by BAN Jun.


CI verbgen Version License Platform

SwiftSparql includes:

  • SPARQL query generator
  • SPARQL response parser (SPARQL 1.1 Query Results JSON Format (application/sparql-results+json))
  • Swift code generator verbgen for significant verbs in an endpoint


To run the example project, clone the repo, and run pod install from the Example directory first.



SwiftSparql is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftSparql'

Swift Package Manager

Step by step:

mkdir sparql
cd sparql
swift package init --type executable

Edit generated Package.swift:

// swift-tools-version:4.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "sparql",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        .package(url: "https://github.com/banjun/SwiftSparql", from: "0.2.0"),
    targets: [
            name: "sparql",
            dependencies: ["SwiftSparql"])
swift package generate-xcodeproj
open sparql.xcodeproj

Edit main.swift & Run with macOS destination.

import Foundation
import SwiftSparql

For example:

// construct query
let q = SelectQuery(
    where: WhereClause(patterns:
            .rdfTypeIsImasIdol() // in the followings, verbs for this type can be auto-completed
            .imasType(is: "Cu")
            .foafAge(is: 17)
            .schemaName(is: Var("name"))
            .schemaHeight(is: Var("height"))
    order: [.desc(v: Var("height"))])

// capture results by Codable type
struct Idol: Codable {
    var name: String
    var height: Double

// request query to the endpoint URL
Request(endpoint: URL(string: "https://sparql.crssnky.xyz/spql/imas/query")!, select: q)
    .onSuccess { (idols: [Idol]) in // Codable type annotation for being decoded by fetch()
        idols.forEach { idol in
            print("\(idol.name) (\(idol.height)cm)")
    }.onFailure {fatalError(String(describing: $0))}


Special Thanks

[email protected] dataset & its endpoint: https://github.com/imas/imasparql


SwiftSparql is available under the MIT license. See the LICENSE file for more info.