Giter VIP home page Giter VIP logo

Comments (5)

calvincestari avatar calvincestari commented on May 25, 2024 1

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.

PareshPatel721 avatar PareshPatel721 commented on May 25, 2024 1

@calvincestari
That is working
It saves my million of time
Thank you for your time and effort

from apollo-ios.

calvincestari avatar calvincestari commented on May 25, 2024

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.

github-actions avatar github-actions commented on May 25, 2024

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.

PareshPatel721 avatar PareshPatel721 commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.