Skip to content

Inconsistent response between required and optional fields #7235

@AndreasHald

Description

@AndreasHald

Issue Description

Querying fields that is required / not required provides inconsistent responses.

Consider the repro - there are 2 fields that can be queried willFailFieldIsRequired and willFailFieldIsNotRequired both return a string and the only difference is that one field is required and the other is not. the resolvers for both fields throws an error meaning that both will fail. However the shape of the response differs.

willFailFieldIsRequired returns the expected errors array with the data property as null

  errors: [
    {
      message: 'something went wrong',
      locations: [Array],
      path: [Array],
      extensions: [Object]
    }
  ],
  data: null
}

willFailFieldIsNotRequired returns the expected errors array, but the data property is an object with the key willFailFieldIsNotRequired as null.

{
  errors: [
    {
      message: 'something went wrong',
      locations: [Array],
      path: [Array],
      extensions: [Object]
    }
  ],
  data: { willFailFieldIsNotRequired: null }
}

Other than the fact that this is inconsistent it causes issues with apollo client, because querying a field that is required if the resolver throws an error the Apollo client fails with null is not an object (evaluating 'rootValue[aliasedFieldName]') because the clients expects the aliasedFieldName to be present within data. And it only is if the field is optional.

This essentially means it is impossible to have any required field be able to throw an error.

Link to Reproduction

https://stackblitz.com/edit/node-datsna?file=index.mjs

Reproduction Steps

  • Open link
  • npm start
  • check logs

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions