Skip to content

Commit 5840bcd

Browse files
calvincestarigh-action-runner
authored andcommitted
docs: Update custom scalar sample code (#854)
1 parent 0b2eece commit 5840bcd

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

docs/source/custom-scalars.mdx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ For example, a custom scalar `GeoPoint` that has a JSON representation of `"100.
106106
```swift title="MySchema/CustomScalars/GeoPoint.swift"
107107
import Foundation
108108

109+
@_spi(Internal) @_spi(Execution) import ApolloAPI
110+
109111
extension MySchema {
110112
public struct GeoPoint: CustomScalarType, Hashable {
111113
let x: Float
@@ -146,15 +148,19 @@ When this happens, because you don't know the type that's coming in, you can't s
146148
This happens most often with JSON, which can return either an array or a dictionary. Here's an example of how you can use an enum to allow dynamic-but-limited types to parse (with `CustomJSON` as a placeholder type name`):
147149

148150
```swift title="MySchema/CustomScalars/CustomJSON.swift"
151+
import Foundation
152+
153+
@_spi(Internal) @_spi(Execution) import ApolloAPI
154+
149155
extension MySchema {
150156
public enum CustomJSON: CustomScalarType, Hashable {
151-
case dictionary([String: AnyHashable])
152-
case array([AnyHashable])
157+
case dictionary([String: JSONValue])
158+
case array([JSONValue])
153159

154160
public init(_jsonValue value: JSONValue) throws {
155-
if let dict = value as? [String: AnyHashable] {
161+
if let dict = value as? [String: JSONValue] {
156162
self = .dictionary(dict)
157-
} else if let array = value as? [AnyHashable] {
163+
} else if let array = value as? [JSONValue] {
158164
self = .array(array)
159165
} else {
160166
throw JSONDecodingError.couldNotConvert(value: value, to: CustomJSON.self)
@@ -163,14 +169,14 @@ extension MySchema {
163169

164170
public var _jsonValue: JSONValue {
165171
switch self {
166-
case let .dictionary(json as AnyHashable),
167-
let .array(json as AnyHashable):
172+
case let .dictionary(json as JSONValue),
173+
let .array(json as JSONValue):
168174
return json
169175
}
170176
}
171177

172178
public static func == (lhs: CustomJSON, rhs: CustomJSON) -> Bool {
173-
lhs._jsonValue == rhs._jsonValue
179+
AnyHashable(lhs._jsonValue) == AnyHashable(rhs._jsonValue)
174180
}
175181

176182
public func hash(into hasher: inout Hasher) {

0 commit comments

Comments
 (0)