How to handle identity fields in GraphQL SPQR

352 views Asked by At

Let's say I have

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="trade_id")
    int tradeId;

When I query this from the database, I will want to get the tradeId. However, when performing a Create, obviously I won't have the tradeId, since the Database is going to generate it when I insert it.

input TradeInput {
  tradeId: Int!
...

But the schema SPQR is generating for me is setting this field to Not Null, however.

So my question is, how can prevent this field from automatically coming up as Not Null, so I won't have to send it down on Creates, but can retrieve it.

1

There are 1 answers

2
kaqqao On BEST ANSWER

An int simply can not be null. But you have a few options:

  • Turn it into an ID scalar type (using @GraphQLId) instead of an Int
  • Give it a default value via @GraphQLInputField
  • Use Integer instead of int, as Integer is nullable