-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow manually specifying nil on input objects #8
Conversation
@@ -56,6 +56,24 @@ class IntegrationTests: XCTestCase { | |||
XCTAssertEqual(queryString, "mutation{set_integer(input:{key:\"answer\",value:42,negate:true})}") | |||
} | |||
|
|||
func testInputObjectExplictNilConstructor() { | |||
let query = Generated.buildMutation { $0 | |||
.setInteger(input: Generated.SetIntegerInput(key: "answer", value: 42, negate: .some(nil))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs .none instead of .some
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so? I need a way to distinguish between an optional argument being set to it's default value (which is nil
) and the user trying to tell me they want a property to be nil
, hence the .some(nil)
Because it unwraps to nil, I can try to unwrap each of the optional arguments and then just use the value inside if it's there as the property's value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional.none
is an optional with nil, Optional.some(nil)
is saying that the optional has a value, but when you unwrap it is nil. That would be making it a double optional like your description Optional(Optional(value))
var a: String? = .none
print(a)
var b: String?? = ""
print(b)
// output
nil
Optional(Optional(""))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I want something that you unwrap to nil. The problem is distinguishing between: foo(optinal: nil
) and foo()
which I can't do at a language level because within foo
, optional
will have a value of nil regardless of whether the caller provided the nil explicitly or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR seems good to me.
Although adding none(nil)
explicitly to check doesn't look nice I don't see any other way to do that.
Note: There is no way to check whether the input used is the default value or is the one that you entered explicitly.
func optionalTest(thing: Int? = nil) {
if thing == nil {
if expression { //there isn't any expression to check if the param was passed per param
print("default")
} else {
print("explicit")
}
}
}
One workaround to check whether a param was passed or not is to create two different functions, one that receives thing
param and the other not. However, it's a mess when you have a lot of optional null params.
We shouldn't accept assgining nil to a nested list object. If it's a list it should be |
Do you mean that we shouldn't let a |
Yes. Why should I send a @dylanahsmith |
That's because you are thinking about mapping the data to active record associations, but we shouldn't think about our data model in that way. For instance, an association can't return a Conversely, I don't think we should actually encourage specifying I don't think it makes sense to impose arbitrary restrictions in this generic code generator. It should work with any GraphQL schema. However, if we want to add restrictions to the client for fields that shouldn't allow a |
Swift equivalent of Shopify/graphql_java_gen#28
This adds support for input objects so that you can manually set a property to nil and have that show up in the mutation sent to the server.
The weird part of this is that the argument to init are now double optional (eg:
inputArg??
). This is so that we have a way of distinguishing between a user supplying an argument and not when the value they supply could be nil.My only concern here is that in order to manually specify
nil
in a constructor/init is to use.some(nil)
(see the test). I feel like this is very non-obvious and could cause a bug in the future. I'm open to suggestions on how else to fix this.