Skip to content

Generate getters for interface-typed fields when generating interfaces#882

Merged
jiholee17 merged 2 commits intomasterfrom
feature/interface-field-getters
Oct 13, 2025
Merged

Generate getters for interface-typed fields when generating interfaces#882
jiholee17 merged 2 commits intomasterfrom
feature/interface-field-getters

Conversation

@jiholee17
Copy link
Copy Markdown
Collaborator

@jiholee17 jiholee17 commented Oct 10, 2025

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 InterfaceGenerator to generate getters for interface fields that are also an interface by default. If generateInterfaceMethodsForInterfaceFields is 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 override List<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 InterfaceGenerator and verified expected behavior using a snapshot in test repo.

if (config.generateInterfaceSetters) {
// Only generate setters for non-interface-typed fields unless generateInterfaceMethodsForInterfaceFields is true
if (config.generateInterfaceMethodsForInterfaceFields ||
(config.generateInterfaceSetters && !isFieldAnInterface(fieldDefinition))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would config.generateInterfaceSetters && !isFieldAnInterface(fieldDefinition) condition be ever true for interfaces (assuming that config.generateInterfaceSetters controls setters generation specifically for interfaces)?

Copy link
Copy Markdown
Collaborator Author

@jiholee17 jiholee17 Oct 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for clarifying!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you please document these config options in the docs?

@paulbakker
Copy link
Copy Markdown
Collaborator

I wasn't aware of generateInterfaceMethodsForInterfaceFields and generateInterfaceSetters, and it's a bit of a mess with options.

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.
I did a bit of testing with the existing flags, and it looks like it works for what we need.

@paulbakker
Copy link
Copy Markdown
Collaborator

After talking to @jiholee17 I realize my understanding about generateInterfaceSetters was incorrect. There is actually no way to only generate getters for fields that are an interface.

I think it makes sense to do that by default like this PR implements.

@jiholee17 jiholee17 merged commit 4559d5c into master Oct 13, 2025
4 checks passed
@jiholee17 jiholee17 deleted the feature/interface-field-getters branch October 22, 2025 17:37
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

Successfully merging this pull request may close these issues.

GraphQL Interface fields that are also an interface, are omitted from the generated interface

3 participants