GraphQL error when not passing value for optional variables
MichaelDeBoey opened this issue · 3 comments
On updating to the newest release, we started getting errors when not passing a value for optional variables
schema:
type Query {
Files(linked_id: LinkedId!, map_id: Int): File
}
type File {
id: Int
name: String
added_time: String
}
type LinkedId {
contract_id: Int
object_id: Int
}
query:
query getAllFilesForFolderQuery($linkedId: LinkedId!, $folderId: Int) {
files: Files(linked_id: $linkedId, map_id: $folderId) {
id
name
addedOn: added_time
}
}
variables:
{
linkedId: {contract_id: 4394885}
}
response:
{"errors": [
{"message": "Variable folderId hasn't been submitted"}
]}
@MichaelDeBoey it was actually a flaw in the library... there's not such thing as "optional variable" in the GraphQL protocol... we'll try to reproduce it in the JS version, but according to https://facebook.github.io/graphql/#sec-Coercing-Field-Arguments (http://take.ms/dAxN1) it's not clear. You can join conversation on gitter, we're just discussing it https://gitter.im/Youshido/GraphQL
As @viniychuk mentioned, variables don't have default values by themselves - you have to provide a default value for that variable (copied example from here):
query ($username: String = "GovSchwarzenegger"){
reddit {
user(username: $username) {
username
commentKarma
createdISO
}
}
}
Unfortunately as it seems, the library cannot parse this yet. At least when I do this on a query, I do get a parse error:
{
"errors": [
{
"message": "Can\\t recognize token type",
"locations": [
{
"line": 1,
"column": 25
}
]
}
]
}
which corresponds to the whitespace after the variable's type.
fixed in 1.4.3.4