Skip to content

Commit

Permalink
fix: specify type better for context arg in defaultErrorFormatter (#690)
Browse files Browse the repository at this point in the history
* fix: specify type better for context 

When context is null for example, it crashes with a type error
```
TypeError: Cannot read property 'reply' of null
    at AsyncFunction.defaultErrorFormatter (/home/capaj/work-repos/authier-repos/authier/node_modules/mercurius/lib/errors.js:67:19)
```

* test: add specs for new types
  • Loading branch information
capaj committed Dec 22, 2021
1 parent c0c0dd3 commit 0ace215
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Expand Up @@ -470,7 +470,7 @@ export interface MercuriusCommonOptions {
/**
* Change the default error formatter.
*/
errorFormatter?: <TContext extends Record<string,any> = MercuriusContext>(
errorFormatter?: <TContext extends MercuriusContext = MercuriusContext>(
execution: ExecutionResult,
context: TContext
) => {
Expand Down Expand Up @@ -614,7 +614,7 @@ declare namespace mercurius {
*/
const defaultErrorFormatter: (
execution: ExecutionResult,
context: any
context: MercuriusContext
) => { statusCode: number; response: ExecutionResult };

/**
Expand Down
10 changes: 10 additions & 0 deletions test/types/index.ts
Expand Up @@ -629,3 +629,13 @@ declare module 'fastify' {
graphql: MercuriusPlugin
}
}

mercurius.defaultErrorFormatter({}, {} as MercuriusContext)

expectError(() => {
return mercurius.defaultErrorFormatter({}, null)
})

expectError(() => {
return mercurius.defaultErrorFormatter({}, undefined)
})

0 comments on commit 0ace215

Please sign in to comment.