Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
Last active October 3, 2020 19:12
Show Gist options
  • Save jsoneaday/ee822a8b0fb26bc5419802d8198dbf42 to your computer and use it in GitHub Desktop.
Save jsoneaday/ee822a8b0fb26bc5419802d8198dbf42 to your computer and use it in GitHub Desktop.
GraphQL content
//
// ContentView.swift
// SwiftUI-Gql
//
// Created by David Choi on 9/19/20.
//
import SwiftUI
struct ContentView: View {
@ObservedObject var vehicles = ObservableVehicle()
var body: some View {
ScrollView {
VStack (spacing: 10) {
VStack {
Text("Cars").font(.system(.title))
ForEach(self.vehicles.cars) { car in
VStack {
Text("id \(car.id)").frame(width: 300)
Text("name \(car.name)")
Text("wheelCount \(car.wheelCount)")
Text("passengerCount \(car.passengerCount)")
}
.padding(.bottom, 10)
}
}
VStack {
Text("Trucks").font(.system(.title))
ForEach(self.vehicles.trucks) { tr in
VStack {
Text("id \(tr.id)").frame(width: 300)
Text("name \(tr.name)")
Text("wheelCount \(tr.wheelCount)")
Text("maxPayload \(tr.maxPayload)")
}
.padding(.bottom, 10)
}
}
VStack {
Text("Boats").font(.system(.title))
ForEach(self.vehicles.boats) { bt in
VStack {
Text("id \(bt.id)").frame(width: 300)
Text("name \(bt.name)")
Text("passengerCount \(bt.passengerCount)")
}
.padding(.bottom, 10)
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
class ObservableVehicle: ObservableObject {
@Published var cars = [Car]()
@Published var trucks = [Truck]()
@Published var boats = [Boat]()
init() {
loadData()
}
func loadData() {
GqlClient.shared.apollo.fetch(query: GetAllVehiclesQuery()) { result in
switch result {
case .success(let gqlresult):
for vehicle in gqlresult.data!.getAllVehicles! {
if let car = vehicle.asCar {
self.cars.append(Car(car.id, car.name, car.wheelCount ?? 0, car.passengerCount ?? 0))
} else if let truck = vehicle.asTruck {
self.trucks.append(Truck(truck.id, truck.name, truck.wheelCount ?? 0, truck.maxPayload ?? 0))
} else if let boat = vehicle.asBoat {
self.boats.append(Boat(boat.id, boat.name, boat.passengerCount ?? 0))
}
}
case .failure(let error):
print("Failed \(error)")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment