Swiftpack.co - Saelyria/Artemis as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Saelyria.
Saelyria/Artemis 1.0.1
Write GraphQL queries like SwiftUI views - no strings, no dictionaries.
⭐️ 63
🕓 2 years ago
iOS macOS
.package(url: "https://github.com/Saelyria/Artemis.git", from: "1.0.1")

Artemis

Build status Platform iOS Swift 5.3 compatible License: MIT

Artemis is a GraphQL library for Swift that lets you interact with a GraphQL backend entirely in Swift - no unsafe queries made of strings, no Data or [String: Any] responses you need to parse though manually. Artemis uses various Swift features to keep track of types used in queries, so this request:

// Artemis                                              // Rendered GraphQL query   
.query {                                                query {   
    $0.country(arguments: .init(code: "CA")) {              country(code: "CA") {       
        $0.name                                                 name
        $0.continent {                                          continent {
            $0.name(alias: "continentName")                         continentName: name
        }                                                       }
    }                                                       }
}                                                       }

...is entirely type-checked - we can't add fields not on our schema, put fields in the wrong place, pass invalid arguments, or pass invalid types for our arguments.

Performing that query results in a Partial<Country> object that you can interact with using the same keypaths and type inference as a normal Country instance. Artemis will populate the response object with the fetched data - so this query (and its response) are handled like this:

let client = Client<Query>()

client.perform(.query { ... }) { result in
    switch result {
    case .success(let country):
        country.name // "Canada"
        country.continent?.name(alias: "continentName") // "North America"
        country.languages // nil
    case .failure(let error):
        // error handling
    }
}

The schema representation in Swift is also very readable and maintains a reasonably close resemblance to the GraphQL schema syntax. A schema for the above query might look something like this:

// Artemis                                          // Original GraphQL schema
final class Query: Object {                         type Query {
    @Field("country")                                   country(code: String!): Country!
    var country: (Country, CountryArgs.Type)        }
                                                
    struct CountryArgs: ArgumentsList {             type Country {
        var code: String                                name: String!
    }                                                   languages: [String!]!
}                                                       continent: Continent!
                                                    }
final class Country: Object {                    
    @Field("name")                                  type Continent {
    var name: String                                    name: String!
                                                    }
    @Field("languages")
    var languages: [String]

    @Field("continent")
    var continent: Continent
}

final class Continent: Object {
    @Field("name")
    var name: String
}

Don't let this simple example sell Artemis short, though - it includes full support for fragments, arguments, interfaces, inputs, aliases, mutations, and multiple query fields. It's also very light (requiring only Foundation), so supports iOS, macOS, or anywhere else Swift and Foundation can run.

Artemis can be added to any project using Swift Package Manager.

Once you have it added to a project, you can check out the tutorial on getting started with Artemis guide to get up and running with making requests.

Contributors

Aaron Bosnjak (email: [email protected], Twitter: @aaron_bosnjak)

Artemis is open to contributors! If you have a feature idea or a bug fix, feel free to open a pull request. Issues and feature ideas are tracked on this Trello board.

License

Artemis is available under the MIT license, so do pretty much anything you want with it. As always, see the LICENSE file for more info.

GitHub

link
Stars: 63
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics