Skip to content

Commit

Permalink
Make InputValue JSONEncodable & add some TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Sep 21, 2021
1 parent ed91ff8 commit ed20344
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/Apollo/GraphQLMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import ApolloAPI
#endif

#warning("TODO: Delete")
#warning("TODO: Delete?")
public typealias GraphQLMap = [String: JSONEncodable?]

fileprivate extension Dictionary where Key == String, Value == JSONEncodable? {
Expand Down
1 change: 1 addition & 0 deletions Sources/ApolloAPI/CodegenV1/GraphQLOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public protocol GraphQLOperation: AnyObject {

var queryDocument: String { get }

#warning("TODO: We need to support setting a null value AND a nil value. Considering just going back to using GraphQLMap, or else this should be [String: GraphQLOptional<InputValue>].")
var variables: [String: InputValue]? { get }

associatedtype Data: RootSelectionSet
Expand Down
15 changes: 15 additions & 0 deletions Sources/ApolloAPI/InputValue.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

#warning("TODO: It might be more performant to just use the raw values like before commit: e7a9b2c27f9d01764943f1aa7ff9d759d8762bff - We should run performance tests and try reverting and see what performance is like.")
/// Represents an input value to an argument on a `GraphQLField`'s `FieldArguments`.
///
/// - See: [GraphQLSpec - Input Values](http://spec.graphql.org/June2018/#sec-Input-Values)
Expand Down Expand Up @@ -27,6 +28,20 @@ public indirect enum InputValue {
case none
}

// MARK: - JSONEncodable

extension InputValue: JSONEncodable {
public var jsonValue: JSONValue {
switch self {
case let .scalar(value): return value
case let .variable(variableName): return "$\(variableName)"
case let .list(values): return values.map { $0.jsonValue }
case let .object(valueObject): return valueObject.mapValues { $0.jsonValue }
case .none: return NSNull()
}
}
}

// MARK: - InputValueConvertible

extension InputValue: InputValueConvertible {
Expand Down
1 change: 1 addition & 0 deletions Sources/ApolloAPI/ScalarTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ public protocol AnyScalarType {}
public protocol ScalarType:
AnyScalarType,
JSONDecodable,
JSONEncodable,
Cacheable,
InputValueConvertible {}

Expand Down

0 comments on commit ed20344

Please sign in to comment.