Generating multiple clients in one project #939
-
I am trying to build a form of backend for frontend/api gateway as a proof of concept that communicates with multiple graphql services. I do want to transform the schema though so I cannot only rely on federation. Is this the case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hello 👋 It is pretty ugly as that was not one of our use cases but following should work // configuration to generate first client against gscheibel/graphql-kotlin-demo
graphql {
client {
sdlEndpoint = "http://localhost:8080/sdl"
packageName = "com.example.generated"
clientType = GraphQLClientType.WEBCLIENT
queryFileDirectory = "${project.projectDir.absolutePath}/src/main/resources/first"
}
}
// above graphql extension configuration is equivalent to the below
//val graphqlDownloadSDL by tasks.getting(GraphQLDownloadSDLTask::class) {
// endpoint.set("http://localhost:8080/sdl")
//}
//
//val graphqlGenerateClient by tasks.getting(GraphQLGenerateClientTask::class) {
// clientType.set(GraphQLClientType.WEBCLIENT)
// packageName.set("com.example.generated")
// schemaFile.set(graphqlDownloadSDL.outputFile)
// queryFileDirectory.set("${project.projectDir.absolutePath}/src/main/resources/first")
// dependsOn("graphqlDownloadSDL")
//}
// configuration for the second client running against graphql-kotlin/examples/spring
val graphqlDownloadOtherSDL by tasks.registering(GraphQLDownloadSDLTask::class) {
endpoint.set("http://localhost:8081/sdl")
// explicitly define dependency on the other client generation so it will download other schema AFTER the first generation
dependsOn("graphqlGenerateClient")
}
val graphqlGenerateOtherClient by tasks.registering(GraphQLGenerateClientTask::class) {
clientType.set(GraphQLClientType.WEBCLIENT)
packageName.set("com.example.generated2")
schemaFile.set(graphqlDownloadOtherSDL.get().outputFile)
queryFileDirectory.set("${project.projectDir.absolutePath}/src/main/resources/second")
outputDirectory.set(project.layout.buildDirectory.dir("generated/source/graphql/main"))
dependsOn("graphqlDownloadOtherSDL")
}
tasks {
// original client generation task will automatically add itself to compileKotlin dependency
// make sure that before we run compile task it will generate other client as well
compileKotlin {
dependsOn("graphqlGenerateOtherClient")
}
} I tested it with an example project dariuszkuc/graphql-kotlin-webclient-demo@1686460 using https://github.com/gscheibel/graphql-kotlin-demo as the first server running on port 8080 and Spring example app from graphql-kotlin repo running on port 8081. |
Beta Was this translation helpful? Give feedback.
Hello 👋
If you are using Gradle it should be possible to generate multiple clients from different schemas. It might be possible to do in Maven by forcing specific phases to order the tasks but unsure if it will work.
It is pretty ugly as that was not one of our use cases but following should work