Files
GitHawk/Pods/Apollo/Sources/Apollo/GraphQLSelectionSet.swift
Ryan Nystrom e697b13dfc Upgrade project and deps to Swift 4
Some notes about this update:

- Using apollo-codegen 0.17.0-alpha.13 for Swift 4 support
- Upgraded SnapKit, HTMLImage to versions with Swift 4 support
- Added 3.2 intermix support for local CocoaPods ([from here](https://github.com/CocoaPods/CocoaPods/issues/6791))
- Using alpha version of Apoolo iOS as well for Swift 4
2017-10-07 17:21:11 +01:00

129 lines
3.3 KiB
Swift

public typealias Snapshot = [String: Any?]
public protocol GraphQLSelectionSet {
static var selections: [GraphQLSelection] { get }
var snapshot: Snapshot { get }
init(snapshot: Snapshot)
}
public extension GraphQLSelectionSet {
init(jsonObject: JSONObject) throws {
let executor = GraphQLExecutor { object, info in
.result(.success(object[info.responseKeyForField]))
}
executor.shouldComputeCachePath = false
self = try executor.execute(selections: Self.selections, on: jsonObject, accumulator: GraphQLSelectionSetMapper<Self>()).await()
}
var jsonObject: JSONObject {
return snapshot.jsonObject
}
}
extension GraphQLSelectionSet {
public init(_ selectionSet: GraphQLSelectionSet) throws {
try self.init(jsonObject: selectionSet.jsonObject)
}
}
public protocol GraphQLSelection {
}
public struct GraphQLField: GraphQLSelection {
let name: String
let alias: String?
let arguments: [String: GraphQLInputValue]?
var responseKey: String {
return alias ?? name
}
let type: GraphQLOutputType
public init(_ name: String, alias: String? = nil, arguments: [String: GraphQLInputValue]? = nil, type: GraphQLOutputType) {
self.name = name
self.alias = alias
self.arguments = arguments
self.type = type
}
func cacheKey(with variables: [String: JSONEncodable]?) throws -> String {
if let argumentValues = try arguments?.evaluate(with: variables), !argumentValues.isEmpty {
let argumentsKey = orderIndependentKey(for: argumentValues)
return "\(name)(\(argumentsKey))"
} else {
return name
}
}
}
public indirect enum GraphQLOutputType {
case scalar(JSONDecodable.Type)
case object([GraphQLSelection])
case nonNull(GraphQLOutputType)
case list(GraphQLOutputType)
var namedType: GraphQLOutputType {
switch self {
case .nonNull(let innerType), .list(let innerType):
return innerType.namedType
case .scalar, .object:
return self
}
}
}
private func orderIndependentKey(for object: JSONObject) -> String {
return object.sorted { $0.key < $1.key }.map {
if let object = $0.value as? JSONObject {
return "[\($0.key):\(orderIndependentKey(for: object))]"
} else {
return "\($0.key):\($0.value)"
}
}.joined(separator: ",")
}
public struct GraphQLBooleanCondition: GraphQLSelection {
let variableName: String
let inverted: Bool
let selections: [GraphQLSelection]
public init(variableName: String, inverted: Bool, selections: [GraphQLSelection]) {
self.variableName = variableName
self.inverted = inverted;
self.selections = selections;
}
}
public struct GraphQLTypeCondition: GraphQLSelection {
let possibleTypes: [String]
let selections: [GraphQLSelection]
public init(possibleTypes: [String], selections: [GraphQLSelection]) {
self.possibleTypes = possibleTypes
self.selections = selections;
}
}
public struct GraphQLFragmentSpread: GraphQLSelection {
let fragment: GraphQLFragment.Type
public init(_ fragment: GraphQLFragment.Type) {
self.fragment = fragment
}
}
public struct GraphQLTypeCase: GraphQLSelection {
let variants: [String: [GraphQLSelection]]
let `default`: [GraphQLSelection]
public init(variants: [String: [GraphQLSelection]], default: [GraphQLSelection]) {
self.variants = variants
self.default = `default`;
}
}