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

Invalid default value for a clauses argument #1

Open
OlegIlyenko opened this issue Jun 5, 2018 · 2 comments
Open

Invalid default value for a clauses argument #1

OlegIlyenko opened this issue Jun 5, 2018 · 2 comments

Comments

@OlegIlyenko
Copy link

OlegIlyenko commented Jun 5, 2018

Recently I noticed that Universe GraphQL schema has changed a bit and new JSON scalar type as well as a default value were introduced. In the introspection JSON it looks like this:

{
  "defaultValue" : "[]",
  "description" : "array of {key: value} or 'string' where clauses",
  "name" : "clauses",
  "type" : {
    "kind" : "SCALAR",
    "name" : "JSON",
    "ofType" : null
  }
}

Based on my understanding of the GraphQL spec, the default value [] is not compatible the the argument type. According to the GraphQL spec:

defaultValue may return a String encoding (using the GraphQL language) of the default value used by this input value in the condition a value is not provided at runtime. If this input value has no default value, returns null.

The argument type is a simple scalar value. This means that it can't have value like [] (the default value is described with GraphQL syntax, not JSON). I'm not sure how JSON scalar type is implemented internally, but if it is represented with a String literal, then the default value should be something like "defaultValue": "\"[]\"".

I was not 100% sure where to report this issue, so I decided to put it here. Would be great if somebody could look into it.

@exAspArk
Copy link
Contributor

Hey @OlegIlyenko, thanks for opening the issue.

I'm not exactly following why "[]" is an invalid value?

If we think about JSON, then [] is a valid JSON value, right? If we string encode the value, we have "[]". If you pass this value with the GraphQL request, it should work fine:

query {
  host(id: "someId") {
    report(clauses: "[]") {
      columns
      preview
    }
  }
}

On the server-side, we simply take the "[]" string and decode it to the language-specific data structure (object, hash, map, etc.).

@OlegIlyenko
Copy link
Author

OlegIlyenko commented Jun 29, 2018

Thanks a lot for looking into it! I totally agree with what you have described, I think it is right behaviour. I think the issue boils down to an encoding of defaultValue in the introspection results.

As you have described, "[]" is valid GraphQL value. It is a string literal and correct representation of the JSON custom scalar in GraphQL syntax.

According to the spec, the defaultValue in introspection result must be encoded in GraphQL syntax. this would mean that valid introspection result should look like this:

{
  "defaultValue" : "\"[]\"",
  "description" : "array of {key: value} or 'string' where clauses",
  "name" : "clauses",
  "type" : {
    "kind" : "SCALAR",
    "name" : "JSON",
    "ofType" : null
  }
}
  "defaultValue" : "\"[]\"",
                    ^    ^
                    |    |
                    +-+--+
                      |
          this is actual value of the "default value"

Currently introspection result looks like this:

{
  "defaultValue" : "[]",
  "description" : "array of {key: value} or 'string' where clauses",
  "name" : "clauses",
  "type" : {
    "kind" : "SCALAR",
    "name" : "JSON",
    "ofType" : null
  }
}

If we translate it back in your example query, this would imply that query should look like this:

query {
  host(id: "someId") {
    report(clauses: []) {
      columns
      preview
    }
  }
}

So closes then must be of a list type.

It often gets confusing when it comes to string escaping. Let me know if you could follow my explanation.

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