Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizations for @JsonTypeInfo and @JsonSubTypes in generated interfaces #40

Open
AlexanderPraegla opened this issue Feb 17, 2021 · 4 comments

Comments

@AlexanderPraegla
Copy link

AlexanderPraegla commented Feb 17, 2021

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.

@srinivasankavitha
Copy link
Contributor

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.

@AlexanderPraegla
Copy link
Author

AlexanderPraegla commented Mar 10, 2021

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?

@predhme
Copy link

predhme commented Sep 22, 2021

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

@srinivasankavitha
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants