dgs-codegen icon indicating copy to clipboard operation
dgs-codegen copied to clipboard

Customizations for @JsonTypeInfo and @JsonSubTypes in generated interfaces

Open AlexanderPraegla opened this issue 4 years ago • 4 comments

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.

AlexanderPraegla avatar Feb 17 '21 16:02 AlexanderPraegla

The scenario you describe does seem useful. We'll need to look into whether it is feasible to support. We do have the ability to map types by specifying it in a config in the gradle task. This config handles mapping schema types to Java/Kotlin classes. Might be able to leverage that for generating the @JsonTypeInfo as you mentioned.

srinivasankavitha avatar Feb 24 '21 22:02 srinivasankavitha

Sorry for the late reply, I just got around to read your reply.

I tried your suggestion and it works quite well (Thanks for that). So i added a mapping for my custom interface "Animal" with necessary @JsonTypeInfo like this:

"Animal" : "de.interhyp.dgs.example.domaingraphserviceexample.animals.Animal"

This works fine, but the only issue is, that the code generator is still generating another interface class with the name "Animal". That a little bit confusing. Is there an option to prevent this?

AlexanderPraegla avatar Mar 10 '21 12:03 AlexanderPraegla

I have a similar use case. It would be great if the type info and the sub types could be customized. Much appreciated!

predhme avatar Sep 22 '21 18:09 predhme

I see, we should be able to fix that, i.e., if given a mapped type config for something that would otherwise be generated, avoid generating that class. I think that is a bug in the current implementation.

srinivasankavitha avatar Sep 22 '21 22:09 srinivasankavitha