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

Indicate errors in User Input #95

Open
oporkka opened this issue Jan 13, 2021 · 1 comment
Open

Indicate errors in User Input #95

oporkka opened this issue Jan 13, 2021 · 1 comment

Comments

@oporkka
Copy link
Member

oporkka commented Jan 13, 2021

We are currently having problem that we get errors from invalid variables passed to the queries, and we tag them as server side errors instead of user errors, and this causes unnecessary errors in the monitoring / tracing and eventually even paging of the on-call duty.

Examples

    type Query{
      parameterField(param: String!): String
    }

    type Mutation {
      inputMutation(input: TestInput!): String
    }

    input TestInput { id: ID! }

Example 1

Calling

query paramQuery($param: String!) { parameterField(param: $param) } 

without variable results in:

Variable "$param" of required type "String!" was not provided.

Example 2

Calling

mutation M($input: TestInput!) { inputMutation(input: $input) } 

with

{ "input": {} }

causes

Variable "$input" got invalid value {}; Field value.id of required type ID! was not provided.

Workaround

We can currently only parse this kind of errors from the GraphQL error message. It would be helpful to have indication for user error type.

Possible solution

The thrown errors have a field or other clear mechanism to indicate that the error is a user error.

@boopathi
Copy link
Member

I think this is solvable in user land as GraphQL JIT does not run the GraphQL validation. We can add error codes for compilation errors - but if the purpose of this would be to only detect if compilation errors are present, then it's best left to the project using JIT. For example, in the pipeline, you can simply attach error code after each step -

const {query, variables} = request.body;
let document;
try {
  document = parse(query)
} catch (e) {
  return response400(e.message);
}


const validationErrors = validate(schema, document, variables);
if (validationErrors) {
  attachErrorCode(validationErrors, "VALIDATION_ERROR");
  return response400(validationErrors);
}


const compilationResult = compileQuery(schema, document)
if (!isCompiledQuery(compilationResult)) {
  attachErrorCode(compilationResult, "COMPILATION_FAILURE");
  return response400(validationErrors);
}

const result = executeQuery(schema, compilationResult, variables);
return result

and error code enricher would simply be something like -

function attachErrorCode(result, code) {
  for (const error of result.errors) {
    error.extensions = { code };
  }
}

These error codes can then be inspected for tracing / other monitoring signals.

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