-
|
I'm trying to implement a small bank for an assignment which I've chosen to implement with Quarkus and Smallrye GraphQL. However my DTO When I leave out the constructors the conversion works, but this is not really an option since the constructors are used for serialization. This is also picked up on the JSON-B docs: https://javaee.github.io/jsonb-spec/docs/user-guide.html#custom-instantiation The @Query
@Description("Returns a collection of accounts")
public @NonNull Collection<AccountDTO> getAccounts() throws IOException {
var accNrs = bank.getBank().getAccountNumbers();
var accounts = new ArrayList<AccountDTO>();
for (var accNr : accNrs) {
bank.Account acc = bank.getBank().getAccount(accNr);
accounts.add(new AccountDTO(acc.getOwner(), acc.getNumber(), acc.getBalance(), true));
}
return accounts;
}
@Query
@Description("Returns an account")
public Optional<AccountDTO> getAccount(String number) throws IOException {
var acc = bank.getBank().getAccount(number);
return Optional.of(new AccountDTO(acc.getOwner(), acc.getNumber(), acc.getBalance(), true));
}
@Mutation
@Description("Creates an account")
public AccountDTO createAccount(String owner) throws IOException {
var nr = bank.getBank().createAccount(owner);
var account = bank.getBank().getAccount(nr);
return new AccountDTO(account.getOwner(), account.getNumber(), account.getBalance(), account.isActive());
}But as soon as they are used as input types the schema generation failes: @Mutation
@Description("Transfer balance between accounts")
public Optional<String> transfer(@Source TransferDTO transferDTO) {
try {
var s = bank.addTransfer(transferDTO);
bank.executeTransfer(s);
return Optional.empty();
} catch (Exception e) {
return Optional.ofNullable(exceptionMapper(e));
}
}The records are as follows: import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.json.bind.annotation.JsonbCreator;
import jakarta.json.bind.annotation.JsonbNillable;
import jakarta.json.bind.annotation.JsonbProperty;
@RegisterForReflection
@JsonbNillable
public record AccountDTO(
@JsonProperty("owner") @JsonbProperty("owner") String owner,
@JsonProperty("number") @JsonbProperty("number") String number,
@JsonProperty("balance") @JsonbProperty("balance") double balance,
@JsonProperty("active") @JsonbProperty("active") boolean active
) {
@JsonbCreator
public AccountDTO() {
this("", "", 0.0, true);
}
@JsonbCreator
public AccountDTO(@JsonbProperty("owner") String owner) {
this(owner, "", 0.0, true);
}
@JsonbCreator
public AccountDTO(@JsonbProperty("owner") String owner, @JsonbProperty("number") String number) {
this(owner, number, 0.0, true);
}
@JsonbCreator
public AccountDTO(@JsonbProperty("owner") String owner, @JsonbProperty("number") String number, @JsonbProperty("balance") double balance) {
this(owner, number, balance, true);
}
}import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.json.bind.annotation.JsonbCreator;
import jakarta.json.bind.annotation.JsonbNillable;
import jakarta.json.bind.annotation.JsonbProperty;
@RegisterForReflection
@JsonbNillable
public record TransferDTO(
@JsonProperty("transferId") @JsonbProperty("transferId") String transferId,
@JsonProperty("from") @JsonbProperty("from") AccountDTO from,
@JsonProperty("to") @JsonbProperty("to") AccountDTO to,
@JsonProperty("amount") @JsonbProperty("amount") double amount,
@JsonProperty("executed") @JsonbProperty("executed") boolean executed
) {
@JsonbCreator
public TransferDTO() {
this("", null, null, 0, false);
}
@JsonbCreator
public TransferDTO(@JsonbProperty("from") AccountDTO from, @JsonbProperty("to") AccountDTO to, @JsonbProperty("amount") double amount) {
this("", from, to, amount, false);
}
@JsonbCreator
public TransferDTO(@JsonbProperty("transferId") String transferId, @JsonbProperty("from") AccountDTO from, @JsonbProperty("to") AccountDTO to, @JsonbProperty("amount") double amount) {
this(transferId, from, to, amount, false);
}
}I've also tried to write import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.runtime.annotations.RegisterForReflection;
import jakarta.json.bind.annotation.JsonbCreator;
import jakarta.json.bind.annotation.JsonbNillable;
import jakarta.json.bind.annotation.JsonbProperty;
import org.eclipse.microprofile.graphql.Ignore;
@RegisterForReflection
@JsonbNillable
public class TransferDTO {
@JsonProperty("transferId") @JsonbProperty("transferId") String transferId;
@JsonProperty("from") @JsonbProperty("from") AccountDTO from;
@JsonProperty("to") @JsonbProperty("to") AccountDTO to;
@JsonProperty("amount") @JsonbProperty("amount") double amount;
@JsonProperty("executed") @JsonbProperty("executed") boolean executed;
@JsonbCreator
public TransferDTO() {
this.transferId = "";
this.from = null;
this.to = null;
this.amount = 0;
this.executed = false;
}
@JsonbCreator
public TransferDTO(@JsonbProperty("from") AccountDTO from, @JsonbProperty("to") AccountDTO to, @JsonbProperty("amount") double amount) {
this.transferId = "";
this.from = from;
this.to = to;
this.amount = amount;
this.executed = false;
}
@JsonbCreator
public TransferDTO(@JsonbProperty("transferId") String transferId, @JsonbProperty("from") AccountDTO from, @JsonbProperty("to") AccountDTO to, @JsonbProperty("amount") double amount) {
this.transferId = transferId;
this.from = from;
this.to = to;
this.amount = amount;
this.executed = false;
}
@JsonbCreator
public TransferDTO(@JsonbProperty("transferId") String transferId, @JsonbProperty("from") AccountDTO from, @JsonbProperty("to") AccountDTO to, @JsonbProperty("amount") double amount, @JsonbProperty("executed") boolean executed) {
this.transferId = transferId;
this.from = from;
this.to = to;
this.amount = amount;
this.executed = executed;
}
@Ignore
public String transferId() {
return transferId;
}
@Ignore
public AccountDTO from() {
return from;
}
@Ignore
public AccountDTO to() {
return to;
}
@Ignore
public double amount() {
return amount;
}
@Ignore
public boolean executed() {
return executed;
}
}How can I tell Quarkus / Smallrye to ignore the constructors or what annotations to I need for the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 9 replies
-
|
@phillip-kruger @t1 Could this be a bug? |
Beta Was this translation helpful? Give feedback.
Ok you need getters with names prefixed with
get, if they are named exactly the same as the field, they won't be recognized as getters