mercurius
Advanced tools
Comparing version 8.5.0 to 8.6.0
# mercurius | ||
- [Plugin options](#plugin-options) | ||
- [HTTP endpoints](#http-endpoints) | ||
- [GET /graphql](#get-graphql) | ||
- [POST /graphql](#post-graphql) | ||
- [POST /graphql with Content-type: application/graphql](#post-graphql-with-content-type-applicationgraphql) | ||
- [GET /graphiql](#get-graphiql) | ||
- [Decorators](#decorators) | ||
- [app.graphql(source, context, variables, operationName)](#appgraphqlsource-context-variables-operationname) | ||
- [app.graphql.extendSchema(schema), app.graphql.defineResolvers(resolvers) and app.graphql.defineLoaders(loaders)](#appgraphqlextendschemaschema-appgraphqldefineresolversresolvers-and-appgraphqldefineloadersloaders) | ||
- [app.graphql.replaceSchema(schema)](#appgraphqlreplaceschemaschema) | ||
- [app.graphql.transformSchema(transforms)](#appgraphqltransformschematransforms) | ||
- [app.graphql.schema](#appgraphqlschema) | ||
- [reply.graphql(source, context, variables, operationName)](#replygraphqlsource-context-variables-operationname) | ||
- [Error extensions](#use-errors-extension-to-provide-additional-information-to-query-errors) | ||
- [mercurius](#mercurius) | ||
- [API](#api) | ||
- [Plugin options](#plugin-options) | ||
- [queryDepth example](#querydepth-example) | ||
- [HTTP endpoints](#http-endpoints) | ||
- [GET /graphql](#get-graphql) | ||
- [POST /graphql](#post-graphql) | ||
- [POST /graphql with Content-type: application/graphql](#post-graphql-with-content-type-applicationgraphql) | ||
- [GET /graphiql](#get-graphiql) | ||
- [Decorators](#decorators) | ||
- [app.graphql(source, context, variables, operationName)](#appgraphqlsource-context-variables-operationname) | ||
- [app.graphql.extendSchema(schema), app.graphql.defineResolvers(resolvers) and app.graphql.defineLoaders(loaders)](#appgraphqlextendschemaschema-appgraphqldefineresolversresolvers-and-appgraphqldefineloadersloaders) | ||
- [app.graphql.replaceSchema(schema)](#appgraphqlreplaceschemaschema) | ||
- [app.graphql.transformSchema(transforms)](#appgraphqltransformschematransforms) | ||
- [app.graphql.schema](#appgraphqlschema) | ||
- [reply.graphql(source, context, variables, operationName)](#replygraphqlsource-context-variables-operationname) | ||
- [Errors](#errors) | ||
- [ErrorWithProps](#errorwithprops) | ||
- [Extensions](#extensions) | ||
- [Status code](#status-code) | ||
- [Error formatter](#error-formatter) | ||
## API | ||
@@ -442,4 +448,30 @@ | ||
### Use errors extension to provide additional information to query errors | ||
### Errors | ||
Mercurius help the error handling with two useful tools. | ||
- ErrorWithProps class | ||
- ErrorFormatter option | ||
### ErrorWithProps | ||
ErrorWithProps can be used to create Errors to be thrown inside the resolvers or plugins. | ||
it takes 3 parameters: | ||
- message | ||
- extensions | ||
- statusCode | ||
```js | ||
'use strict' | ||
throw new ErrorWithProps('message', { | ||
... | ||
}, 200) | ||
``` | ||
#### Extensions | ||
Use errors `extensions` to provide additional information to query errors | ||
GraphQL services may provide an additional entry to errors with the key `extensions` in the result. | ||
@@ -499,1 +531,29 @@ | ||
``` | ||
#### Status code | ||
To control the status code for the response, the third optional parameter can be used. | ||
```js | ||
throw new mercurius.ErrorWithProps('Invalid User ID', {moreErrorInfo}) | ||
// using de defaultErrorFormatter the response statusCode will be 500 | ||
throw new mercurius.ErrorWithProps('Invalid User ID', {moreErrorInfo}, 200) | ||
// using de defaultErrorFormatter the response statusCode will be 200 | ||
const error = new mercurius.ErrorWithProps('Invalid User ID', {moreErrorInfo}, 500) | ||
error.data = {foo: 'bar'} | ||
throw error | ||
// using de defaultErrorFormatter the response status code will be always 200 because error.data is defined | ||
``` | ||
### Error formatter | ||
Allows the status code of the response to be set, and a GraphQL response for the error to be defined. | ||
By default uses the defaultErrorFormatter, but it can be overridden in the [mercurius options](/docs/api/options.md#plugin-options) changing the errorFormatter parameter. | ||
**Important**: *using the default formatter, when the error has a data property the response status code will be always 200* |
@@ -595,3 +595,3 @@ import { | ||
class ErrorWithProps extends Error { | ||
constructor(message: string, extensions?: object); | ||
constructor(message: string, extensions?: object, statusCode?: number); | ||
/** | ||
@@ -601,2 +601,3 @@ * Custom additional properties of this error | ||
extensions?: object; | ||
statusCode?: number; | ||
} | ||
@@ -603,0 +604,0 @@ |
@@ -7,5 +7,6 @@ 'use strict' | ||
class ErrorWithProps extends Error { | ||
constructor (message, extensions) { | ||
constructor (message, extensions, statusCode) { | ||
super(message) | ||
this.extensions = extensions | ||
this.statusCode = statusCode || 500 | ||
} | ||
@@ -12,0 +13,0 @@ } |
{ | ||
"name": "mercurius", | ||
"version": "8.5.0", | ||
"version": "8.6.0", | ||
"description": "Fastify GraphQL adapter with gateway and subscription support", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,2 +10,7 @@ 'use strict' | ||
test('ErrorWithProps - support status code in the constructor', async (t) => { | ||
const error = new ErrorWithProps('error', { }, 500) | ||
t.equal(error.statusCode, 500) | ||
}) | ||
test('errors - multiple extended errors', async (t) => { | ||
@@ -730,1 +735,37 @@ const schema = ` | ||
}) | ||
test('errors - should override statusCode to 200 if the data is present', async (t) => { | ||
const schema = ` | ||
type Query { | ||
error: String | ||
successful: String | ||
} | ||
` | ||
const resolvers = { | ||
Query: { | ||
error () { | ||
throw new ErrorWithProps('Error', undefined, 500) | ||
}, | ||
successful () { | ||
return 'Runs OK' | ||
} | ||
} | ||
} | ||
const app = Fastify() | ||
app.register(GQL, { | ||
schema, | ||
resolvers | ||
}) | ||
await app.ready() | ||
const res = await app.inject({ | ||
method: 'GET', | ||
url: '/graphql?query={error,successful}' | ||
}) | ||
t.equal(res.statusCode, 200) | ||
}) |
@@ -1,2 +0,2 @@ | ||
import { expectAssignable } from 'tsd' | ||
import { expectAssignable, expectError } from 'tsd' | ||
/* eslint-disable no-unused-expressions */ | ||
@@ -596,1 +596,7 @@ import { EventEmitter } from 'events' | ||
}) | ||
expectError(() => { | ||
return new mercurius.ErrorWithProps('mess', {}, 'wrong statusCode') | ||
}) | ||
expectAssignable<Error>(new mercurius.ErrorWithProps('mess', {}, 200)) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
784688
27559