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.
TibberSwift comes with documentation right inside Xcode, but this documentation is also available online via https://ppeelen.github.io/TibberSwift/.
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"),
...
]
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.
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")
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.
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.
If you wish to make your own custom operation, follow these steps:
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.
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
.
The Output
is 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
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
}
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")
}
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
}
}
}
This package currently does not support subscriptions. This is something that should be added in the future.