Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netflix DGS with JPA

I have been playing around with the Netflix GraphQL framework DGS. Using the codegen plugin I can generate my model classes directly from the schema.

My question is how do I add persistance to these generated classes. Do I just add the annotations to the generated classes, in which case won't they be removed if the class is regenerated? Or do I create another set of classes for JPA and map between these two sets of classes?

like image 896
user3690467 Avatar asked Dec 30 '25 09:12

user3690467


1 Answers

The first question you should ask yourself is if the JPA model is really what you want to expose through your GraphQL API. For simple use cases this can be ok, but often it would be better to separate the two models.

Depending on how you answer the above question there are three options:

  1. The GraphQL and JPA model should be separated; this means you need to map the classes in code. (There are some libraries that could potentially help).

  2. The JPA model is really what you want to expose in your API. Manually create the JPA classes and use those in your data fetchers. Don't bother using codegen from the schema, as your database is now your model.

  3. As a shortcut to get to the second option above, you could do a one-time generation, copy the generated code into your source directories, and manually manage the classes from there.

Although it's a bit more work, I would typically recommend option 1.

like image 86
Paul Bakker Avatar answered Jan 03 '26 11:01

Paul Bakker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!