Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Allow undefined variable in query #70

Merged
merged 3 commits into from
Jan 17, 2018

Conversation

oliversong
Copy link
Contributor

@oliversong oliversong commented Jan 12, 2018

Before:
screen shot 2018-01-12 at 4 10 28 pm

After:
screen shot 2018-01-12 at 4 10 38 pm

Copy link
Contributor

@stephen stephen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for looking into this.

as we discussed, we also do not support default values specified in the query document, which would be a nice to have follow up.

@@ -43,7 +43,7 @@ type InputValue struct {
Name string
Description string
Type Type
DefaultValue string
DefaultValue *string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you reference the spec doc in the commit message?

http://facebook.github.io/graphql/October2016/#sec-The-__InputValue-Type

@@ -36,7 +36,7 @@ func valueToJson(value ast.Value, vars map[string]interface{}) (interface{}, err
case *ast.Variable:
actual, ok := vars[value.Name.Value]
if !ok {
return nil, NewClientError("unknown var: %s", value.Name.Value)
return nil, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add tests for the 2x2:

  • argument is optional, and argument is not specified in variables
  • argument is optional, and argument is specified in variables
  • argument is not optional, and argument is not specified in variables
  • argument is not optional, and argument is specified in variables

From the spec: http://facebook.github.io/graphql/October2016/#sec-The-__InputValue-Type
`null` is the correct default value for a variable that is not provided and
does not have its own default value. This will allow clients to pass in
arguments as `undefined` (i.e. omitted from the json variables block)
without subtlely breaking queries.
@oliversong oliversong force-pushed the oliver/thunder-fix-default-types branch from c943689 to d50e086 Compare January 12, 2018 22:33
}

if !reflect.DeepEqual(internal.AsJSON(result), internal.ParseJSON(`
{"optional": -1}`)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I expect an error here, since we passed in nothing, but the variable is required

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you're right. I think I need to make a "mandatory" field that has a matching go function definition

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the fact that this test passes right now implies that we are doing something wrong though - your variable is declared as non null, but its null-ness is never checked before being passed down to the executor.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(i.e. even though none of the users of the argument require it, the query itself should be requiring it.)

builtSchema := schema.MustBuild()

// argument is optional and not passed in
q := graphql.MustParse(`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we clean up the copy/paste in an inner function to make this easier to read?

@oliversong oliversong force-pushed the oliver/thunder-fix-default-types branch 3 times, most recently from d660127 to d71d6fd Compare January 13, 2018 01:06
@oliversong
Copy link
Contributor Author

oliversong commented Jan 17, 2018

Smoke tested. Nothing blew up.

Copy link
Contributor

@stephen stephen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with a nit.

"testArg": float64(5),
}

// arg is optional and passed in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer full sentences (capital/period) in go comments

@@ -36,7 +36,7 @@ func valueToJson(value ast.Value, vars map[string]interface{}) (interface{}, err
case *ast.Variable:
actual, ok := vars[value.Name.Value]
if !ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed irl, there is a new bug introduced in this PR that we think we are okay with ignoring for now.

That is, if a variable is marked optional in the operation definition, we do not check for whether or not it is passed in, which goes against the spec. We think this is okay because if the variable was actually required (i.e. used at some location), then the executor will get unhappy about it downstream.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filed an issue here: #73

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Thanks for writing the issue.

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

Successfully merging this pull request may close these issues.

None yet

2 participants