Skip to content

Commit

Permalink
InputValue & OutputType Conversion protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Sep 21, 2021
1 parent b91ac93 commit 8d96c9c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Apollo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
DECD4992262F841600924527 /* ApolloCodegenTestSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DECD490B262F81BF00924527 /* ApolloCodegenTestSupport.framework */; };
DECD49C9262F88FE00924527 /* ApolloCodegenLib.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9B7B6F47233C26D100F32205 /* ApolloCodegenLib.framework */; };
DECD49DB262F8AAA00924527 /* ApolloTestSupport.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9F8A95781EC0FC1200304A2D /* ApolloTestSupport.framework */; };
DECD53CF26EC0EE50059A639 /* OutputTypeConvertible.swift in Sources */ = {isa = PBXBuildFile; fileRef = DECD53CE26EC0EE50059A639 /* OutputTypeConvertible.swift */; };
DED45C2A2615319E0086EF63 /* DefaultInterceptorProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DED45C292615319E0086EF63 /* DefaultInterceptorProviderTests.swift */; };
DED45D73261675890086EF63 /* StarWarsServerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FA6ABC91EC0A9F7000017BE /* StarWarsServerTests.swift */; };
DED45D852616759C0086EF63 /* TestConfigs.swift in Sources */ = {isa = PBXBuildFile; fileRef = DED45D842616759C0086EF63 /* TestConfigs.swift */; };
Expand Down Expand Up @@ -874,6 +875,7 @@
DECD492F262F820500924527 /* Apollo-Target-CodegenTestSupport.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Apollo-Target-CodegenTestSupport.xcconfig"; sourceTree = "<group>"; };
DECD4930262F824B00924527 /* Workspace-Target-TestSupport.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Workspace-Target-TestSupport.xcconfig"; sourceTree = "<group>"; };
DECD493E262F82D600924527 /* Workspace-Target-Codegen.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = "Workspace-Target-Codegen.xcconfig"; sourceTree = "<group>"; };
DECD53CE26EC0EE50059A639 /* OutputTypeConvertible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OutputTypeConvertible.swift; sourceTree = "<group>"; };
DED45C172615308E0086EF63 /* TestServerURLs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestServerURLs.swift; sourceTree = "<group>"; };
DED45C292615319E0086EF63 /* DefaultInterceptorProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DefaultInterceptorProviderTests.swift; sourceTree = "<group>"; };
DED45C3B26165DD70086EF63 /* Apollo-IntegrationTestPlan.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = "Apollo-IntegrationTestPlan.xctestplan"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1798,6 +1800,7 @@
DE3C7B10260A6FC900D2F4FF /* SelectionSet.swift */,
DE2FCF1C26E806710057EA67 /* SchemaConfiguration.swift */,
DE664ED326602AF60054DB4F /* Selection.swift */,
DECD53CE26EC0EE50059A639 /* OutputTypeConvertible.swift */,
DE2FCF2026E807EF0057EA67 /* Cacheable.swift */,
9FC750601D2A59C300458D91 /* GraphQLOperation.swift */,
);
Expand Down Expand Up @@ -2927,6 +2930,7 @@
DE05860D266978A100265760 /* ScalarTypes.swift in Sources */,
DE9C04AF26EAAEE800EC35E7 /* CacheKey.swift in Sources */,
DE05860E266978A100265760 /* GraphQLOptional.swift in Sources */,
DECD53CF26EC0EE50059A639 /* OutputTypeConvertible.swift in Sources */,
DE2FCF2126E807EF0057EA67 /* Cacheable.swift in Sources */,
DE2FCF1F26E807CC0057EA67 /* CacheTransaction.swift in Sources */,
DE2FCF2826E8083A0057EA67 /* Interface.swift in Sources */,
Expand Down
40 changes: 40 additions & 0 deletions Sources/ApolloAPI/CodegenV1/OutputTypeConvertible.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public protocol OutputTypeConvertible {
@inlinable static var asOutputType: Selection.Field.OutputType { get }
}

extension String: OutputTypeConvertible {
public static let asOutputType: Selection.Field.OutputType = .nonNull(.scalar(String.self))
}
extension Int: OutputTypeConvertible {
public static let asOutputType: Selection.Field.OutputType = .nonNull(.scalar(Int.self))
}
extension Bool: OutputTypeConvertible {
public static let asOutputType: Selection.Field.OutputType = .nonNull(.scalar(Bool.self))
}
extension Float: OutputTypeConvertible {
public static let asOutputType: Selection.Field.OutputType = .nonNull(.scalar(Float.self))
}
extension Double: OutputTypeConvertible {
public static let asOutputType: Selection.Field.OutputType = .nonNull(.scalar(Double.self))
}

extension Optional: OutputTypeConvertible where Wrapped: OutputTypeConvertible {
@inlinable public static var asOutputType: Selection.Field.OutputType {
guard case let .nonNull(wrappedOutputType) = Wrapped.asOutputType else {
return Wrapped.asOutputType
}
return wrappedOutputType
}
}

