|
| 1 | +# mercurius-validation |
| 2 | + |
| 3 | +- [Plugin options](#plugin-options) |
| 4 | +- [Registration](#registration) |
| 5 | + |
| 6 | +## Plugin options |
| 7 | + |
| 8 | +<!-- TODO --> |
| 9 | +**mercurius-validation** supports the following options: |
| 10 | + |
| 11 | +Extends: [`Options`](https://ajv.js.org/options.html) |
| 12 | + |
| 13 | +* **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. |
| 14 | +* **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. |
| 15 | + |
| 16 | +It extends the [AJV options](https://ajv.js.org/options.html). These can be used to register additional `formats` and provide further customization to the AJV validation behavior for example. |
| 17 | + |
| 18 | +### Parameter: `MercuriusValidationSchema` |
| 19 | + |
| 20 | +Extends: `Record<string, MercuriusValidationSchemaType>` |
| 21 | + |
| 22 | +Each key within the `MercuriusValidationSchema` type corresponds with the GraphQL type name. For example, if we wanted validation on input type: |
| 23 | + |
| 24 | +```gql |
| 25 | +input Filters { |
| 26 | + ... |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +We would use the key: `Filters`: |
| 31 | + |
| 32 | +```js |
| 33 | +{ |
| 34 | + Filters: { ... } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +### Parameter: `MercuriusValidationSchemaType` |
| 39 | + |
| 40 | +Extends: `Record<string, MercuriusValidationSchemaField>` |
| 41 | + |
| 42 | +* **__typeValidation** `JSONSchema | JTD` (optional) - The [JSON Schema](https://json-schema.org/understanding-json-schema/) or [JTD](https://jsontypedef.com/docs/) schema definitions for the type. This is only applicable to GraphQL Input object types, so only schema definitions for `object` are applicable here. |
| 43 | + |
| 44 | +Each key within the `MercuriusValidationSchemaType` type corresponds with the GraphQL field name on a type. For example, if we wanted validation on type field `text`: |
| 45 | + |
| 46 | +```gql |
| 47 | +input Filters { |
| 48 | + id: ID |
| 49 | + text: String |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +We would use the key: `text`: |
| 54 | + |
| 55 | +```js |
| 56 | +{ |
| 57 | + Filters: { |
| 58 | + text: { ... } |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### Parameter: `MercuriusValidationSchemaField` |
| 64 | + |
| 65 | +The field definition is different from GraphQL Input Object types and GraphQL Object types. |
| 66 | + |
| 67 | +#### GraphQL Input Object Types |
| 68 | + |
| 69 | +Union: `JSONSchema | JTD` |
| 70 | + |
| 71 | +#### GraphQL Object Types |
| 72 | + |
| 73 | +Extends: `Record<string, MercuriusValidationSchemaArgument>` |
| 74 | + |
| 75 | +Each key within the `MercuriusValidationSchemaField` type corresponds with the GraphQL argument name on a field. For example, if we wanted validation on field argument `id`: |
| 76 | + |
| 77 | +```gql |
| 78 | +type Query { |
| 79 | + message(id: ID): String |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +We would use the key: `id`: |
| 84 | + |
| 85 | +```js |
| 86 | +{ |
| 87 | + Query: { |
| 88 | + message: { |
| 89 | + id: {... } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### Parameter: `MercuriusValidationSchemaArgument` |
| 96 | + |
| 97 | +Union: `JSONSchema | JTD` | `MercuriusValidationFunction` |
| 98 | + |
| 99 | +### Parameter: `MercuriusValidationFunction(metadata, value, parent, arguments, context, info)` |
| 100 | + |
| 101 | +Arguments: |
| 102 | + |
| 103 | +* **metadata** `MercuriusValidationFunctionMetadata` - the GraphQL argument metadata associated with the function definition. |
| 104 | +* **value** `any` - the value of the argument. |
| 105 | +* **parent** `object` - the parent data associated with the GraphQL field. |
| 106 | +* **arguments** `object` - the key value object of the GraphQL field arguments. |
| 107 | +* **context** `MercuriusContext` - the [Mercurius context](https://mercurius.dev/#/docs/context). |
| 108 | +* **info** `GraphQLResolveInfo` - the [GraphQL Resolve info](https://graphql.org/graphql-js/type/#graphqlobjecttype) of the object type. |
| 109 | + |
| 110 | +#### Parameter: `MercuriusValidationFunctionMetadata` |
| 111 | + |
| 112 | +* **type** `string` - the name of the GraphQL type. |
| 113 | +* **field** `string` - the name of the GraphQL field. |
| 114 | +* **argument** `string` - the name of the GraphQL argument. |
| 115 | + |
| 116 | +Returns: `void` |
| 117 | + |
| 118 | +### Parameter: `JSONSchema` |
| 119 | + |
| 120 | +The [JSON Schema](https://json-schema.org/understanding-json-schema/) schema definition for the input object type. |
| 121 | + |
| 122 | +### Parameter: `JTD` |
| 123 | + |
| 124 | +The [JTD](https://jsontypedef.com/docs/) schema definition for the input object type field. |
| 125 | + |
| 126 | +## Registration |
| 127 | + |
| 128 | +The plugin must be registered **after** Mercurius is registered. |
| 129 | + |
| 130 | +```js |
| 131 | +'use strict' |
| 132 | + |
| 133 | +const Fastify = require('fastify') |
| 134 | +const mercurius = require('mercurius') |
| 135 | +const mercuriusValidation = require('mercurius-validation') |
| 136 | + |
| 137 | +const app = Fastify() |
| 138 | + |
| 139 | +const schema = ` |
| 140 | + directive @validation( |
| 141 | + requires: Role = ADMIN, |
| 142 | + ) on OBJECT | FIELD_DEFINITION |
| 143 | +
|
| 144 | + enum Role { |
| 145 | + ADMIN |
| 146 | + REVIEWER |
| 147 | + USER |
| 148 | + UNKNOWN |
| 149 | + } |
| 150 | +
|
| 151 | + type Query { |
| 152 | + add(x: Int, y: Int): Int @validation(requires: USER) |
| 153 | + } |
| 154 | +` |
| 155 | + |
| 156 | +const resolvers = { |
| 157 | + Query: { |
| 158 | + add: async (_, { x, y }) => x + y |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +app.register(mercurius, { |
| 163 | + schema, |
| 164 | + resolvers |
| 165 | +}) |
| 166 | + |
| 167 | +app.register(mercuriusValidation, { |
| 168 | + validationContext (context) { |
| 169 | + return { |
| 170 | + identity: context.reply.request.headers['x-user'] |
| 171 | + } |
| 172 | + }, |
| 173 | + async applyPolicy (validationDirectiveAST, parent, args, context, info) { |
| 174 | + return context.validation.identity === 'admin' |
| 175 | + }, |
| 176 | + validationDirective: 'validation' |
| 177 | +}) |
| 178 | + |
| 179 | +app.listen(3000) |
| 180 | +``` |
0 commit comments