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

Increase customizability of values ​​that can be returned by apiReturnType #1167

Closed
matsudamper opened this issue May 1, 2023 · 6 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@matsudamper
Copy link
Contributor

matsudamper commented May 1, 2023

Description

I want to generate the following code

  • Need to return CompletableFuture to dispatch DataLoader
  • Need to set localContext

DataLoader

fun book(
    id: Long,
    env: DataFetchingEnvironment,
): CompletionStage<DataFetcherResult<Book>> { // here
    val bookNameDataLoader = env.dataLoaderRegistry.getDataLoader<Long, String>("key")
    val bookNameCompletableFuture = bookNameDataLoader.load(id)

    return CompletableFuture.supplyAsync {
        DataFetcherResult.newResult<Book>()
            .localContext("additionalContext")
            .data(
                Book(
                    id = id,
                    name = bookNameCompletableFuture.get(),
                )
            )
            .build()
    }
}

The apiReturnType setting does not seem to allow the following to be output. We would like to enable this.
CompletionStage<DataFetcherResult<T>>

Describe the solution you'd like

I want to enable the following statements

apiReturnType = "java.util.concurrent.CompletionStage<graphql.execution.DataFetcherResult<{{TYPE}}>>"

Describe alternatives you've considered

Other

I can submit the code.
https://github.com/kobylynskyi/graphql-java-codegen/compare/main...matsudamper:graphql-java-codegen:feature/damper/api-return-type-placeholder?expand=1

@kobylynskyi
Copy link
Owner

@matsudamper your approach looks good to me. But can you please add a few unit tests:

  • specifying apiReturnType with {{TYPE}} and checking generated code for APIs that return both type and list of types.
  • specifying apiReturnType and apiReturnListType with {{TYPE}} and checking generated code for APIs that return both type and list of types.

@kobylynskyi
Copy link
Owner

@matsudamper can you please add a few unit tests for the functionality you added? Thank you.

@viacheslav-dobrynin
Copy link

viacheslav-dobrynin commented Jul 21, 2023

Hi, @kobylynskyi, @matsudamper!
Can you please tell me how it should work?
When I add apiReturnType = "java.util.concurrent.CompletableFuture<{{TYPE}}>", this is added for all resolvers.
Although I need only for field resolvers, because for @QueryMapping Spring does not recognize CompletableFuture, only for SchemaMapping (this is necessary for the DataLoader to work properly)

UPD: Spring is not the problem, see details below

@kobylynskyi
Copy link
Owner

@DobryninVyacheslav in this case I guess we would need to introduce another property, something like fieldResolverReturnType. You would use it instead of apiReturnType

@viacheslav-dobrynin
Copy link

viacheslav-dobrynin commented Jul 21, 2023

Now I tested my code again and found that the problem was not in Spring.
In fact, in the process of testing, I removed the resolver interface, in which there was an annotation @Argument, due to the absence of which nothing worked.
When I returned it, everything worked. I'm sorry I didn't notice this sooner.
However, yes, it would be convenient to have such an option (fieldResolverReturnType)
Thank you!

@viacheslav-dobrynin
Copy link

In the end, I came to the conclusion that these settings allow the use of DataLoader and looks like a good solution:

apiReturnType = "reactor.core.publisher.Mono"
apiReturnListType = "reactor.core.publisher.Flux"

For blocking code too. You can use Mono.fromFuture, Flux.fromIterable and so on.
Therefore, for my case, there is no longer a need for the option fieldResolverReturnType, but perhaps it will be useful to someone.

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

3 participants