Apollo-ios: How to assign nil to a graphQL property

Created on 2 Jul 2019  路  9Comments  路  Source: apollographql/apollo-ios

Hi All.
I got the schema file from backend and in the API.swift (Auto-generated) I had this property

public var pictureUrl: Swift.Optional<String?> {
    get {
      return graphQLMap["pictureUrl"] as! Swift.Optional<String?>
    }
    set {
      graphQLMap.updateValue(newValue, forKey: "pictureUrl")
    }
  }

How I can assign that pictureUrl with nil?

crash question

Most helpful comment

It slightly depends on what you're trying to do, but the short answer is that if you want the value of pictureURL to be sent as null, you need to set it as .some(nil).

Essentially with the double-optional, we're saying "Is this missing here, or is the value nil?". If you set a double optional directly to nil, it thinks the value simply was not provided and won't send it to the server. If you set it to .some(nil) you're telling it explicitly that it needs to send a value of null to the server.

All 9 comments

It slightly depends on what you're trying to do, but the short answer is that if you want the value of pictureURL to be sent as null, you need to set it as .some(nil).

Essentially with the double-optional, we're saying "Is this missing here, or is the value nil?". If you set a double optional directly to nil, it thinks the value simply was not provided and won't send it to the server. If you set it to .some(nil) you're telling it explicitly that it needs to send a value of null to the server.

Thank you for your answer.
I wanted to pass the nil value for pictureUrl and pass it to the server again.

I tried with your way
_If you set it to .some(nil) you're telling it explicitly that it needs to send a value of null to the server._

but It didn't work. I got this issue
Could not cast value of type 'Swift.Optional<Swift.String>' (0x105f2c488) to 'Swift.String' (0x105ef6190).

Hm, where are you seeing that latter issue? It'd help if I could have a little context of what's happening as well. It seems like the runtime is expecting a String somewhere it should be expecting an Optional<String>, and the compiler's not catching it.

If I drop the schema.json file. Could you run a new project with it and see what I mean?

Yeah if you can send the schema.json and the query that's not working, that'd be awesome. You can upload here or email ellen at apollographql dot com

@Vuong-Dao Can you send along your schema file? Otherwise I'll need to close this out.

Sorry for lately in response @designatednerd
You can download this schema and here is the query

query getAllContents($languages: [Language!]) { contents(languages: $languages) { edges { node { id pictureUrl } } } }

Hey, apologies for the delay in getting back to you.

I'm not quite sure how this is related, but from the schema it looks like contents and contentSubjects are two different queries, and only contentSubjects actually takes a languages argument. Does this issue go away if you switch to using contentSubjects instead of contents within your query?

Since I haven't heard back on this in a couple weeks and we've shipped 0.14.0 which gets rid of some crashes around double-optionals, I'm going to go ahead and close this out. @Vuong-Dao If you're still having problems, feel free to reopen. Anyone else, please open a new issue. Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hiteshborse12 picture hiteshborse12  路  4Comments

vishal-p picture vishal-p  路  4Comments

jeromeDms picture jeromeDms  路  5Comments

maxsz picture maxsz  路  4Comments

MrAlek picture MrAlek  路  3Comments