crystal icon indicating copy to clipboard operation
crystal copied to clipboard

Improved error messages when getting an absent field from fieldArgs

Open wesselvdv opened this issue 2 years ago • 2 comments

Summary

Getting an incorrect field from a mutation FieldArgs results in a unable to get type from undefined error.

Steps to reproduce

Try and get an absent field from a FieldArgs

Expected results

More verbose error message clearly indicating that you're trying to get a field that doesn't exist.

Actual results

Getting a can't read type from undefined error.

Additional context

Possible Solution

wesselvdv avatar Oct 13 '23 09:10 wesselvdv

Could you share some code about what you mean, do you mean fieldArgs.get("<unknown>") or fieldArgs.$unknown or...? What do you mean by a "mutation FieldArgs" - the FieldArgs of a mutation field's plan resolver?

benjie avatar Oct 13 '23 10:10 benjie

Could you share some code about what you mean, do you mean fieldArgs.get("<unknown>") or fieldArgs.$unknown or...? What do you mean by a "mutation FieldArgs" - the FieldArgs of a mutation field's plan resolver?

import { gql, makeExtendSchemaPlugin } from "postgraphile/utils"

export const MutationErrorPlugin = makeExtendSchemaPlugin(() => {
  return {
    typeDefs: gql`
      input Input {
        id: UUID!
      }

      type Payload {
        query: Query
      }

      extend type Mutation {
        doSomething(input: Input!): Payload!
      }
    `,
    plans: {
      Mutation: {
        doSomething: (_, fieldArgs) =>
          // error because ids doesn't exist
          fieldArgs.get(["input", "ids"]),
      },
    },
  }
})

wesselvdv avatar Oct 13 '23 11:10 wesselvdv