-
Notifications
You must be signed in to change notification settings - Fork 40
Add Input for supporting nulifying input object #30
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.shopify.graphql.support; | ||
|
||
import java.io.Serializable; | ||
|
||
/** | ||
* Created by henrytao on 9/7/17. | ||
*/ | ||
|
||
public final class Input<T> implements Serializable { | ||
|
||
private final T value; | ||
private final boolean defined; | ||
|
||
public static <T> Input<T> value(@Nullable T value) { | ||
return new Input<>(value, true); | ||
} | ||
|
||
public static <T> Input<T> undefined() { | ||
return new Input<>(null, false); | ||
} | ||
|
||
private Input(T value, boolean defined) { | ||
this.value = value; | ||
this.defined = defined; | ||
} | ||
|
||
public T getValue() { | ||
return value; | ||
} | ||
|
||
public boolean isDefined() { | ||
return defined; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package com.shopify.graphql.support; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
@@ -166,10 +167,26 @@ public void testOptionalFieldOnInput() throws Exception { | |
} | ||
|
||
@Test | ||
public void testUnsetOptionalFieldOnInput() throws Exception { | ||
public void testOptionalFieldOnInputAsUndefined() throws Exception { | ||
String queryString = Generated.mutation(mutation -> mutation | ||
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtl(null).unsetTtl()) | ||
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtlInput(Input.<LocalDateTime>undefined())) | ||
).toString(); | ||
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42})}", queryString); | ||
} | ||
|
||
@Test | ||
public void testOptionalFieldOnInputAsExplicitNull() throws Exception { | ||
String queryString = Generated.mutation(mutation -> mutation | ||
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtlInput(Input.<LocalDateTime>value(null))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do this instead of what is done in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It tests different way to set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I don't understand is why we are adding another way to do this when we already have a way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You mean why do we have another setter like To be able to do this: Why do we have 2 of them? Because of MBSDK depends on this and if we remove existing one it means breaking changes. So we keep 2 of them for now. |
||
).toString(); | ||
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:null})}", queryString); | ||
} | ||
|
||
@Test | ||
public void testOptionalFieldOnInputAsInputValue() throws Exception { | ||
String queryString = Generated.mutation(mutation -> mutation | ||
.setInteger(new Generated.SetIntegerInput("answer", 42).setTtlInput(Input.<LocalDateTime>value(LocalDateTime.of(2017, 1, 31, 10, 9, 48)))) | ||
).toString(); | ||
assertEquals("mutation{set_integer(input:{key:\"answer\",value:42,ttl:\"2017-01-31T10:09:48\"})}", queryString); | ||
} | ||
} |
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.
Personally, I don't like final classes because it restricts what future developers can do and it makes mocking impossible
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.
Agree that in some cases it makes more sense to make them open, but not here as:
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 guess it is mostly just a value class, I just don't see the value of restricting future developers from extending this. It's not like this is a super complicated class or is super critical.
I tend to default to trusting (future) developers to do the right thing.
Anyway, I am turning into a 🚲 🏠 discussion. This works fine