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

Introduce a ktor feature #37

Open
3 of 5 tasks
jeggy opened this issue Jun 18, 2019 · 10 comments
Open
3 of 5 tasks

Introduce a ktor feature #37

jeggy opened this issue Jun 18, 2019 · 10 comments
Assignees
Labels
enhancement New feature or request

Comments

@jeggy
Copy link
Member

jeggy commented Jun 18, 2019

Would be nice to make this into a multi project and create a ktor feature which adds a /graphql route. And would have some configuration support to allow graphiql support out of the box

Status:

@Allali84
Copy link

Hello,
Is there someone working on this feature ? can i take care of it ? :)

@jeggy
Copy link
Member Author

jeggy commented Feb 24, 2020

I started to look at this last week. Quickly ran into the question of what would be the best way to solve this?

I was thinking of just making a extension function to ktor's Route
so you would use it like this:

install(Routing) {
  graphql {
    query("myType") {
      resolver { -> "Hello World" }
    }
  }
  // ... Your other normal routes
}

I would like to have easy setup of different things like:

  • GraphiQL
  • Websockets
  • Authentication
  • Ktor context mapping to KGraphQL context.

So I wanted to design the API before going directly to implementation.

[Edit - August 2020]: This was just a suggestion and is not what we ended with. Please look at the docs for the correct instructions for setting GraphQL up within ktor.

install(WebSockets)
install(Routing) {
  // Wrapping this inside a `Route.authenticate`, should work out of the box also.
  graphql {
    configure {
      // Setting this to true would return a web app of [https://github.com/prisma-labs/graphql-playground] 
      // Only when a HTTP method is `GET` and HTTP Accept header has `text/html`
      playground = true
    }
    query("myType") {
      resolver { ctx: Context ->
        // This User will come directly from the 
        val user = ctx.get<User>()
        "Hello ${user.name}"
      }
    }
  }
}

We can maybe wait a bit with the web sockets part, as that is mostly for subscription queries.

Other than that I would love to hear input, how would you imagine the usage would be?
And do you think the idea I have provided here would be easy to use?

@jeggy
Copy link
Member Author

jeggy commented Feb 24, 2020

You are welcome to come with a pull request for this 😃

Then I can put my focus on something else instead

@Allali84
Copy link

I have not thought about the design yet but you have already helped me with your idea that I like very much, I will try to implement it and make a pull request so that we can discuss it at that time
You can focus on something else and let take care of it 😃

@jeggy
Copy link
Member Author

jeggy commented Feb 24, 2020

I have created some basic code that you can look at here: 82c6203

I tried using Kotlin Serilization instead of Gson, but ran into some problems and just went with Gson in that example.

I would love it to use Kotlin Serialization or even better it would just use whatever implementation that they have used within their ContentNegotiation configuration. So we will be one step closer to multiplatform support(#6)

Allali84 added a commit to Allali84/KGraphQL that referenced this issue Mar 1, 2020
Allali84 added a commit to Allali84/KGraphQL that referenced this issue Mar 1, 2020
Allali84 added a commit to Allali84/KGraphQL that referenced this issue Mar 1, 2020
jeggy pushed a commit that referenced this issue Mar 1, 2020
jeggy pushed a commit that referenced this issue Mar 1, 2020
@zole40
Copy link

zole40 commented Aug 17, 2020

Hello, guys. I try to use the Ktor plugin and found a problem but I do not know it is a bug or just I don`t understand how it works. I migrated our code to use the Ktor plugin and it works well. After that, I tried to use the playground but is it not work with basic authentication and return with HTTP 401 in any way. It is a little confusing because it works with Ktor client and Insomnia as well. Can you help to fix or understand the problem?

@jeggy
Copy link
Member Author

jeggy commented Aug 17, 2020

@zole40 Could you provide some more information about your setup? As for the small examples that I have tested it with, it does work.
Do you have some authentication setup using the wrap/context?
Do you have some custom CORS setup?
...

If possible it would be great if you could create a small example setup of where it fails, then I can fully investigate what's going wrong.

@zole40
Copy link

zole40 commented Aug 18, 2020

@zole40 Could you provide some more information about your setup? As for the small examples that I have tested it with, it does work.
Do you have some authentication setup using the wrap/context?
Do you have some custom CORS setup?
...

If possible it would be great if you could create a small example setup of where it fails, then I can fully investigate what's going wrong.

I used a simple configuration like this for test it:

   install(Authentication) {
       basic("test") {
            validate {
                UserIdPrincipal("test")
            }
        }
    }
    install(GraphQL) {
        playground = true
        wrap {
            authenticate("test", build = it)
        }
        context { call ->
            // access to authentication is only available if this is wrapped inside a `authenticate` before hand.
            call.authentication.principal<UserIdPrincipal>()?.let {
                +UserInfo(it.name, "")
            }
        }
        schema {
            configure {
                useDefaultPrettyPrinter = true
            }
            query("hello") {
                resolver { ctx: Context ->
                    val user = ctx.get<UserInfo>()!!
                    "Hello ${user.name}"
                }
            }
        }
    }

When I call it from Insomnia, it works. But when navigate to the playground localhost:8080/graphql it stopped working and I have the following error message.
image
[Error] Blocked http://localhost:8080/graphql from asking for credentials because it is a cross-origin request.
It looks like the playground calls do not contains the authentication headers.

@jeggy
Copy link
Member Author

jeggy commented Aug 18, 2020

I got a completely different issue kotlinx.serialization.MissingFieldException: Field 'operationName' is required, but it was missing when using that Insomnia client. I have fixed this specific issue in version 0.15.6.

Are you making the request from a separate machine than where the ktor server is on? If so it seems like you should look at setting up CORS

@zole40
Copy link

zole40 commented Aug 19, 2020

I got a completely different issue kotlinx.serialization.MissingFieldException: Field 'operationName' is required, but it was missing when using that Insomnia client. I have fixed this specific issue in version 0.15.6.

Are you making the request from a separate machine than where the ktor server is on? If so it seems like you should look at setting up CORS

I making a request from the same machine.
I tried to set up the CORS and it does not solve the problem. I found a new error message in another browser and it looks like the problem is the playground js code is loaded from another host and tries to make a cross-origin request and block itself. It could be possible the Ktor server does not host the playground js code?
ServerParseError: Unexpected end of JSON input at JSON.parse (<anonymous>) at http://cdn.jsdelivr.net/npm/graphql-playground-react/build/static/js/middleware.js:124:179090

@jeggy jeggy added the enhancement New feature or request label Jun 6, 2021
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