Giter Site home page Giter Site logo

Comments (9)

martijnwalraven avatar martijnwalraven commented on July 23, 2024

I'd like to better understand your use case before recommending a solution. GraphQLResultReader isn't really meant to be used directly, so maybe there is a better way of doing what you want.

This native object is getting serialized using Alamofire and custom serializers. So at some point I also need to serialize the [LinkedIdentity]? array from the received json representation that Alamofire gives me.

Do you mean deserialized? Is the idea that have a blob of JSON and want to deserialize this into your own custom objects, which include a reference to [LinkedIdentity]??

Does this JSON come from a GraphQL server? If so, why can't you use a query for this? Or does it come from somewhere else?

Why does the custom object need to contain GetLinkedIdentitiesQuery.Data.Me.LinkedIdentity.Record then, instead of another custom object that isn't tied to a particular query response?

from apollo-ios.

attheodo avatar attheodo commented on July 23, 2024

Hey @martijnwalraven

yes, sorry, I mean deserialized.

Here's the scenario: We have a core view controller that uses a custom-written Album object. This is getting deserialized by Alamofire which executes the GraphQL query to fetch the JSON. Currently we cannot afford to migrate this controller to Apollo so we need to stick with our custom Album model for a while.

In the meantime, we started using Apollo for some of our new features. One of which is a managing a list of LinkedIdentity objects, which like I mentioned is a typealias for an Apollo auto-generated model.

Now currently, this Album object needs to include an array of LinkedIdentity as one of its properties. It needs to be of that type because related view controllers (created after experimenting with Apollo) expect it to be that way.

So what we're trying to achieve here is have the Album object deserialize all its current properties the traditional, Alamofire way, but deserialize its [LinkedIdentity]? array, which is an Apollo-generated model, using GraphQLResultReader or something similar.

Due to the struct chaining and significant use of fragments in LinkedIdentity, manually trying to deserialize [LinkedIdentity] using Alamofire's way will be too much work so we're thinking whether we can utilize some parts of the Apollo stack to just deserialize that particular property of the model.

Does that make sense?

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

I see. That sounds like a complicated setup :) So hopefully it is temporary. You also miss out on features like caching of course.

Like I said, GraphQLResultReader isn't really meant to be used in this way, but I think something like this should work:

let reader = GraphQLReader(rootObject: ["linkedIdentities": linkedIdentitiesArray])
let linkedIdentities: [LinkedIdentity]? = try reader.optionalList(for: Field(responseName: "linkedIdentities"))

(Instead of ["linkedIdentities": linkedIdentitiesArray], you could also just pass in the JSON object that has the linkedIdentities field.)

from apollo-ios.

attheodo avatar attheodo commented on July 23, 2024

@martijnwalraven I guess I also have to modify my custom GraphQL query string to include __typename everywhere?

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

You may also want to think about using a fragment for LinkedIdentity, because in general you don't really want other parts of your app to rely on query-specific types.

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

Yes, __typename is required.

from apollo-ios.

attheodo avatar attheodo commented on July 23, 2024

@martijnwalraven I think I am getting closer. I've modified the query appropriately but I am getting a deserialization error from Apollo: https://gist.github.com/attheodo/7ea10fbc8ebc3c2857cfc372d9981a30

 let reader = GraphQLResultReader(rootObject: representation as! JSONObject)
 let linkedIdentities: [LinkedIdentity]? = try! reader.optionalList(for: Field(responseName: "selected_identities"))

Here representation is the whole JSON response and I am querying the reader for selected_identities which is the part I am interested in.

Since you have the experience, maybe you have any idea on what's going wrong based on the error?

from apollo-ios.

martijnwalraven avatar martijnwalraven commented on July 23, 2024

From your output it seems 'selected_identities' is not actually an array, but an object with a 'records' field.

from apollo-ios.

attheodo avatar attheodo commented on July 23, 2024

@martijnwalraven right, my bad. I had to drill down into records path. But hey, it worked 🎉

You just saved me at least a days work man. Thanks so much!

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.