Generate getters for interface-typed fields when generating interfaces#882
Generate getters for interface-typed fields when generating interfaces#882
Conversation
| if (config.generateInterfaceSetters) { | ||
| // Only generate setters for non-interface-typed fields unless generateInterfaceMethodsForInterfaceFields is true | ||
| if (config.generateInterfaceMethodsForInterfaceFields || | ||
| (config.generateInterfaceSetters && !isFieldAnInterface(fieldDefinition)) |
There was a problem hiding this comment.
how would config.generateInterfaceSetters && !isFieldAnInterface(fieldDefinition) condition be ever true for interfaces (assuming that config.generateInterfaceSetters controls setters generation specifically for interfaces)?
There was a problem hiding this comment.
config.generateInterfaceSetters (default true) controls whether setters are generated for fields that are not interface types in the generated interface class. For example config.generateInterfaceSetters && !isFieldAnInterface(fieldDefinition) would be true and generate a setter for a String field in an interface class. Setters for fields that are themselves interfaces are generated only if config.generateInterfaceMethodsForInterfaceFields is true.
I think we should update the docs for configuration options to make this clear as I also had to trace the code to figure this out
There was a problem hiding this comment.
thank you for clarifying!
There was a problem hiding this comment.
can you please document these config options in the docs?
graphql-dgs-codegen-core/src/test/kotlin/com/netflix/graphql/dgs/codegen/CodeGenTest.kt
Show resolved
Hide resolved
|
I wasn't aware of I'm wondering if these flags are satisfying what the user was asking for; if that's the case, we should probably not change any default behavior. If there's another issue with these flags we can address that. |
|
After talking to @jiholee17 I realize my understanding about I think it makes sense to do that by default like this PR implements. |
Closes #881
Changes
Currently, getters and setters for GraphQL interface fields that are also an interface are omitted from the generated interface. This PR updates
InterfaceGeneratorto generate getters for interface fields that are also an interface by default. IfgenerateInterfaceMethodsForInterfaceFieldsis enabled in the application configuration, both getters and setters will be generated for interfaces, which preserves existing behavior.Getters are not generated for fields that are GraphQL list types where the inner type is an interface as Java does not support overriding these types with more specific types (i.e.
List<Dog>in a concrete implementation does not overrideList<Pet>in an interface, but[Dog]overrides[Pet]in GraphQL). GraphQL union types and generated Kotlin interfaces are not affected.Testing
Updated unit tests for
InterfaceGeneratorand verified expected behavior using a snapshot in test repo.