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

Generates data fetcher interface for query types #126

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

zaenk
Copy link

@zaenk zaenk commented May 8, 2021

The Java generator has an option to generate example data fetchers that can be copied into the codebase and extended as needed, but the kotlin generator has no such option.

This PR implements example data fetcher generator for kotlin with a twist: instead of generating a a class with TODO bodies, it generates an interface, for now.

This is the first in a series of PRs that would add an option similar to OpenAPI kotlin-spring generator interfaceOnly option.

Interfaces generated by this way would only be usable if the example output directory is set to the generated code directory. However, the generated interfaces would not be useable until Netflix/dgs-framework#320 is resolved. - update this is now merged

The enables the development of schemas in a separate repository. Automation would use this generator to generate data classes for types and interfaces for data fetchers and mutations that server component must implement. The generated sources would be packaged and published as a jar. Added a configuration flag generateDataFetcherInterfaces to make this behaviour opt-in.

edit: elaborates scope, links to PR in dsg-framework, new config param.

@ilterpehlivan
Copy link

Hi,
This PR would be answering my question here ? Netflix/dgs-framework#308 (comment)

Thanks

@zaenk
Copy link
Author

zaenk commented Dec 7, 2021

@ilterpehlivan yes, essentially it does, original discussion: Netflix/dgs-framework#234

@zaenk zaenk force-pushed the feature/generate-kotlin-data-fetchers-from-queries branch 3 times, most recently from 4ed5f96 to 5bb5f5e Compare December 7, 2021 20:51
@zaenk
Copy link
Author

zaenk commented Dec 8, 2021

rebased, tests are now passing

@zaenk zaenk force-pushed the feature/generate-kotlin-data-fetchers-from-queries branch from 5bb5f5e to a927018 Compare February 14, 2025 16:08
@zaenk
Copy link
Author

zaenk commented Feb 14, 2025

Many things changed since I opened this MR. Should this behaviour be also added to the config.generateKotlinNullableClasses == true code branch?

edit: added in a later commit - the generated interfaces are the same, I thing this code branch only effects data classes, but not data fetcher interfaces

@zaenk zaenk changed the title Generates data fetcher interface for query types Draft: Generates data fetcher interface for query types Feb 14, 2025
@zaenk
Copy link
Author

zaenk commented Feb 14, 2025

TODO: resolve issues with the generated annotation edit: fiexed, see next comment

public interface ShowsDatafetcher {
  @DgsData(
    parentType,
    field,
  )
  public fun getShows(titleFilter: String?, dataFetchingEnvironment: DataFetchingEnvironment):
      List<Show?>?
}

@zaenk zaenk force-pushed the feature/generate-kotlin-data-fetchers-from-queries branch from a927018 to 1599deb Compare February 14, 2025 17:59
@zaenk
Copy link
Author

zaenk commented Feb 14, 2025

resolved annotation generation issue, also added DgsComponent annotation to this interface

@DgsComponent
public interface ShowsDatafetcher {
  @DgsData(
    parentType = DgsConstants.QUERY.TYPE_NAME,
    field = DgsConstants.QUERY.Shows,
  )
  public fun getShows(titleFilter: String?, dataFetchingEnvironment: DataFetchingEnvironment):
      List<Show?>?
}

@zaenk zaenk changed the title Draft: Generates data fetcher interface for query types Generates data fetcher interface for query types Feb 14, 2025
@zaenk zaenk force-pushed the feature/generate-kotlin-data-fetchers-from-queries branch from 1599deb to d3fc878 Compare February 14, 2025 22:42
@zaenk
Copy link
Author

zaenk commented Feb 14, 2025

added one more thing, again, oversight from my part: input argument annotation

@DgsComponent
public interface ShowsDatafetcher {
  @DgsData(
    parentType = DgsConstants.QUERY.TYPE_NAME,
    field = DgsConstants.QUERY.Shows,
  )
  public fun getShows(@InputArgument("titleFilter") titleFilter: String?,
      dataFetchingEnvironment: DataFetchingEnvironment): List<Show?>?
}

@zaenk
Copy link
Author

zaenk commented Feb 14, 2025

changed datafetcher postfix to query, also added generator for mutation and subscription - these are based on https://github.com/Netflix/dgs-examples-kotlin

@DgsComponent
public interface ShowsQuery {
  @DgsData(
    parentType = DgsConstants.QUERY.TYPE_NAME,
    field = DgsConstants.QUERY.Shows,
  )
  public fun shows(@InputArgument(DgsConstants.QUERY.SHOWS_INPUT_ARGUMENT.TitleFilter)
      titleFilter: String?, dataFetchingEnvironment: DataFetchingEnvironment): List<Show?>?
}


@DgsComponent
public interface AddArtworkMutation {
  @DgsData(
    parentType = DgsConstants.MUTATION.TYPE_NAME,
    field = DgsConstants.MUTATION.AddArtwork,
  )
  public fun addArtwork(
    @InputArgument(DgsConstants.MUTATION.ADDARTWORK_INPUT_ARGUMENT.ShowId) showId: Int,
    @InputArgument(DgsConstants.MUTATION.ADDARTWORK_INPUT_ARGUMENT.Upload) upload: MultipartFile,
    dataFetchingEnvironment: DataFetchingEnvironment,
  ): List<Image?>
}

@DgsComponent
public interface AddReviewMutation {
  @DgsData(
    parentType = DgsConstants.MUTATION.TYPE_NAME,
    field = DgsConstants.MUTATION.AddReview,
  )
  public fun addReview(@InputArgument(DgsConstants.MUTATION.ADDREVIEW_INPUT_ARGUMENT.Review)
      review: SubmittedReview?, dataFetchingEnvironment: DataFetchingEnvironment): List<Review?>?
}


@DgsComponent
public interface ReviewAddedSubscription {
  @DgsData(
    parentType = DgsConstants.SUBSCRIPTION.TYPE_NAME,
    field = DgsConstants.SUBSCRIPTION.ReviewAdded,
  )
  public fun reviewAdded(@InputArgument(DgsConstants.SUBSCRIPTION.REVIEWADDED_INPUT_ARGUMENT.ShowId)
      showId: Int, dataFetchingEnvironment: DataFetchingEnvironment): Publisher<Review?>
}

@zaenk
Copy link
Author

zaenk commented Feb 15, 2025

also addedd datafetcher interface generation to kotlin2 code branch

it would generate the same interface files as the other branch - the difference in this branch is that instead of generating non-nullable fields on data classes, it generates a "supplier". the reason for this is when the client doesn't requests a non-nullable field, the server would still invoke the data fetcher for it, otherwise it would run into a initialization filter. this behaviour doesn't affect the data fetcher interfaces

@zaenk
Copy link
Author

zaenk commented Feb 15, 2025

@paulbakker @iuliiasobolevska @srinivasankavitha this feature request adds new functionality without modifying existing behaviour. wdyt, is it something that you can merge into mainline? Short description of the feature below:

I've added a new configuration option to the gradle plugin, generateDataFetcherInterfaces - this will generate SAM for each property a Mutation, Query or Subscription object has - effectively anything that needs a datafetcher implementation.

This enables a workflow descirbed in Netflix/dgs-framework#234

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.

2 participants