How to serialize GraphQLEnum JSON in swift with Apollo version 1.3

How to serialize GraphQLEnum JSON in swift with Apollo version 1.3


0

I am trying to serialize data from a GraphQL Apollo request into JSON in order to decode into my swift model. However, when trying to serialize the data with an enum it fails.

static func decodeUser(userJson:JSONObject)->UserQL?{
        let decoder = JSONDecoder()
        print(userJson)
        guard let jsonData = try? JSONSerializationFormat.serialize(value: userJson) else {
            return nil
        }
        return try? decoder.decode(UserQL.self, from: jsonData)
    }

How to serialize GraphQLEnum JSON in swift with Apollo version 1.3

How to serialize GraphQLEnum JSON in swift with Apollo version 1.3

Below is my User DTO

struct UserQL:Codable,Equatable{
    var id:String
    var username:String
    var userType: UserType
    var photoUrl:String?
    var createdAt:String?
    var bio:String?
    
    enum CodingKeys:String,CodingKey {
        case id,username,photoUrl,createdAt,bio,userType
    }

I have tried changing userType to GraphQLEnum, however, that does not do anything. I was able to find a fix by changing the userType on the type User in the schema to String! because the enum is piped across as a String, then defining the enum in swift

How to serialize GraphQLEnum JSON in swift with Apollo version 1.3


I’m hoping to be able to avoid that though, because then my schema won’t match across platforms.


Load 7 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *