diff --git a/Apollo.xcodeproj/project.pbxproj b/Apollo.xcodeproj/project.pbxproj index 86e8a306de..5fc0c4b4eb 100644 --- a/Apollo.xcodeproj/project.pbxproj +++ b/Apollo.xcodeproj/project.pbxproj @@ -2012,7 +2012,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1130; - LastUpgradeCheck = 1200; + LastUpgradeCheck = 1210; ORGANIZATIONNAME = "Apollo GraphQL"; TargetAttributes = { 9B2DFBB524E1FA0D00ED3AE6 = { diff --git a/Apollo.xcodeproj/xcshareddata/xcschemes/Apollo Playground.xcscheme b/Apollo.xcodeproj/xcshareddata/xcschemes/Apollo Playground.xcscheme index ccbe345913..204cd57513 100644 --- a/Apollo.xcodeproj/xcshareddata/xcschemes/Apollo Playground.xcscheme +++ b/Apollo.xcodeproj/xcshareddata/xcschemes/Apollo Playground.xcscheme @@ -1,6 +1,6 @@ ResultMap in value.resultMap }]) + } + + public var whatTimeIsIt: WhatTimeIsIt? { + get { + return (resultMap["whatTimeIsIt"] as? ResultMap).flatMap { WhatTimeIsIt(unsafeResultMap: $0) } + } + set { + resultMap.updateValue(newValue?.resultMap, forKey: "whatTimeIsIt") + } + } + + public struct WhatTimeIsIt: GraphQLSelectionSet { + public static var selections: [GraphQLSelection] { + return [ + GraphQLField("__typename", type: .nonNull(.scalar(String.self))), + GraphQLField("date", type: .nonNull(.scalar(CustomDate.self))), + ] + } + + public private(set) var resultMap: ResultMap + + public init(unsafeResultMap: ResultMap) { + self.resultMap = unsafeResultMap + } + + public var __typename: String { + get { + return resultMap["__typename"]! as! String + } + set { + resultMap.updateValue(newValue, forKey: "__typename") + } + } + + public var date: CustomDate { + get { + return resultMap["date"]! as! CustomDate + } + set { + resultMap.updateValue(newValue, forKey: "date") + } + } + } + } +} + +/*: +Next, we'll create the raw JSON that you'd receive in return, with the `CustomDate` type being a string that has an ISO8601-formatted date. +*/ + +let json = +""" +{ + "data": { + "whatTimeIsIt": { + "__typename": "DateInfo", + "date": "2020-03-17T21:12:34Z" + } + } +} +""" + +/*: +We'll turn this into the JSON that gets used by `GraphQLResponse`: +*/ +let data = json.data(using: .utf8)! + +let toJSON = try! JSONSerializationFormat.deserialize(data: data) +let asObject = toJSON as! JSONObject + +let response: GraphQLResponse = GraphQLResponse(operation: CustomScalarDateQuery(), body: asObject) + +/*: +Next, we'll turn that response into a result and try to get the date that was parsed: +*/ +let result = try! response.parseResultFast() + +guard let parsedDate = result.data?.whatTimeIsIt?.date else { + fatalError("date did not parse correctly!") +} + +/*: + Finally, we'll use a date formatter set to UTC using the en-US locale, with short time style and long date style to print out the date. This should print out "March 17, 2020 at 9:12 PM". + */ + +let dateFormatter = DateFormatter() +dateFormatter.locale = Locale(identifier: "en-US") +dateFormatter.dateStyle = .long +dateFormatter.timeStyle = .short +dateFormatter.timeZone = TimeZone(abbreviation: "UTC")! + +print(dateFormatter.string(from: parsedDate)) + +//: [Next](@next) diff --git a/Playgrounds/ApolloMacPlayground.playground/Pages/SQLiteCache.xcplaygroundpage/Contents.swift b/Playgrounds/ApolloMacPlayground.playground/Pages/SQLiteCache.xcplaygroundpage/Contents.swift index 756df99a0e..05090c0f5d 100644 --- a/Playgrounds/ApolloMacPlayground.playground/Pages/SQLiteCache.xcplaygroundpage/Contents.swift +++ b/Playgrounds/ApolloMacPlayground.playground/Pages/SQLiteCache.xcplaygroundpage/Contents.swift @@ -64,4 +64,4 @@ PlaygroundPage.current.needsIndefiniteExecution = true //: Once "FINISHED" prints, you can open the database file at the path printed out with "File path" and examine it to see the persisted data. //: If you don't already have a SQLite file browser, you can try the free one at https://sqlitebrowser.org/ -//: [Next](@next) +//: [Custom Scalars](@next) diff --git a/Playgrounds/ApolloMacPlayground.playground/contents.xcplayground b/Playgrounds/ApolloMacPlayground.playground/contents.xcplayground index ca47cd0250..48e43fd9b7 100644 --- a/Playgrounds/ApolloMacPlayground.playground/contents.xcplayground +++ b/Playgrounds/ApolloMacPlayground.playground/contents.xcplayground @@ -5,5 +5,7 @@ + + \ No newline at end of file diff --git a/docs/source/fetching-queries.md b/docs/source/fetching-queries.md index aa235397bb..c1665dc302 100644 --- a/docs/source/fetching-queries.md +++ b/docs/source/fetching-queries.md @@ -117,6 +117,14 @@ apollo.fetch(query: HeroAndFriendsNamesQuery(episode: .empire)) { result in Because the above query won't fetch `appearsIn`, this property is not part of the returned result type and cannot be accessed here. +### Notes on working with Custom Scalars + +Custom scalars are types defined by your schema that are based on other GraphQL scalar types (such as `String` or `Int`). Without intervention, code generation will use the underlying types to generate code for the custom scalars. + +If you want to use the custom scalars within your code, you must set `passthroughCustomScalars` to true either at the command line or using Swift Scripting. + +Once you've done that, you can either create your own type locally or use a `typealias` to declare an equivilent. This is very, very frequently used with `Date` types. Please see the [Custom Scalar Playground Page](https://github.com/apollographql/apollo-ios/main/custom-scalar-playground-page/Playgrounds/ApolloMacPlayground.playground/Pages/CustomScalars.xcplaygroundpage) available within the `apollo-iOS` repo for a full example using a custom date type. + ## Specifying a cache policy [This section has moved to the Caching documentation](/caching/).