Comments (9)
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.
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.
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.
@martijnwalraven I guess I also have to modify my custom GraphQL query string to include __typename
everywhere?
from apollo-ios.
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.
Yes, __typename
is required.
from apollo-ios.
@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.
From your output it seems 'selected_identities' is not actually an array, but an object with a 'records' field.
from apollo-ios.
@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)
- iOS 13.0 or newer error - compile fails HOT 3
- Custom Scalar | ApolloAPI.JSONDecodingError.couldNotConvert(value: AnyHashable to: Swift.String HOT 5
- How should I continue after handleErrorAsync inside a custom ApolloErrorInterceptor? HOT 5
- Defining a CustomScalarType which is [String:Any] HOT 12
- Deprecate `legacyResponse` and prepare for partial/incremental caching HOT 2
- Custom scalar isn't being respected HOT 7
- @defer issue HOT 8
- Documentation for @import directive. HOT 9
- Add debugDescription api for Apollo generated types
- Circular reference Build Error HOT 6
- Invalid redeclaration of synthesized property '_id' HOT 7
- Split/Hybrid Caching between InMemory and SQL review and thoughts HOT 2
- ID as a custom scalar HOT 6
- Bug: Cache writes of incremental responses uses incorrect parent key (custom cache keys)
- Incorrect PR link in release notes for version 1.11.0 HOT 2
- apollo-ios-cli deprecated --schema-name HOT 2
- Confused about cache behavior HOT 2
- CLI fails to run with 1.12.0 HOT 4
- Response value getting nil HOT 2
- Version mismatch between package and CLI HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from apollo-ios.