Apollo-server: input coercion errors should be ValidationErrors

Created on 17 Sep 2019  Β·  2Comments  Β·  Source: apollographql/apollo-server

If I make a request that is illegal because the input arguments/variables are the wrong type, I would expect Apollo to emit a ValidationError. Instead, the more generic GraphQLError is thrown.

A very naive example:

This Query:

query UserQuery($userId: Int!) {
  user(userId: $userId) {
    firstName
  }
}

with the variables:

{ "userId": null }

produces an error object (passed to formatError) that looks something like:

{
  "message":  "Variable \"$userId\" of non-null type \"Int!\" must not be null.",
  "name":"GraphQLError"
}

Because these errors are typically the result of a bad client request, I would like to have some sort of mechanism where I can ensure that they are logged with a lower severity (compared to an actual execution error).

error-handling

Most helpful comment

The coercion process is called by graphql/execution/values.js
which is in turn called by graphql/execution/execute.js

I wasn't able to locate a good point where Apollo could unambiguously intercept these errors πŸ˜•and convert them to ValidationErrors – I wonder if this is a change that would need to be made in graphql-js itself....

All 2 comments

The coercion process is called by graphql/execution/values.js
which is in turn called by graphql/execution/execute.js

I wasn't able to locate a good point where Apollo could unambiguously intercept these errors πŸ˜•and convert them to ValidationErrors – I wonder if this is a change that would need to be made in graphql-js itself....

@schmod I mentioned a possible (but really ugly) fix here: https://github.com/apollographql/apollo-server/issues/3498#issuecomment-554966929

Is there currently any discussion ongoing on changing this behaviour you mentioned? Would this involve changing the GraphQL spec or is this something that must be handled inside the implementations (such as graphql-js)?

Was this page helpful?
0 / 5 - 0 ratings