-
Notifications
You must be signed in to change notification settings - Fork 183
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
Issue where the generated field of my output type is not the same as my POKO entity? #462
Comments
I'm not sure what names Kotlin ends up generating for the getter and setter, but it seems like it does not generate JavaBeans compliant names (which in this case would be Anyway, the simplest would be to append The bottom line is that SPQR by default expects the JavaBeans spec to be respected ( Advanced usageYou almost certainly don't need to do this, unless you're doing some deep customization, like supporting a new set of annotations, or a JVM language with strange naming conventions. But, you can always provide a custom E.g. to replace the naming logic the default generator.withNestedResolverBuilders((config, defaultBuilders) -> defaultBuilders
// Customize the built-in BeanResolverBuilder
.replace(BeanResolverBuilder.class, def -> def.withOperationInfoGenerator(new MemberOperationInfoGenerator()))) Or to provide a fully custom naming logic: private class CustomOperationInfoGenerator extends AnnotatedOperationInfoGenerator {
@Override
public String name(OperationInfoGeneratorParams params) {
// `elements` contains the field, the getter and the setter, is they exist
List<AnnotatedElement> elements = params.getElement().getElements();
// Find the field, if it exists
Optional<String> field = Utils.extractInstances(elements, Field.class).findFirst().map(Field::getName);
// Return the explicit name from the annotation, if it exists. If not, the name of field.
// If that is also missing, return null and let the default logic take over
return Utils.coalesce(super.name(params), field.orElse(null));
}
}
// Delegates to your custom impl, and falls back to the defaults if yours returns `null`
OperationInfoGenerator customNaming = new DefaultOperationInfoGenerator()
.withDelegate(new CustomOperationInfoGenerator());
generator.withNestedResolverBuilders((config, defaultBuilders) -> defaultBuilders
// Customize the built-in BeanResolverBuilder
.replace(BeanResolverBuilder.class, def -> def.withOperationInfoGenerator(customNaming))) |
Thanks for your response. I added the @GraphQLQuery annotation to the isMarried property as follows: @GraphQLType(name = "Person")
data class Person(
val name: String,
@GraphQLQuery(name = "isMarried")
var isMarried: Boolean? = null
) That did not seem to work. I am still seeing the generated GraphQL type returning the property as I will try with a CustomOperationInfoGenerator and give feedback. |
When I changed the name of the @GraphQLType(name = "Person")
data class Person(
val name: String,
@GraphQLQuery(name = "isMarried")
var married: Boolean? = null
) However, if I leave the name of the field in my data class as Could it possibly because SPQR does not detect it as a |
I think i may have figure out something. implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.8") |
Ignore that last post (although you may still want jackson-module-kotlin to solve other issues) This has not been properly tested, but it may be a starting point for you:
@kaqqao Do you have a better suggestion for the isBoolean implementation? |
Discussed in #461
Originally posted by wojcik-marcin June 5, 2023
I would like to request some guidance with an issue I am experiencing.
I am making use of the SPQR Spring Boot Starter v0.0.7 within my Kotlin Spring Boot application.
I have a Person data class defined as follows:
I have a @GraphQLMutation endpoint to update the Person
The GraphQL schema types generated are as follows:
As you can see, the input type generates the property name for
isMarried
correctly, however, I expected the same for the Person type that is being returned as the output type. Instead, it is returning asmarried
.I am using Jackson as my JSON serializer, and adding
@JsonProperty("isMarried")
to the field in the data class doesn't seem to work.How can I enforce that the output type's
isMarried
property generates correctly to be the same as that of what is defined in the Person data class?Any assistance in this matter will be greatly appreciated.
The text was updated successfully, but these errors were encountered: