Steve
Steve

Reputation: 4701

How to handle identity fields in GraphQL SPQR

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.

Upvotes: 1

Views: 442

Answers (1)

kaqqao
kaqqao

Reputation: 15469

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

Upvotes: 1

Related Questions