Skip to content

Commit

Permalink
Update docs for the latest changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalshareef committed Mar 19, 2024
1 parent 8b88031 commit a510290
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/api/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Extends: [`AJVOptions`](https://ajv.js.org/options.html)
* **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.
* **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.
* **directiveValidation** `boolean` (optional, default: `true`) - turn directive validation on or off. It is on by default.
* **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.

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.

Expand Down
22 changes: 22 additions & 0 deletions docs/json-schema-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,28 @@ app.register(mercuriusValidation, {
})
```

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' }`.

```js
app.register(mercuriusValidation, {
schema: {
Filters: {
isAvailable: { type: 'boolean' }
},
Query: {
product: {
id: { type: 'string', minLength: 1 }
}
}
},
customTypeInferenceFn: (type, isNonNull) => {
if (type === GraphQLBoolean) {
return isNonNull ? { type: 'boolean' } : { type: ['boolean', 'null'] }
}
}
})
```

## Caveats

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 comments on commit a510290

Please sign in to comment.