extension Array: OutputTypeConvertible where Element: OutputTypeConvertible {
@inlinable public static var asOutputType: Selection.Field.OutputType {
.nonNull(.list(Element.asOutputType))
}
}

extension AnySelectionSet {
@inlinable public static var asOutputType: Selection.Field.OutputType {
.nonNull(.object(self))
}
}
12 changes: 11 additions & 1 deletion Sources/ApolloAPI/CodegenV1/Selection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum Selection {
public struct Arguments: ExpressibleByDictionaryLiteral {
public let arguments: InputValue

public init(dictionaryLiteral elements: (String, InputValue)...) {
@inlinable public init(dictionaryLiteral elements: (String, InputValue)...) {
arguments = .object(Dictionary(elements, uniquingKeysWith: { (_, last) in last }))
}
}
Expand Down Expand Up @@ -89,4 +89,14 @@ public enum Selection {
self.default = `default`;
}
}

// MARK: - Convenience Initializers

static public func field(
_ name: String,
alias: String? = nil,
_ type: OutputTypeConvertible.Type,
arguments: Field.Arguments? = nil) -> Selection {
return .field(.init(name, alias: alias, arguments: arguments, type: type.asOutputType))
}
}
12 changes: 10 additions & 2 deletions Sources/ApolloAPI/CodegenV1/SelectionSet.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public protocol AnySelectionSet: ResponseObject {
public protocol AnySelectionSet: ResponseObject, OutputTypeConvertible {
static var selections: [Selection] { get }

/// The GraphQL type for the `SelectionSet`.
Expand All @@ -14,7 +14,6 @@ public enum ParentType {
}

public protocol SelectionSet: AnySelectionSet {

associatedtype Schema: SchemaConfiguration
}

Expand Down Expand Up @@ -46,6 +45,15 @@ public protocol ResponseObject {

extension ResponseObject {

public init(json: JSONObject) {
self.init(data: ResponseDict(json))
}

#warning("TODO: Audit all _ prefixed things to see if they should be avaialbe using ApolloExtension.")
public func _toJSONObject() -> JSONObject {
data.data
}

/// Converts a `SelectionSet` to a `Fragment` given a generic fragment type.
///
/// - Warning: This function is not supported for use outside of generated call sites.
Expand Down
38 changes: 34 additions & 4 deletions Sources/ApolloAPI/InputValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ public indirect enum InputValue {
case none
}

// MARK: - InputValueConvertible

extension InputValue {
public init(_ value: InputValueConvertible) {
self = value.asInputValue
}
}

public protocol InputValueConvertible {
@inlinable var asInputValue: InputValue { get }
}

extension Array: InputValueConvertible where Element: InputValueConvertible {
@inlinable public var asInputValue: InputValue { .list(self.map{ $0.asInputValue })}
}

extension Dictionary: InputValueConvertible where Key == String, Value: InputValueConvertible {
@inlinable public var asInputValue: InputValue { .object(self.mapValues { $0.asInputValue })}
}

// MARK: - Expressible as literals
extension InputValue: ExpressibleByNilLiteral {
@inlinable public init(nilLiteral: ()) {
self = .none
Expand Down Expand Up @@ -58,13 +79,22 @@ extension InputValue: ExpressibleByBooleanLiteral {
}

extension InputValue: ExpressibleByArrayLiteral {
@inlinable public init(arrayLiteral elements: InputValue...) {
self = .list(Array(elements))
@inlinable public init(arrayLiteral elements: InputValueConvertible...) {
self = .list(Array(elements.map { $0.asInputValue }))
}
}

extension InputValue: ExpressibleByDictionaryLiteral {
public init(dictionaryLiteral elements: (String, InputValue)...) {
self = .object(Dictionary(elements, uniquingKeysWith: { (_, last) in last }))
@inlinable public init(dictionaryLiteral elements: (String, InputValueConvertible)...) {
self = .object(Dictionary(elements.map{ ($0.0, $0.1.asInputValue) },
uniquingKeysWith: { (_, last) in last }))
}
}

// MARK = Variable Dictionary Conversion

extension Dictionary where Key == String, Value == InputValueConvertible {
@inlinable public func toInputVariables() -> [String: InputValue] {
mapValues { $0.asInputValue }
}
}
4 changes: 3 additions & 1 deletion Sources/ApolloAPI/ScalarTypes.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public protocol ScalarType: JSONDecodable, Cacheable {}
public protocol ScalarType: JSONDecodable, Cacheable, InputValueConvertible {}

extension String: ScalarType {}
extension Int: ScalarType {}
Expand All @@ -10,6 +10,8 @@ extension ScalarType {
public static func value(with cacheData: JSONValue, in transaction: CacheTransaction) throws -> Self {
return cacheData as! Self
}

public var asInputValue: InputValue { .scalar(self) }
}

public protocol CustomScalarType: JSONDecodable, Cacheable {
Expand Down

0 comments on commit 8d96c9c

Please sign in to comment.