Skip to content

fix(gateway-js): Default variables to an empty object when not provided #167

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

Merged
merged 6 commits into from
Sep 30, 2020

Conversation

cabelitos
Copy link
Contributor

@cabelitos cabelitos commented Sep 21, 2020

Given a query like this:

query MyQuery($arg: Int) {
	myQuery(arg: $arg)
}

In case the arg variable is not provided on
the GraphQL request, even though it's not required,
the Gateway would crash, since it would try
to coearce a nullish variables object.
This behavior is not expected,
since the GraphQL-JS library handle this
nicely (see https://github.com/graphql/graphql-js/blob/d6e760cf7060217bd25fa934bd104ff2400aad96/src/execution/execute.js#L347).
In order to prevent further crashes, a check
was added it will simple provide an empty
object to the getVariablesValues(), just
like the original implementationl.

Closes #196

Given a query like this:

query MyQuery($arg: Int) {
	myQuery(arg: $arg)
}

In case the arg variable is not provided on
the GraphQL request, even though it's not required,
the Gateway would crash, since it would try
to coearce a nullish variables object.
This behavior is not expected behavior,
since the GraphQL-JS library handle this
nicely [1].
In order to prevent further crashes, a check
was added it will simple provide an empty
object to the getVariablesValues(), just
like the original implementationl.

[1]: https://github.com/graphql/graphql-js/blob/d6e760cf7060217bd25fa934bd104ff2400aad96/src/execution/execute.js#L347
@apollo-cla
Copy link

@cabelitos: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@cabelitos
Copy link
Contributor Author

Hey guys, sorry. I did not know that I had to sign the CLA contract. I've just signed.

Copy link

@barbieri barbieri left a comment

Choose a reason for hiding this comment

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

lgtm

@cabelitos
Copy link
Contributor Author

Hey guys, any news on this one?

@abernix abernix self-assigned this Sep 30, 2020
@abernix abernix added this to the @apollo/gateway vNEXT milestone Sep 30, 2020
@abernix abernix changed the title Fix crash when operation variables are not provided fix(gateway-js): Default variables to an empty object when not provided Sep 30, 2020
@abernix
Copy link
Member

abernix commented Sep 30, 2020

Reproduction

Given

type Query {
  plan(id: ID): String
}

and

query Testing($arg:ID) {
  plan(id:$arg)
}

Then

$ curl 'http://endpoint/graphql' \
  -H 'Content-Type: application/json' \
  --data-binary '{
    "query": "query Testing($arg:ID) { plan(id:$arg) }"
  }'

Yields

{"errors":[{"message":"Cannot convert undefined or null to object","extensions":{"code":"INTERNAL_SERVER_ERROR"}}]}

Copy link
Member

@abernix abernix left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks so much for the contribution/fix!

@abernix abernix merged commit c82fe58 into apollographql:main Sep 30, 2020
@cabelitos
Copy link
Contributor Author

@abernix thank you for the support!

@abernix
Copy link
Member

abernix commented Sep 30, 2020

Released in @apollo/[email protected]!

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

Successfully merging this pull request may close these issues.

Internal Server Error when nullable input not provided to federated graph.
5 participants