What would be the best workflow to convert a fetch data response to a custom Class or Struct

ios Swift 5:

What im trying to do: I Already have an app that uses a core Class of : Codeable to hold data from a payload json request and caches. Im trying to fold in GraphQL since the client is using Strapi and GraphQL so I can do array requests. The .Fetch() is working fine. But I need to automate the conversion of the response data into my current Data class

The below code shows the fetch, its query, a sample of the record class, and in that class its copyFrom: function

The query gets all events

The fetch creates a new record and then does a post copyFrom() function because I found I cannot JSONSerialization the root data result given from GraphQL.
So I found I can pass the data into the class function if the type is of the generated graphQL codes struct member. I had to debugger a lot to figure out that.

I also cant parse the whole results data and assign cause the clients featuredImage.formats is of type JSON or Custom Scalar which took a long while to figure out how to read it in but thats solved. I can now encode and decode formats prop.

This example here is of one single array query, I will have single record queries and multiple, and same exact database query request values but in a different database table or called “collection type” in strapi
eg: event, events, venue, venues, tour, tours …

So to do this I will have to make multiple signature functions of copyFrom(:) and that sounded fine except I dont at the moment know how I can pass the multiple generated types that are generated ex: GettingEventsQuery, EventQuery etc… and then do optional get properties

Swift wont allow object[“prop”] or some conditional getter

Example of generated type:

I hope that makes sense, its a lot to take in but its a basic thought I guess, how to change the data results in into a Class best practice


query GettingEvents {
   events {
     id : Int
     guid : String
     featuredImage {
       id : Int
       formats : JSON

Fetch request

Network.shared.apollo.fetch(query: GettingEventsQuery() ) { result in
  let data = try? result.get().data

  if let event = data!.events?[0] {
    let record = Single()
    record.copyFrom(graphQL: event)


SingleRecord Class

class SingleRecord : Codable, Identifiable {

  func copyFrom(graphQL: GettingEventsQuery.Data.Event){
    title = graphQL.title

    // everything is nil by default
    featuredImage = FeaturedImage()
    //  featuredImage formats is json so we have to do lots more work
    do {
        if let yy = graphQL.featuredImage!.formats {
            let featuredImageJsonData = try JSONSerialization.data(withJSONObject: yy, options: .prettyPrinted)
            featuredImage!.formats = try decoder.decode(SingleRecord.ImageFormats.self, from: featuredImageJsonData)
    catch {