Skip to content

Instantly share code, notes, and snippets.

View jsoneaday's full-sized avatar

jsoneaday

View GitHub Profile
@jsoneaday
jsoneaday / WeakOnURLSessionInUse
Created October 30, 2020 17:14
Weak on URLSession in use
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")
@jsoneaday
jsoneaday / WeakOnURLSession
Created October 30, 2020 17:01
Weak on URLSession
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 October 30, 2020 15:25
Using unowned for two objects that references each other
class Unicycle {
var wheel: Wheel?
init() {
self.wheel = Wheel(parent: self)
}
deinit {
print ("Unicycle is being deinitialized")
}
@jsoneaday
jsoneaday / GqlContent
Last active October 3, 2020 19:12
GraphQL content
//
// ContentView.swift
// SwiftUI-Gql
//
// Created by David Choi on 9/19/20.
//
import SwiftUI
struct ContentView: View {
@jsoneaday
jsoneaday / ApolloClient
Created October 3, 2020 18:35
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 October 3, 2020 18:34
View models for GraphQL result
//
// Entities.swift
// SwiftUI-Gql
//
// Created by David Choi on 9/19/20.
//
import Foundation
protocol IVehicle {
@jsoneaday
jsoneaday / SwiftUI-Query
Created October 3, 2020 18:28
GraphQL query in SwiftUI
query GetAllVehicles {
getAllVehicles {
__typename
... on Car {
id
name
wheelCount
passengerCount
}
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 October 3, 2020 17:27
Vehicle Interface Schema
import { gql } from "apollo-server-express";
const typeDefs = gql`
scalar Void
interface IVehicle {
id: ID!
name: String!
desc: String!
}
@jsoneaday
jsoneaday / App.tsx
Created October 2, 2020 15:44
Shows call to GraphQL server being done without use of any GraphQL client
import React, { useEffect, useState } from "react";
import "./App.css";
const GetCars = `
{
getCars {
id
name
passengerCount
}