Comments (5)
Codegen is generating public typealias JSON = String
because that is the default for custom scalars in 1.0. We use String
because that is most likely the data type used to transport the data in the JSON payload and simple execution of the default generated operation model would succeed.
You are able to edit this file and the codegen engine will not overwrite it during the next code generation. So, if your custom scalar is not a String
then you must edit the code in that file and write the new code for your custom scalar. All this information can be found in our documentation on Custom Scalars.
Firstly you need to delete public typealias JSON = String
since your custom scalar is not String
.
Of the code you posted above this one looks correct but you might not need to put it inside an extension depending on how you created your schema module:
public enum JSON: CustomScalarType, Hashable {
case dictionary([String: AnyHashable])
case array([AnyHashable])
public init(_jsonValue value: JSONValue) throws {
if let dict = value as? [String: AnyHashable] {
self = .dictionary(dict)
} else if let array = value as? [AnyHashable] {
self = .array(array)
} else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSON.self)
}
}
public var _jsonValue: JSONValue {
switch self {
case
let .dictionary(json as AnyHashable),
let .array(json as AnyHashable):
return json
}
}
// You don't need to manually define `Equatable` and `Hashable` conformance the compiler will synthesize that for you.
}
from apollo-ios.
@calvincestari
That is working
It saves my million of time
Thank you for your time and effort
from apollo-ios.
Hi @PareshPatel721 - since you mention the build phase script it sounds like you're familiar with the old 0.x versions. 1.0 has changed code generation quite a bit. We have a 1.0 migration guide and there is a specific section about custom scalars.
tl;dr - you need to write the custom scalar code yourself, just like before but in a different place now.
from apollo-ios.
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.
from apollo-ios.
I've written custom scalar code, but it still not working
public typealias JSON = String (this is what i get default implementation)
In same file I've try to extend many ways
like below
public typealias JSON = String
`extension JSON: JSONDecodable {
public init(jsonValue value: JSONValue) throws {
guard let dictionary = value as? Dictionary else {
throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
}
self = dictionary
}
}`
`extension SchemaConfiguration {
public enum JSON: CustomScalarType, Hashable {
case dictionary([String: AnyHashable])
case array([AnyHashable])
public init(_jsonValue value: JSONValue) throws {
if let dict = value as? [String: AnyHashable] {
self = .dictionary(dict)
} else if let array = value as? [AnyHashable] {
self = .array(array)
} else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSON.self)
}
}
public var _jsonValue: JSONValue {
switch self {
case let .dictionary(json as AnyHashable),
let .array(json as AnyHashable):
return json
}
}
public static func == (lhs: JSON, rhs: JSON) -> Bool {
lhs._jsonValue == rhs._jsonValue
}
public func hash(into hasher: inout Hasher) {
hasher.combine(_jsonValue)
}
}
}`
`extension Schema {
public enum JSON: CustomScalarType, Hashable {
case dictionary([String: AnyHashable])
case array([AnyHashable])
public init(_jsonValue value: JSONValue) throws {
if let dict = value as? [String: AnyHashable] {
self = .dictionary(dict)
} else if let array = value as? [AnyHashable] {
self = .array(array)
} else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSON.self)
}
}
public var _jsonValue: JSONValue {
switch self {
case let .dictionary(json as AnyHashable),
let .array(json as AnyHashable):
return json
}
}
public static func == (lhs: JSON, rhs: JSON) -> Bool {
lhs._jsonValue == rhs._jsonValue
}
public func hash(into hasher: inout Hasher) {
hasher.combine(_jsonValue)
}
}
}`
`extension JSON: JSONDecodable, JSONEncodable {
public init(jsonValue value: JSONValue) throws {
let string = value as? String
if (string == nil) {
do {
let data1 = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions.prettyPrinted) // first of all convert json to the data
let convertedString = String(data: data1, encoding: String.Encoding.utf8) // the data will be converted to the string
if (convertedString == nil) {
throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
} else {
self = convertedString ?? ""
}
} catch let myJSONError {
print(myJSONError)
throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
}
} else {
self = string ?? ""
}
}
public var jsonValue: JSONValue {
return self
}
}`
` extension SchemaConfiguration {
public enum JSON: CustomScalarType, Hashable {
case dictionary([String: AnyHashable])
case array([AnyHashable])
public init(_jsonValue value: JSONValue) throws {
if let dict = value as? [String: AnyHashable] {
self = .dictionary(dict)
} else if let array = value as? [AnyHashable] {
self = .array(array)
} else {
throw JSONDecodingError.couldNotConvert(value: value, to: JSON.self)
}
}
public var _jsonValue: JSONValue {
switch self {
case let .dictionary(json as AnyHashable),
let .array(json as AnyHashable):
return json
}
}
public static func == (lhs: JSON, rhs: JSON) -> Bool {
lhs._jsonValue == rhs._jsonValue
}
public func hash(into hasher: inout Hasher) {
hasher.combine(_jsonValue)
}
}
} `
Please help me out where and how i can write custom scalar code, so my response get automatically parsed and confirmed to custom scalar
Thank you
from apollo-ios.
Related Issues (20)
- Add support for SPM `Package.resolved` version `3`
- Support multiple schemas / endpoint in one project HOT 7
- Parity with Kotlin SDK for cache chaining and optimistic cache updates HOT 2
- PrivacyInfo.xcprivacy file is invalid for Apollo and ApolloApi. HOT 7
- SchemaConfiguration should be scoped within the namespace enum for `embeddedInTarget` module type
- Investigation: Allow codegen projects to modify generated filenames
- Add support for incremental cache writes
- Add support for incremental cache reads
- Avoid File Overwriting for Unchanged Files HOT 1
- `InMemoryNormalizedCache` is a memory bomb HOT 5
- iOS 13.0 or newer error - compile fails HOT 3
- How should I continue after handleErrorAsync inside a custom ApolloErrorInterceptor? HOT 5
- Defining a CustomScalarType which is [String:Any] HOT 10
- Deprecate `legacyResponse` and prepare for partial/incremental caching HOT 2
- Custom scalar isn't being respected HOT 7
- @defer issue HOT 8
- Documentation for @import directive. HOT 9
- Add debugDescription api for Apollo generated types
- Circular reference Build Error HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-ios.