question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maintainable way to create multiple different Inputs

See original GitHub issue

I’m trying to figure out how to create multiple different inputs from one Java class. Suppose I have a Car class

@Data
public class Car {
    private long ID;
    private String brand;
    private String model;
}

with three mutations: addCar to add a new Car, updateCar to update a Car and deleteCar to delete a Car. Using graphql-spqr I can write something like:

@GraphQLMutation
public Car addCar(Car car) {
...
}

@GraphQLMutation
public Car updateCar(Car car) {
...
}

@GraphQLMutation
public boolean deleteCar(Car car) {
...
}

This will result more or less in the following schema:

type Mutation {
    addCar(car: CarInput) : Car
    updateCar(car: CarInput) : Car
    deleteCar(car: CarInput) : Boolean
}

type Car {
    ID: Long
    brand: String
}

type CarInput {
    ID: Long
    brand: String
}

However, the addCar mutation does not need a CarInput with an ID field, the deleteCar only requires a CarInput with an ID (it is easier to just use the ID as only input of the mutation but this is as an example). If for example the brand of the Car can only be set at initialization, the CarInput of the update should not include the brand.

I know I can create different inputs by extending the Car class and then overriding the getters and setters and adding the @GraphQLIgnore annotation, but with multiple different Inputs and more fields in the class, it quickly becomes cumbersome to create classes for each Input and overriding the getters and setters. Especially with a lot of fields in the base class. Also, changing fields in the base class takes a lot more time and is more error prone. My question is if there is an easier way to achieve multiple Inputs while keeping the java to a minimum?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
alturkoviccommented, Jan 20, 2019

I am afraid I am still not following, most likely due to lack of documentation and examples, so I am probably misusing things. This is also the first project I am trying to do with GraphQL so this is all very new to me and I might be misunderstanding basic things…

Would you mind posting a simple example of how you would reuse the model object as input (take the above Car example) without making another DTO (UpdateCar CreateCar) or listing multiple @GraphQLArgument parameters (requires copying fields from model)? Using only Car as input parameter, but requiring id field for update and ignoring id for create?

That is what I am trying to do, only one object model for all operations…

1reaction
slavapcommented, Nov 23, 2019

For similar purpose I’m using class inheritance, that way I can easily unify Query and Mutation classes. Example:

public class AuthorBase {
    @GraphQLId
    public String id;
    public String name;
    public String thumbnail;
}

public class Author extends AuthorBase {

    @GraphQLNonNull
    public String getId() {
        return id;
    }

    @GraphQLNonNull
    public String getName() {
        return name;
    }
}

@GraphQLType(name = "Author")
public class AuthorInput extends AuthorBase {
}

public class PostBase {
    @GraphQLId
    public String id;

    public String title;
    public String text;
    public String category;

    @GraphQLIgnore
    public String authorId;
}

public class Post extends PostBase {

    @GraphQLNonNull
    public String getId() {
        return id;
    }

    @GraphQLNonNull
    public String getText() {
        return text;
    }
}

@GraphQLType(name = "Post")
public class PostInput extends PostBase {
    public AuthorInput author;
}

Of course it’s possible to have additional input classes for insert/update/delete (with @GraphQLIgnore adjusted fields), but it’s a little bit overkill IMO 😃 Generates schema like this:

type Author implements Node {
  name: String!
  thumbnail: String
  id: ID!
  posts: [Post]!
}

input AuthorInput {
  name: String
  thumbnail: String
  id: ID
}

interface Node {
  id: ID!
}

type Post implements Node {
  id: ID!
  text: String!
  title: String
  category: String
  author: Author
}

input PostInput {
  author: AuthorInput
  category: String
  title: String
  text: String
  id: ID
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

5 Tips to Design Maintainable Inputs in Figma - UI Prep
4) Use Auto Layout ... Use auto layout to give your inputs multiple resizing behaviors for different amounts of content and container sizes....
Read more >
How to create maintainable and reusable Logstash pipelines
Execute a unique pipeline for processing events from each unique input source. This approach requires duplicating and copying common ...
Read more >
Multiple Output from a Single Model — Maintainable MPS ...
A pattern that is proven in use in many projects is to separate generation and the model from each other. The model contains...
Read more >
How to bind one model to multiple inputs with Angular JS
The simplest solution is to have 3 scope properties and then combine them. · I think a reusable directive that contains the three...
Read more >
Providing Multiple Constructors in Your Python Classes
In other words, you want a class that implements multiple constructors. This kind of class comes in handy when you need to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found