considering the following javascript code (partially taken from Apollo Server documentation), it creates an instance of ApolloServer and start it.
const {ApolloServer} = require('apollo-server')
const server = new ApolloServer({ ... });
server.listen().then(({ url }) => {
console.log(`Server ready at ${url}`);
});
Now consider to replicate the same behaviour using KotlinJS.
Firstly, Kotlin doesn't have the "new" keyword and calling ApolloServer()
as expected, won't work but raise an error (TypeError: Class constructor ApolloServer cannot be invoked without 'new').
// We can banally represent part of the code above like:
external fun require(module: String): dynamic
val ApolloServer = require("apollo-server").ApolloServer
// ApolloServer is a js class
Declaring an external class like:
external open class ApolloServer() {
open fun listen(vararg opts: Any): Promise<Any>
operator fun invoke(): Any
}
and set it as ApolloServer type doesn't help.
How do we replicate "new ApolloServer()" call?
new
. One band-aid fix could be to implementfunction createServer(...) { return new ApolloServer(...) }
? – Lidanew
keyword in Kotlin (atleast what we do in Kotlin/JVM) – Angell