Skip to content

TibberSwift is a small unofficial SPM to make simple GraphQL queries towards Tibber's GraphQL service, fully written in Swift and made for use with SwiftUI.

License

Notifications You must be signed in to change notification settings

ppeelen/TibberSwift

Repository files navigation

TibberSwift Logo

GitHub Actions Workflow Status Codecov
Swift 5.9 Swift UI MIT License
Mastodon Follow Twitter Follow

TibberSwift

TibberSwift is a Swift package written to make simple queries towards Tibber's GraphQL backend indeed simple, without a need of embedding large libraries. TibberSwift requires you to have an APIKey to function. You will write your GraphQL queries and either store them as .graphql file in your bundle, or directly add the query to the operation.

TibberSwift functions by creating an operation which in turn sets an Input as well as an Output. The Input is of Encodable type and output of Decodable. You can checkout the custom UserLoginOperation in the Example app to see how you can create an operation inside your application.

Documentation

TibberSwift comes with documentation right inside Xcode, but this documentation is also available online via https://ppeelen.github.io/TibberSwift/.

Installation

TibberSwift can be installed with the Swift Package Manager:

https://github.com/ppeelen/TibberSwift.git

Example:

dependencies: [
    .package(url: "https://github.com/ppeelen/TibberSwift.git", branch: "main"),
    ...
]

Support

You can request support here in this Github project, but there is also a facebook group where you can request support and/or show your work. Checkout the Facebook group Tibber for Developers.

Usage

TibberSwift comes with documentation right inside Xcode, however there is also an example app which shows a few examples on how to fetch data from Tibber. You will need an API key from Tibber, which can either be fetched from their developer portal or via oAuth.

This documentation will be updated over time with more examples.

Initialise TibberSwift with the API key fetched from the developer portal, or OAuth connection.

let tibberSwift = TibberSwift(apiKey: "my-api-key")

Operations

There are two types of operations you can preform. Either pre-defined ones, which exist in the TibberSwift project, or you can create your custom one.

Pre-defined operation

TibberSwift comes with two pre-defined operations, called homes and consumption. If you want to fetch the standard consumption as defined in Consumption.graphql (Which is based of the one from the API Explorer), you can just call the consumption() function from Tibber swift, like so:

private func fetchConsumption() {
    Task {
        do {
            self.consumption = try await tibberSwift.consumption()
        } catch {
            debugPrint("Could not fetch the consumption. Error: \(error.localizedDescription)")
        }
    }
}

The same goes for fetching homes.

Custom Operation

If you wish to make your own custom operation, follow these steps:

1: Create a query

GraphQL uses queries to fetch certain data. Check the API Reference to see what queries you can create and try them out using the API Explorer. Once you have a query you need to define the Input and Output for Tibber Swift.

1.2: Input

Input parameters are parameters that you send in to your query. This could be the resulution and last which are send in for Consumption. Some queries do not need any input, like fetching which user is logged in. In such case, you can use EmptyInput as your input parameter.

Your Input should conform to Encodable.

1.3: Output

The Outputis the data you want to extract from GraphQL that exists inside the viewer node. Example:

{
  "data": {
    "viewer": {
        <<< OUTPUT JSON >>>
    }
  }
}

Your Output should conform to Codable

2: Create operation

Create an extension to GraphQLOperation, where Input = your new input, and Output = your new output.

Example:

// Import the Tibber Swift project
import TibberSwift

// Create the operation
public extension GraphQLOperation where Input == EmptyInput, Output == UserLogin {
    // Create and return the operation with your query
    static func userLogin() -> Self {
        GraphQLOperation(
            input: EmptyInput(), // The input needed for your query
            // The query you created in step 1
            operationString: """
                {
                  viewer {
                    login
                  }
                }
                """
        )
    }
}

// The output you are expecting from the operation
public struct UserLogin: Codable {
    public let login: String
}

2.1: .graphql file

If you wish to seperate your query to a graphql file, you can do so and instead use GraphQLOperation(input:queryFilename:) instead, like so:

static func currentEnergyPrice() throws -> Self {
    try GraphQLOperation(input: EmptyInput(), queryFilename: "CurrentEnergyPrice")
}

3: Run the operation

Now in your code, all that is left to do is to run the operation; this is an example from the example app:

func userLoginAction() {
    userLoginLoading = true
    Task {
        do {
            let operation = GraphQLOperation.userLogin()
            self.userLogin = try await tibberSwift.customOperation(operation)
            userLoginLoading = false
        } catch {
            errorMessage = error.localizedDescription
            userLoginLoading = false
        }
    }
}

Future development

This package currently does not support subscriptions. This is something that should be added in the future.

About

TibberSwift is a small unofficial SPM to make simple GraphQL queries towards Tibber's GraphQL service, fully written in Swift and made for use with SwiftUI.

Topics

Resources

License

Stars

Watchers

Forks

Languages