Swiftpack.co -  Package - GetStream/TinyGraphQL
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
GetStream/TinyGraphQL
🌸 Simple and lightweight GraphQL query builder for the Swift language - Made with 💘 by @GetStream
.package(url: "https://github.com/GetStream/TinyGraphQL.git", from: "1.0.0")

Swift CI Status Cocoapods Cocoapods platforms SPM & Carthage compatible License Twitter Follow Twitter

TinyGraphQL is a simple and lightweight query builder for the Swift language with zero dependencies. It provides a syntax close to that of GraphQL while preventing you from making time-consuming syntax mistakes.

Made with 💘 by the folks @ Stream.

Table of Contents

Snippets

Initialization

TinyGraphQL is also a container for pre-configuring your requests. In the initialization step, you should specify the URL for the GraphQL endpoint and any HTTP headers you'd need for the requests.

let graphQL = TinyGraphQL(
    url: URL(string: "https://api.my.app/graphql")!,
    headers: ["Content-Type": "application/json", "combase-organization": "5fd7ecb251b33b10c380977b"]
)

Query

See below a comparison between a regular GraphQL query and how to generate a similar query using TinyGraphQL. Note that it's possible to have multiple levels of fields like in regular GraphQL.

Raw GraphQL

query {
    organizationById(_id: "(id)") {
        name
        stream {
            key
        }
        agentCount
    }
}

TinyGraphQL

Query("organizationById", ["_id": id]) {
    "name"
    Field("stream") {
        "key"
    }
    "agentCount"
}

Mutation

Mutations work pretty much the same as queries, except behind the scenes where it becomes a POST request instead of GET.

Raw GraphQL

mutation {
    getOrCreateUser(record: { name: "(name)", email: "(email)" }) {
        _id
        name
        streamToken
    }
}

TinyGraphQL

Mutation("getOrCreateUser", ["record": ["name": name, "email": email]]) {
    "_id"
    "name"
    "streamToken"
}

Request

Making a request without TinyGraphQL is quite tedious and error prone. With TinyGraphQL, it's safe and easy. Look below at a comparison between building the request yourself and having TinyGraphQL do it for you.

Raw GraphQL + URLSession

var urlRequest = URLRequest(url: url)
urlRequest.httpMethod = "POST"
urlRequest.setValue("5fd7ecb251b33b10c380977b", forHTTPHeaderField: "combase-organization")
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.httpBody = """
{
    "query": "mutation { createTicket(message: \\"\(message)\\", user: \\"\(userId)\\") { _id }}"
}
""".data(using: .utf8)

URLSession.shared.dataTask(with: urlRequest, completionHandler: { data, response, error in
    // handle response
}

TinyGraphQL + URLSession

let query = Query("organizationById", ["_id": id]) {
    "name"
    Field("stream") {
        "key"
    }
    "agentCount"
}

let urlRequest = graphQL.request(for: query)

URLSession.shared.dataTask(with: urlRequest, completionHandler: { data, response, error in
    // handle response
}

Installation

TinyGraphQL supports all three major dependency managers (SPM, CocoaPods, and Carthage)

SPM

.package(name: "TinyGraphQL", url: "https://github.com/getstream/TinyGraphQL", from: "0.0.2")

CocoaPods

pod 'TinyGraphQL', '~> 1.0'

Carthage

github "getstream/TinyGraphQL" ~> 1.0

Adopters

  • Combase: Combase is an Open Source white-label chat solution that provides everything you need to instantly spin up a powerful, real-time customer support dashboard. TinyGraphQL is used by the Combase Swift SDK.

If you use TinyGraphQL, you can show your support by opening a PR and including your project or company in this list!

GitHub

link
Stars: 69
Last commit: 5 days ago

Release Notes

First release (1.0.0)
1 week ago

Initial release with basic functionality

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