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

Variables are not supported #51

Closed
Oxymoron290 opened this issue Aug 26, 2019 · 7 comments
Closed

Variables are not supported #51

Oxymoron290 opened this issue Aug 26, 2019 · 7 comments

Comments

@Oxymoron290
Copy link

Oxymoron290 commented Aug 26, 2019

When creating a query like the following, I get a 500 error.

{
    "query":"query($id: Int!)
    {
        TruckLoad(id: $id){
            id
            truckload{id}
        }
    }",
    "variables":{
	"id": 5
    }
}

The endpoint throws an exception about a null argument

com.apurebase.kgraphql.RequestException: argument id is not optional, value cannot be null

However, if I specify a default variable, like the following, the endpoint returns, but only using the default value (2 in this example) instead of the defined variable (5 in this example)

{
    "query":"query($id: Int! = 2)
    {
        TruckLoad(id: $id){
            id
            truckload{id}
        }
    }",
    "variables":{
	"id": 5
    }
}
@jeggy
Copy link
Member

jeggy commented Aug 27, 2019

how are you calling .execute() ?

Passing variables is split into 2 different arguments.

Here's an example:

val dollarSign = '$'

val query = """
    query(${dollarSign}id: Int! = 6){
        TruckLoad(id: ${dollarSign}id) {
            id
            TruckLoad{
                id
            }
        }
    }""".trimIndent()
val variables = """
    {
        "id": 5
    }
""".trimIndent()

val res = schema.execute(query, variables)

println(res)

@Oxymoron290
Copy link
Author

Using ktor:

routing {
    route("/graphql") {
        post {
            val request = call.receive<GraphQLRequest>()
            val query = request.query
            val context: ThetaContext by inject()
            val sch = ThetaSchema(context)
            val result = sch.schema.execute(query)
            call.respondText(result, io.ktor.http.ContentType.Application.Json)
        }
    }
}

@jeggy
Copy link
Member

jeggy commented Sep 13, 2019

I'm planning to do a module for ktor, so in the feature you would just install KGraphQL into ktor something like this:

embeddedServer(...) {
    install(KGraphQL)
}

But for for you could take a look at this project: https://github.com/jeggy/ktorgraphql/blob/master/src/Application.kt#L55

@Oxymoron290
Copy link
Author

I think I saw a PR or something about that in ktor's project. do you have a link?

@jeggy
Copy link
Member

jeggy commented Sep 13, 2019

I've seen some that are using the java implementation of GraphQL. But I haven't seen any for KGraphQL.
I'm gonna do one that is specifically made for KGraphQL

@Oxymoron290
Copy link
Author

Awesome. also, trying your solution it shows that request does not have properties for mutation or variables
image

Using v0.6.6

@Oxymoron290
Copy link
Author

I'm sorry, I got confused
Found what I needed here - https://github.com/jeggy/ktorgraphql/blob/master/src/GraphQLRequest.kt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants