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

Add an ability to generate DataFetcherResult for a specific fields in Java #1416

Closed
kirill071992 opened this issue Nov 30, 2023 · 4 comments
Closed
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@kirill071992
Copy link
Contributor

kirill071992 commented Nov 30, 2023

Is your feature request related to a problem? Please describe.
We have some cases when local context is required for Data fetching, to be able to pass some specific data for child resolvers. In this case, global context from DataFetchingEnvironment is not an option.

An example of using a local context:

return DataFetcherResult.<MobileLandingBlockGraph>newResult()
                .data(new BooksMobileLandingBlockGQL(groups.stream()
                        .map((Function<BooksProductGroup, BooksProductGroupGraph>) BooksProductGroupGQL::new)
                        .toList()))
                .localContext(booksProductService.populateContextForMobile(input.getInput(), "advantures"))
                .build();

As DataFetcherResult is not supported in the current version of the generator, we have to use the following to be able to generate it for a specific field in GraphQl schema:

abstract class GraphQLCodegenFixesGradleTask : DefaultTask() {
    @get:InputFile
    abstract val inputFile: RegularFileProperty

    @TaskAction
    fun updateMobileLandingGridDataGraph() {
        val findStr = "java.util.List<MobileLandingBlockGraph>" // Specify the string to find
        val replaceStr = "java.util.List<graphql.execution.DataFetcherResult<MobileLandingBlockGraph>>" // Specify the string to replace with

        val file = inputFile.get().asFile
        val content = file.readText()
        val updatedContent = content.replace(findStr, replaceStr)
        file.writeText(updatedContent)
    }
}

tasks.register<GraphQLCodegenFixesGradleTask>("graphQLCodegenFixes") {
    dependsOn("graphqlCodegen")
    inputFile.set(file("$buildDir/generated/com/scentbird/web/graphql/model/MobileLandingGridDataGraph.java"))
}

Describe the solution you'd like
The solution I'm proposing is the following:

  1. Add a new property for a plugin:
    fieldsWithDataFetcherResult = setOf("@dataFetcherResult")

This property will be used to mark fields that require DataFetcherResult generation. For example:

type MobileLandingGridData {
  # blocks of landing grid
  blocks: [MobileLandingBlock!]! @dataFetcherResult
  # there are more blocks on the next page
  hasNext: Boolean!
}
  1. Update the generator to be able to generate this code.
@jxnu-liguobin jxnu-liguobin added the enhancement New feature or request label Dec 1, 2023
@kirill071992
Copy link
Contributor Author

@kobylynskyi Hi, any thoughts or suggestions? :)

This was referenced Dec 12, 2023
@Andras-Csanyi
Copy link

I'm also looking for a solution to being able to generate DataFetcherResult.

@kobylynskyi kobylynskyi added this to the 5.10.0 milestone Feb 24, 2024
@kobylynskyi
Copy link
Owner

Will be included in the upcoming 5.10.0 release

@kobylynskyi
Copy link
Owner

This was released in 5.10.0. Thanks!

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

No branches or pull requests

4 participants