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

Improve types for useErrorHandler plugin #2068

Open
andrew0 opened this issue Nov 7, 2023 · 0 comments
Open

Improve types for useErrorHandler plugin #2068

andrew0 opened this issue Nov 7, 2023 · 0 comments
Labels
kind/enhancement New feature or request

Comments

@andrew0
Copy link

andrew0 commented Nov 7, 2023

Is your feature request related to a problem? Please describe.

The context property passed as the payload to useErrorHandler callbacks is typed as Readonly<DefaultContext>:

context: Readonly<DefaultContext>;

I'm trying to use this plugin and this type doesn't seem to be very useful. Given that the useErrorHandler function takes a generic type for the context, I would expect the ErrorHandler type to also be generic instead of just using DefaultContext.

Additionally, depending on the phase, it seems like the type is actually different. The context value is passed to the error handler callback in the parse and validate phases, but in the execution phase it's passing a TypedExecutionArgs<ContextType>:

errorHandler({ errors: result.errors, context: args, phase: 'execution' });
. So even though the type of context is Readonly<DefaultContext>, I need to look in context.contextValue (untyped) to get the same context value as the parse and validate phases.

And in the "context" phase, the context value passed to the error handler callback is a OnPluginInitEventPayload<ContextType>. I don't really understand why this is being passed to error handlers - it would make more sense to me if the context value was pulled from this callback instead:

context.registerContextErrorHandler(({ error }) => {

But I'm not sure if it's intentional that the "onPluginInit" payload is being used instead.

Describe the solution you'd like

Make the ErrorHandler type a generic type with a tagged union:

export type ErrorHandler<ContextType extends Record<string, any> = DefaultContext> = ({
  errors,
  context,
  phase,
}: {
  errors: readonly Error[] | readonly SerializableGraphQLErrorLike[];
} & (
  | {
      context: Readonly<ContextType>;
      phase: 'parse' | 'validate';
    }
  | {
      context: OnPluginInitEventPayload<ContextType>;
      phase: 'context';
    }
  | {
      context: TypedExecutionArgs<ContextType>;
      phase: 'execution';
    }
)) => void;

Describe alternatives you've considered

In my useErrorHandler plugin, explicitly cast the type of context based on the phase.

Additional context

@EmrysMyrddin EmrysMyrddin added the kind/enhancement New feature or request label Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants