graphile / crystal

🔮 Graphile's Crystal Monorepo; home to Grafast, PostGraphile, pg-introspection, pg-sql2 and much more!
https://graphile.org/
Other
12.63k stars 572 forks source link

Improved error messages when getting an absent field from fieldArgs #1812

Open wesselvdv opened 1 year ago

wesselvdv commented 1 year ago

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

benjie commented 1 year ago

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?

wesselvdv commented 1 year ago

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"]),
      },
    },
  }
})