Netflix / Netflix/dgs-codegen

Customizations for @JsonTypeInfo and @JsonSubTypes in generated interfaces

Open
#40 4 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
217
Forks
116
PR merge metrics
No merged PRs in 30d

Description

First of all, I really like your new GraphQL framework and this code generation plugin! It makes lots of stuff easier. I tried a few things and ran into a limitation using this code generator.

For example a schema looks like this:

interface Animal {
    name: String
    animalType: AnimalType
}

type Dog implements Animal {
    name: String
    animalType: AnimalType
    color: String
}

type Cat implements Animal {
    name: String
    animalType: AnimalType!
    eat: String
}

enum AnimalType {
    DOG
    CAT
}

The resulting interface for Animal has the following JsonTypeInfo:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "__typename"
)
@JsonSubTypes({
    @JsonSubTypes.Type(value = Dog.class, name = "Dog"),
    @JsonSubTypes.Type(value = Cat.class, name = "Cat")
})

The problem with this is, that I would like use a custom @JsonSubTypes configuration, using an existing property mapped to an enum value. It should look something like this:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.EXISTING_PROPERTY,
    property = "animalType",
    visible = true)
@JsonSubTypes({
    @JsonSubTypes.Type(value = Dog.class, name = "DOG"),
    @JsonSubTypes.Type(value = Cat.class, name = "CAT")
})

I realize that this is probably a complex issue, but I would like to know if something like this is even possible or a too specific problem?

With this the generator could be used to create a GraphQL schema for an existing service or database and create an lightweight GraphQL service ontop.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the generator creates interfaces and applies JsonTypeInfo and JsonSubTypes annotations, using the schema and annotation examples in this issue. Determine how customization could select an existing property and enum names instead of __typename, then verify generated output for the Animal, Dog, and Cat example.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
backend-api-design, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.