Giter Site home page Giter Site logo

Comments (11)

rickycpadilla avatar rickycpadilla commented on July 4, 2024 31

@VigneshJeyarajG I was able to solve this in a couple steps:

  1. Add --passthroughCustomScalars to the build phase like below (see last line):
APOLLO_FRAMEWORK_PATH="$(eval find $FRAMEWORK_SEARCH_PATHS -name "Apollo.framework" -maxdepth 1)"

if [ -z "$APOLLO_FRAMEWORK_PATH" ]; then
echo "error: Couldn't find Apollo.framework in FRAMEWORK_SEARCH_PATHS; make sure to add the framework to your project."
exit 1
fi

cd "${SRCROOT}/${TARGET_NAME}"
$APOLLO_FRAMEWORK_PATH/check-and-run-apollo-cli.sh codegen:generate --queries="$(find . -name '*.graphql')" --passthroughCustomScalars --schema=schema.json API.swift
  1. Add the typealias like @martijnwalraven mentioned a while back, like:
public typealias CurrentMissionChallenge = [String : Any?]

extension Dictionary: JSONDecodable {
  /// Custom `init` extension so Apollo can decode custom scalar type `CurrentMissionChallenge `
  public init(jsonValue value: JSONValue) throws {
    guard let dictionary = value as? Dictionary else {
      throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
    }
    self = dictionary
  }
}

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 4, 2024 1

Unfortunately, we don't have real support for custom scalars yet. But I think we should be able to get something working using the --passthrough-custom-scalars workaround.

I haven't tested it, but something like this might be a good starting point:

typealias JSON = [String : Any?]

extension Dictionary: JSONDecodable {
  public init(jsonValue value: JSONValue) throws {
    guard let dictionary = value as? Dictionary else {
      throw JSONDecodingError.couldNotConvert(value: value, to: Dictionary.self)
    }
    
    self = dictionary
  }
}

from apollo-ios.

ayman-karram avatar ayman-karram commented on July 4, 2024

I am using the same solution but I am trying to use my custom object class instade of Extenstion
public class Location : JSONDecoder {

var lat : String
var lng : String

public init(jsonValue value: JSONValue) throws {
    guard let locationDic = value as? NSDictionary else {
        throw JSONDecodingError.couldNotConvert(value: value, to: String.self)
    }
    guard let lat = locationDic.value(forKey: "lat") else {
        throw JSONDecodingError.couldNotConvert(value: value, to: Int64.self)
    }
    guard let lng = locationDic.value(forKey: "lng") else {
        throw JSONDecodingError.couldNotConvert(value: value, to: Int64.self)
    }
    
    self.lat = (lat as? String)!
    self.lng = (lng as? String)!
}}

but I am getting this error in API.swift file

Cannot convert value of type 'Json.Type' (aka 'Location.Type') to expected argument type 'JSONDecodable.Type'

public struct AllPointOfInterest: GraphQLSelectionSet {
  public static let possibleTypes = ["PointOfInterest"]

  public static let selections: [GraphQLSelection] = [
    GraphQLField("__typename", type: .nonNull(.scalar(String.self))),
    GraphQLField("name", type: .nonNull(.scalar(String.self))),
    GraphQLField("type", type: .nonNull(.list(.nonNull(.scalar(PointOfInterestType.self))))),
    GraphQLField("location", type: .nonNull(.scalar(Json.self))), // error in this line 
  ]

Any thing is missing ?

from apollo-ios.

ankushkushwaha avatar ankushkushwaha commented on July 4, 2024

Any update on this? I have a field which is the type of scalar JSON. Apollo throws the error

Optional(Apollo.GraphQLResultError(path:` ["stage", "0", "parent", "0", "venue"], underlying: Apollo.JSONDecodingError.couldNotConvert(value: {
    city = Monza;
    coordinates = "45.615899,9.279499";
    country = Italia;
    "country_code" = ITA;
    "curves_left" = 4;
    "curves_right" = 7;
    debut = 1950;
    id = "sr:venue:1024";
    laps = 53;
    length = 5793;
    name = "Autodromo Nazionale Monza";
    "url_official" = "http://www.monzanet.it/";
}, to: Swift.String)))

from apollo-ios.

VigneshJeyarajG avatar VigneshJeyarajG commented on July 4, 2024

I got same Error. But i know the reason of error. Our Schema.json and mutation or query generate the API.swift. Schema.json have a JSON response. This response is treated as String. So Api.swift create [String] type. Please check your API.swift. your response is [JSON] type. But your API.swift file automatically generate the [String] for your [JSON]. During fetching the data from endpoint response [JSON]. So [JSON] couldNotConvert String error will be Occurred.

Solution is during create a Backend using MUTATION RESPONSE instead of [JSON] type Response.

from apollo-ios.

VigneshJeyarajG avatar VigneshJeyarajG commented on July 4, 2024

This is sample response for [JSON].
https://i.stack.imgur.com/zxXiz.png
My sample response is CurrentMissionChallenge is [JSON] type

from apollo-ios.

designatednerd avatar designatednerd commented on July 4, 2024

Closing this out with @rickycpadilla's solution as the present recommended one.

from apollo-ios.

aroraenterprise avatar aroraenterprise commented on July 4, 2024

Hey In Swift 5...there is no JSONDecodable/JSONDecodingError. Is there anything you guys are using for this fix.

from apollo-ios.

designatednerd avatar designatednerd commented on July 4, 2024

@aroraenterprise This is a custom error, not anything thrown by the system. Can you open a new issue with details of the problem you're running into, please?

from apollo-ios.

abhilash-linkites avatar abhilash-linkites commented on July 4, 2024

Cannot find 'JSON' in scope

from apollo-ios.

PareshPatel721 avatar PareshPatel721 commented on July 4, 2024

I'm using Apollo 1.9.3
So there are no run script needed to be set in build phase
I'm not able to set passThroughCustomScalars in run script as it is not needed while using local package.

Can anyone please help me to solve
ApolloAPI.JSONDecodingError.couldNotConvert(value: AnyHashable
to: Swift.String)

public typealias JSON = String

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.