Skip to content

Instantly share code, notes, and snippets.

@jsoneaday
jsoneaday / WeakOnURLSessionInUse
Created Oct 30, 2020
Weak on URLSession in use
View WeakOnURLSessionInUse
import Foundation
class MyApiCaller {
var gotData: Bool = false
func getTodos() -> URLSessionDataTask {
let url = URL(string: "https://jsonplaceholder.typicode.com/todos")
let result: URLSessionDataTask = URLSession.shared.dataTask(with: url!) { [weak self] (data, resp, err) in
guard let todos = data else {
print("No data")
View WeakOnURLSession
import Foundation
class MyApiCaller {
var gotData: Bool = false
func getTodos() -> URLSessionDataTask {
let url = URL(string: "https://jsonplaceholder.typicode.com/todos")
let result: URLSessionDataTask = URLSession.shared.dataTask(with: url!) { (data, resp, err) in
guard let todos = data else {
print("No data")
@jsoneaday
jsoneaday / RefEachOther
Last active Oct 30, 2020
Using unowned for two objects that references each other
View RefEachOther
class Unicycle {
var wheel: Wheel?
init() {
self.wheel = Wheel(parent: self)
}
deinit {
print ("Unicycle is being deinitialized")
}
@jsoneaday
jsoneaday / GqlContent
Last active Oct 3, 2020
GraphQL content
View GqlContent
//
// ContentView.swift
// SwiftUI-Gql
//
// Created by David Choi on 9/19/20.
//
import SwiftUI
struct ContentView: View {
View ApolloClient
import CoreData
import Apollo
class GqlClient {
static let shared = GqlClient()
private(set) lazy var apollo = ApolloClient(url: URL(string: "http://localhost:8000/graphql")!)
}
@jsoneaday
jsoneaday / Entities
Created Oct 3, 2020
View models for GraphQL result
View Entities
//
// Entities.swift
// SwiftUI-Gql
//
// Created by David Choi on 9/19/20.
//
import Foundation
protocol IVehicle {
@jsoneaday
jsoneaday / SwiftUI-Query
Created Oct 3, 2020
GraphQL query in SwiftUI
View SwiftUI-Query
query GetAllVehicles {
getAllVehicles {
__typename
... on Car {
id
name
wheelCount
passengerCount
}
View VehicleResolvers
import { IResolvers } from "apollo-server-express";
import { addCar, getAllVehicles, getCars } from "../dataService";
import { Boat, Car, Truck } from "../types/Entities";
import { GqlContext } from "./GqlContext";
const resolvers: IResolvers = {
SearchResult: {
__resolveType(obj: any, ctx: GqlContext, info: any) {
if (obj.wheelCount && obj.passengerCount) return "Car";
if (obj.wheelCount && obj.maxPayload) return "Truck";
@jsoneaday
jsoneaday / VehiclesSchema
Last active Oct 3, 2020
Vehicle Interface Schema
View VehiclesSchema
import { gql } from "apollo-server-express";
const typeDefs = gql`
scalar Void
interface IVehicle {
id: ID!
name: String!
desc: String!
}
@jsoneaday
jsoneaday / App.tsx
Created Oct 2, 2020
Shows call to GraphQL server being done without use of any GraphQL client
View App.tsx
import React, { useEffect, useState } from "react";
import "./App.css";
const GetCars = `
{
getCars {
id
name
passengerCount
}