Skip to content
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

Partial update in resolver #141

Closed
KrifaYounes opened this issue May 20, 2018 · 6 comments
Closed

Partial update in resolver #141

KrifaYounes opened this issue May 20, 2018 · 6 comments
Labels

Comments

@KrifaYounes
Copy link

it's possible to do partial update in the resolver ?
i have a input to update with only field specified by user like this blog
thanks
https://medium.com/workflowgen/graphql-mutations-partial-updates-implementation-bff586bda989

@oliemansm
Copy link
Member

I read the article, but this feature relies on the Javascript feature that a variable can be undefined or null. Java doesn't have an equivalent of undefined. That would mean that null values would be automatically passed in for parameters that aren't provided in the mutation. How would you know when you would actually have to set them to null and when would you have to ignore them and leave the value as is? Long story short: partial updates are not supported.

@KrifaYounes
Copy link
Author

@oliemansm it's possible
show my code :
https://github.com/KrifaYounes/spring-boot-graphql/blob/master/src/main/java/com/example/demo/graphql/mutation/AuthorMutationResolver.java

@lilianchiassai
Copy link

lilianchiassai commented Dec 3, 2019

I am late to the party but it is possible to implement an Undefined class, inspired from Optional to deal with this use case (code improved from @KrifaYounes idea):

public final class Undefined<T> {

  private static Undefined<?> UNDEFINED = new Undefined();
  private boolean defined;
  private T value;

  private Undefined() {
    this.defined = false;
    this.value = null;
  }

  public static <T> Undefined<T> getInstance() {
    @SuppressWarnings("unchecked")
    Undefined<T> t = (Undefined<T>) UNDEFINED;
    return t;
  }

  public Undefined(T value) {
    this.defined = true;
    this.value = value;
  }

  public boolean isDefined() {
    return defined;
  }

  public T getValue() {
    return value;
  }
}

The schema:

input BookInput {
  title: String
}

The input:

public class BookInput {
  Undefined<String> title = Undefined.getInstance();

  public Undefined<String> getTitle() {
    return this.title;
  } 
  public void setTitle(String title) {
    this.title=new Undefined(title);
  }
}

In the resolver use:

if(input.getTitle().isDefined()) {
  book.setTitle(input.getTitle().getValue();
}

This approach removes the needs to perform several contains operations and to have the literal key hard-coded.
I also use this with spring validation: I can annotate the undefined input field with a custom annotation that can use the undefined state to perform the validation.

@DirkLachowski
Copy link

@AllirionX
Shouldn't the resolver part be:

if(input.getTitle().isDefined()) {
  book.setTitle(input.getTitle().getValue();
}

@lilianchiassai
Copy link

Indeed it should be! I updated the code snippet.

@kliakos
Copy link

kliakos commented Mar 25, 2023

I am late to the party but it is possible to implement an Undefined class, inspired from Optional to deal with this use case

Why not use Optional<T> directly? Null field value to indicate the undefined and Option.empty() to indicate the explicit null. (as suggested here)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants