Another follow up Q... has anyone had any luck exp...
# apollo-kotlin
r
Another follow up Q... has anyone had any luck exposing just an introspected schema and serving it to apollo studio? Trying to use a simple ktor api to host these schemas I am generating, but apollo studio is giving me cors errors 😞 Using the diagnostic tool I get the following
Copy code
> npx diagnose-endpoint@1.1.0 --endpoint=<http://localhost:8080>
Diagnosing <http://localhost:8080>
⚠  OPTIONS response is missing header 'access-control-allow-methods: POST'
(📫 Interested in previewing a local tunnel to bypass CORS requirements? Please let us know at <https://docs.google.com/forms/d/e/1FAIpQLScUCi3PdMerraiy6GpD-QiC_9KEKVHr4oDL5Vef5fIvzqqQWg/viewform> )
but, as far as I can tell, my ktor api should be configured properly
Copy code
fun Application.myApplicationModule() {
    install(CallLogging) {
        level = Level.TRACE
    }
    install(CORS) {
        allowHeader(HttpHeaders.ContentType)
        anyHost()
    }
    routing {
        route("/") {
            post {
                val models = getOgModels()
                val sdl = generateCustomObjectGraph(models).toString()
                val schema = parseSdl(sdl).validateAsSchema().value!!.toIntrospectionSchema().toJson()
                call.response.header("content-type", "application/json")
                call.respondText(schema)
            }
        }
    }
}
Ktor docs indicate that by default, POST is allowed https://ktor.io/docs/cors.html#methods
m
I think the
npx diagnose-endpoint
warning is a false positive, I can run the same thing locally fine
I would suspect the schema you're returning is missing the "data" field.
toJson()
will return the
__Schema
field, you'll need to wrap it in the GraphQL response type (the one with "data" and "error")
More generally though, I wouldn't recommend returning an hardcoded introspection schema for anything else than debugging/experimentation. Clients (like explorer but others too) may request different introspection schemas (since introspection is itself a GraphQL query)
Any regular GraphQL framework like graphql-java, kgraphql or graphql-kotlin should handle introspection for yuo
r
ah interesting... thanks I'll give that a shot. My use case is very strange, so this is really my only option. We aren't really building a graphql api... we are mostly just toying with the idea of using graphql as a facade for an internal query language at my company that (unfortunately imo) isn't going anywhere
m
Gotcha 👍 We always need more query languages 😄
r
updated the code snippet with
_call_.respondText("{*\"*data*\"*: *$*schema}")
but still get the error 😞 still good to at least be sending the right payload now
m
FWIW, what's working for me looks like so:
Copy code
{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query"
      },
      "mutationType": {
        "name": "Mutation"
      },
      "subscriptionType": {
        "name": "Subscription"
      },
      "types": [
Might be worth opening your browser devtools and checking the HTTP response, making sure it's 200
Not an expert by any means but I would expect GraphQL Java to support schema first approaches. There is some doc here. Maybe you can try to omit the runtimeWiring and see what's happening?
(or maybe a very simple runtime wiring?)