UNPKG

@aws-amplify/graphql-types-generator

Version:

Generate API code or type annotations based on a GraphQL schema and statements

422 lines (349 loc) 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.appSyncCompatibilityTypesCode = void 0; exports.appSyncCompatibilityTypesCode = ` import Foundation public protocol GraphQLInputValue { } public struct GraphQLVariable { let name: String public init(_ name: String) { self.name = name } } extension GraphQLVariable: GraphQLInputValue { } extension JSONEncodable { public func evaluate(with variables: [String: JSONEncodable]?) throws -> Any { return jsonValue } } public typealias GraphQLMap = [String: JSONEncodable?] extension Dictionary where Key == String, Value == JSONEncodable? { public var withNilValuesRemoved: Dictionary<String, JSONEncodable> { var filtered = Dictionary<String, JSONEncodable>(minimumCapacity: count) for (key, value) in self { if value != nil { filtered[key] = value } } return filtered } } public protocol GraphQLMapConvertible: JSONEncodable { var graphQLMap: GraphQLMap { get } } public extension GraphQLMapConvertible { var jsonValue: Any { return graphQLMap.withNilValuesRemoved.jsonValue } } public typealias GraphQLID = String public protocol APISwiftGraphQLOperation: AnyObject { static var operationString: String { get } static var requestString: String { get } static var operationIdentifier: String? { get } var variables: GraphQLMap? { get } associatedtype Data: GraphQLSelectionSet } public extension APISwiftGraphQLOperation { static var requestString: String { return operationString } static var operationIdentifier: String? { return nil } var variables: GraphQLMap? { return nil } } public protocol GraphQLQuery: APISwiftGraphQLOperation {} public protocol GraphQLMutation: APISwiftGraphQLOperation {} public protocol GraphQLSubscription: APISwiftGraphQLOperation {} public protocol GraphQLFragment: GraphQLSelectionSet { static var possibleTypes: [String] { get } } public typealias Snapshot = [String: Any?] public protocol GraphQLSelectionSet: Decodable { static var selections: [GraphQLSelection] { get } var snapshot: Snapshot { get } init(snapshot: Snapshot) } extension GraphQLSelectionSet { public init(from decoder: Decoder) throws { if let jsonObject = try? APISwiftJSONValue(from: decoder) { let encoder = JSONEncoder() let jsonData = try encoder.encode(jsonObject) let decodedDictionary = try JSONSerialization.jsonObject(with: jsonData, options: []) as! [String: Any] let optionalDictionary = decodedDictionary.mapValues { $0 as Any? } self.init(snapshot: optionalDictionary) } else { self.init(snapshot: [:]) } } } enum APISwiftJSONValue: Codable { case array([APISwiftJSONValue]) case boolean(Bool) case number(Double) case object([String: APISwiftJSONValue]) case string(String) case null init(from decoder: Decoder) throws { let container = try decoder.singleValueContainer() if let value = try? container.decode([String: APISwiftJSONValue].self) { self = .object(value) } else if let value = try? container.decode([APISwiftJSONValue].self) { self = .array(value) } else if let value = try? container.decode(Double.self) { self = .number(value) } else if let value = try? container.decode(Bool.self) { self = .boolean(value) } else if let value = try? container.decode(String.self) { self = .string(value) } else { self = .null } } func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() switch self { case .array(let value): try container.encode(value) case .boolean(let value): try container.encode(value) case .number(let value): try container.encode(value) case .object(let value): try container.encode(value) case .string(let value): try container.encode(value) case .null: try container.encodeNil() } } } 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 } } 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 } } } 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\`; } } public typealias JSONObject = [String: Any] public protocol JSONDecodable { init(jsonValue value: Any) throws } public protocol JSONEncodable: GraphQLInputValue { var jsonValue: Any { get } } public enum JSONDecodingError: Error, LocalizedError { case missingValue case nullValue case wrongType case couldNotConvert(value: Any, to: Any.Type) public var errorDescription: String? { switch self { case .missingValue: return "Missing value" case .nullValue: return "Unexpected null value" case .wrongType: return "Wrong type" case .couldNotConvert(let value, let expectedType): return "Could not convert \\"\\(value)\\" to \\(expectedType)" } } } extension String: JSONDecodable, JSONEncodable { public init(jsonValue value: Any) throws { guard let string = value as? String else { throw JSONDecodingError.couldNotConvert(value: value, to: String.self) } self = string } public var jsonValue: Any { return self } } extension Int: JSONDecodable, JSONEncodable { public init(jsonValue value: Any) throws { guard let number = value as? NSNumber else { throw JSONDecodingError.couldNotConvert(value: value, to: Int.self) } self = number.intValue } public var jsonValue: Any { return self } } extension Float: JSONDecodable, JSONEncodable { public init(jsonValue value: Any) throws { guard let number = value as? NSNumber else { throw JSONDecodingError.couldNotConvert(value: value, to: Float.self) } self = number.floatValue } public var jsonValue: Any { return self } } extension Double: JSONDecodable, JSONEncodable { public init(jsonValue value: Any) throws { guard let number = value as? NSNumber else { throw JSONDecodingError.couldNotConvert(value: value, to: Double.self) } self = number.doubleValue } public var jsonValue: Any { return self } } extension Bool: JSONDecodable, JSONEncodable { public init(jsonValue value: Any) throws { guard let bool = value as? Bool else { throw JSONDecodingError.couldNotConvert(value: value, to: Bool.self) } self = bool } public var jsonValue: Any { return self } } extension RawRepresentable where RawValue: JSONDecodable { public init(jsonValue value: Any) throws { let rawValue = try RawValue(jsonValue: value) if let tempSelf = Self(rawValue: rawValue) { self = tempSelf } else { throw JSONDecodingError.couldNotConvert(value: value, to: Self.self) } } } extension RawRepresentable where RawValue: JSONEncodable { public var jsonValue: Any { return rawValue.jsonValue } } extension Optional where Wrapped: JSONDecodable { public init(jsonValue value: Any) throws { if value is NSNull { self = .none } else { self = .some(try Wrapped(jsonValue: value)) } } } extension Optional: JSONEncodable { public var jsonValue: Any { switch self { case .none: return NSNull() case .some(let wrapped as JSONEncodable): return wrapped.jsonValue default: fatalError("Optional is only JSONEncodable if Wrapped is") } } } extension Dictionary: JSONEncodable { public var jsonValue: Any { return jsonObject } public var jsonObject: JSONObject { var jsonObject = JSONObject(minimumCapacity: count) for (key, value) in self { if case let (key as String, value as JSONEncodable) = (key, value) { jsonObject[key] = value.jsonValue } else { fatalError("Dictionary is only JSONEncodable if Value is (and if Key is String)") } } return jsonObject } } extension Array: JSONEncodable { public var jsonValue: Any { return map() { element -> (Any) in if case let element as JSONEncodable = element { return element.jsonValue } else { fatalError("Array is only JSONEncodable if Element is") } } } } extension URL: JSONDecodable, JSONEncodable { public init(jsonValue value: Any) throws { guard let string = value as? String else { throw JSONDecodingError.couldNotConvert(value: value, to: URL.self) } self.init(string: string)! } public var jsonValue: Any { return self.absoluteString } } extension Dictionary { static func += (lhs: inout Dictionary, rhs: Dictionary) { lhs.merge(rhs) { (_, new) in new } } } `; //# sourceMappingURL=appSyncCompatibilityTypes.js.map