-
Hey folks. I'm kinda new to Kotlin and GraphQL Kotlin implementation, so I want your help to understand how should I generate stuff using https://graphql-code-generator.com/ so it will be as flexible as possible and will enable lazy calculation of some particular fields.
What currently https://graphql-code-generator.com/ and its So, what approach will you take if you were me? :) Or maybe there are heavy cons against this approach and you can stop me before I do a mistake? :) Kotlin gives a lot of syntax sugar, so I need a bit of guidance here. As I'm unsure if kotlin-graphql calls And just to reiterate in other words: schema {
query: Query
}
type Query {
me: User!
}
type User {
name: String!
organization: Organization!
}
type Organization {
id: ID!
} Will generate abstract class BaseQuery(
val me: BaseUser
)
abstract class BaseUser(
val name: String
val organization: Organization
)
abstract class BaseOrganization(
val id: java.util.UUID
) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 10 replies
-
Ok something like that maybe? interface BaseUser {
val name: String
val organization: BaseOrganization
}
interface BaseQuery {
val me: BaseUser
}
interface BaseOrganization {
val id: UUID
} Implementation: data class Organization(
override val id: UUID
): BaseOrganization
data class User (
override val name: String,
): BaseUser {
override val organization: Organization
get() {
TODO("Lazy call to database for organization")
}
}
class RootQuery: Query, BaseQuery {
override val me: BaseUser
get() = TODO("Not yet implemented")
} Whadayathink? |
Beta Was this translation helpful? Give feedback.
-
Hello 👋 Using code generator to create a new project from an SDL could be useful - i.e. it would allow you to generate some skeleton app that you then fill with logic could save some setup time, but I am unsure how that flow would look like in an already established app. If your code generator generates the data classes you would still need to create some resolver to be able to retrieve those (well at least for the top level query) and you would also be somewhat limited in your code as you wouldn't be able to lazily evaluate those fields. I guess it could work if it generates some abstract interfaces (marked as If all you are trying to do is verify that original SDL matches the one implemented by |
Beta Was this translation helpful? Give feedback.
-
The answer would be, just set up a schema printer and compare manually created extension of the schema e.g. a contract agreed with FE or any other consumer or engineer, the one that is generated by the Kotlin GraphQL using The whole thing will look like this: So printer bean: package your.package
import com.expediagroup.graphql.extensions.print
import graphql.schema.GraphQLSchema
import org.springframework.stereotype.Component
import java.io.File
@Component
class GraphQLSchemaPrinter(val schema: GraphQLSchema) {
init {
printToFile()
}
final fun printToFile() {
File("./generated.schema.graphql").writeText(schema.print())
}
} Here is the gradle task in kotlin syntax if anyone wondering :) tasks {
register<NpmTask>("graphqlDiff") {
dependsOn("npmInstall")
description =
"Logically (not like a string, but AST wise) compares generated schema by KotlinGraphQLSchemaGenerator with manually written schema.graphql file"
if (!file("./generated.schema.graphql").exists()) {
throw GradleException(
"You should have generated.schema.graphql file generated. Run the app, or tests, to generate it."
)
}
inputs.files(fileTree("./") {
include("*.graphql")
})
setArgs(listOf("run", "gql:diff"))
}
} implying that you have {
"name": "backend",
"version": "1.0.0",
"private": true,
"scripts": {
"gql:diff": "graphql-inspector diff \"schema.graphql\" \"generated.schema.graphql\""
},
"devDependencies": {
"@graphql-inspector/cli": "2.3.0",
"graphql": "15.4.0",
"yargs": "16.2.0"
}
} You can also run this command on CI after test run, as after test run, if you're using Spring, this printer will print the |
Beta Was this translation helpful? Give feedback.
The answer would be, just set up a schema printer and compare manually created extension of the schema e.g. a contract agreed with FE or any other consumer or engineer, the one that is generated by the Kotlin GraphQL using
graphql-inspector diff
command :) As generating resolvers interfaces, or smth similar to that mostly makes no sense :) Tho I did it, but it's not as flexible or elegant enough to be a working thing. With rawgraphql-java
DataFetchers can work tho but it's way too verbose for my taste :)The whole thing will look like this:
So printer bean: