Querl

0.2.0

A minimal GraphQL client library for iOS
joinhandshake/Querl

What's New

0.2.0

2023-05-05T20:25:51Z

What's Changed

  • Add support for providing custom JSONDecoders by @nathreed in #2

New Contributors

Full Changelog: 0.1.2...0.2.0

Querl

CI Status Version License Platform

Querl is a minimal GraphQL client library. It aims to be agnostic as to the architecture and technology choices of your app. It can be used with any networking stack, and makes no assumptions about how your models are defined. In addition, it is as Swift-y as possible: protocol-oriented, type-safe, and chock full of generics. It has no dependencies, and comprises less than 200 lines of easily auditable code.

Querl is not a GraphQL query construction library. Rather than providing a framework or DSL to build queries at compile-time, Querl expects you to use your preferred testing tool (Paw, Postman, Graphiql, Insomnia, and so on) to generate a static .graphql file, which Querl reads from disk.

Features

Querl supports the following GraphQL features:

  • Single-model queries
  • Mutations
  • Paginated queries
  • Variables
  • Fragments
  • Type conditions
  • Directives
  • Aliasing

It supports those GraphQL features with these Swift language features:

  • Composable, extensible query protocols
  • Strongly-typed arguments and responses
  • Bring your own model types

Querl does not (yet) support the following GraphQL features:

  • Queries returning multiple top-level models
  • Subscriptions

Example

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

The code can be as simple as this:

CountryForCode.graphql

query($code: ID!) {
    country(code: $code) {
        name
    }
}

main.swift

import Querl

struct CountryForCode: Query, HasArguments {
    struct Response: Decodable {
        let country: Country?
    }
    let code: String
}

let query = CountryForCode(code: "US")
let request = URLRequest(url: graphQLEndpoint)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try JSONSerialization.data(withJSONObject: query.body)
let (data, _) = try await URLSession.shared.data(for: request)
let country = try CountryForCode.decodeResponse(data).country // "United States of America"

This pattern can be easily adapted to Alamofire, or any other networking library that performs POST requests.

Requirements

Querl requires iOS 11.0 or higher.

Installation

CocoaPods

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

pod 'Querl'

Swift Package Manager

Declare Querl as a package dependency.

.package(
    name: "Querl", 
    url: "https://github.com/joinhandshake/Querl", 
    .upToNextMinor(from: "0.1.0")),

Use Querl in target dependencies

.product(name: "Querl", package: "Querl")

Contributing

See CONTRIBUTING.md for more information. Contributions are welcome!

Code of Conduct

See CODE_OF_CONDUCT.md for more information.

Author

Joel Kin, open-source@joinhandshake.com, @foon@mastodon.social

License

Querl is available under the Apache 2.0 license. See the LICENSE file for more info.

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Thu Oct 17 2024 18:16:29 GMT-0900 (Hawaii-Aleutian Daylight Time)