2

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?

user3690467
  • 3,049
  • 6
  • 27
  • 54

1 Answers1

3

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.

Paul Bakker
  • 1,024
  • 7
  • 9