Skip to content

Commit a510290

Browse files
committed
Update docs for the latest changes
1 parent 8b88031 commit a510290

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

docs/api/options.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Extends: [`AJVOptions`](https://ajv.js.org/options.html)
1111
* **mode** `"JSONSchema" | "JTD"` (optional, default: `"JSONSchema"`) - the validation mode of the plugin. This is used to specify the type of schema that needs to be compiled.
1212
* **schema** `MercuriusValidationSchema` (optional) - the validation schema definition that the plugin with run. One can define JSON Schema or JTD definitions for GraphQL types, fields and arguments or functions for GraphQL arguments.
1313
* **directiveValidation** `boolean` (optional, default: `true`) - turn directive validation on or off. It is on by default.
14+
* **customTypeInferenceFn** `Function` (optional) - add custom type inference for JSON Schema Types. This function overrides the default type inference logic which infers GraphQL primitives like `GraphQLString`, `GraphQLInt` and `GraphQLFloat`. If the custom function doesn't handle the passed type, then it should return a falsy value which will trigger the default type inference logic of the plugin. This function takes two parameters. The first parameter is `type` referring to the GraphQL type under inference, while the second one is `isNonNull`, a boolean value referring whether the value for the type is nullable.
1415

1516
It extends the [AJV options](https://ajv.js.org/options.html). These can be used to register additional `formats` for example and provide further customization to the AJV validation behavior.
1617

docs/json-schema-validation.md

+22
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,28 @@ app.register(mercuriusValidation, {
478478
})
479479
```
480480

481+
The type inference is customizable. You can pass `customTypeInferenceFn` in the plugin options and have your own inference logic inside the function. The below code is an example for custom type inference for `GraphQLBoolean` <=> `{ type: 'boolean' }`.
482+
483+
```js
484+
app.register(mercuriusValidation, {
485+
schema: {
486+
Filters: {
487+
isAvailable: { type: 'boolean' }
488+
},
489+
Query: {
490+
product: {
491+
id: { type: 'string', minLength: 1 }
492+
}
493+
}
494+
},
495+
customTypeInferenceFn: (type, isNonNull) => {
496+
if (type === GraphQLBoolean) {
497+
return isNonNull ? { type: 'boolean' } : { type: ['boolean', 'null'] }
498+
}
499+
}
500+
})
501+
```
502+
481503
## Caveats
482504

483505
The use of the `$ref` keyword is not advised because we use this through the plugin to build up the GraphQL type validation. However, we have not prevented use of this keyword since it may be useful in some situations.

0 commit comments

Comments
 (0)