|
| 1 | +import { expectType } from 'tsd' |
| 2 | +import fastify from 'fastify' |
| 3 | +import { GraphQLResolveInfo } from 'graphql' |
| 4 | +import { MercuriusContext } from 'mercurius' |
| 5 | +import mercuriusValidation, { MercuriusValidationHandler, MercuriusValidationHandlerMetadata, MercuriusValidationOptions } from '../..' |
| 6 | + |
| 7 | +const app = fastify() |
| 8 | + |
| 9 | +// Register without options |
| 10 | +app.register(mercuriusValidation) |
| 11 | +app.register(mercuriusValidation, {}) |
| 12 | + |
| 13 | +// Register with AJV options |
| 14 | +app.register(mercuriusValidation, { |
| 15 | + coerceTypes: false |
| 16 | +}) |
| 17 | + |
| 18 | +// Use different modes |
| 19 | +app.register(mercuriusValidation, { mode: 'JTD' }) |
| 20 | +app.register(mercuriusValidation, { mode: 'JSONSchema' }) |
| 21 | + |
| 22 | +// JSON Schema |
| 23 | +app.register(mercuriusValidation, { |
| 24 | + mode: 'JSONSchema', |
| 25 | + schema: { |
| 26 | + Filters: { |
| 27 | + text: { minLength: 1 } |
| 28 | + }, |
| 29 | + Query: { |
| 30 | + message: { |
| 31 | + id: { |
| 32 | + minLength: 1 |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | +}) |
| 38 | + |
| 39 | +// JTD |
| 40 | +app.register(mercuriusValidation, { |
| 41 | + mode: 'JTD', |
| 42 | + schema: { |
| 43 | + Filters: { |
| 44 | + text: { enum: ['hello', 'there'] } |
| 45 | + }, |
| 46 | + Query: { |
| 47 | + message: { |
| 48 | + id: { |
| 49 | + type: 'uint8' |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | +}) |
| 55 | + |
| 56 | +// Function |
| 57 | +app.register(mercuriusValidation, { |
| 58 | + schema: { |
| 59 | + Query: { |
| 60 | + message: { |
| 61 | + async id (metadata, value, parent, args, context, info) { |
| 62 | + expectType<MercuriusValidationHandlerMetadata>(metadata) |
| 63 | + expectType<any>(value) |
| 64 | + expectType<any>(parent) |
| 65 | + expectType<any>(args) |
| 66 | + expectType<MercuriusContext>(context) |
| 67 | + expectType<GraphQLResolveInfo>(info) |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +}) |
| 73 | + |
| 74 | +// Using options as object without generic |
| 75 | +interface CustomParent { |
| 76 | + parent: Record<string, any>; |
| 77 | +} |
| 78 | + |
| 79 | +interface CustomArgs { |
| 80 | + arg: Record<string, any>; |
| 81 | +} |
| 82 | + |
| 83 | +interface CustomContext extends MercuriusContext { |
| 84 | + hello?: string; |
| 85 | +} |
| 86 | + |
| 87 | +const validationOptions: MercuriusValidationOptions = { |
| 88 | + schema: { |
| 89 | + Query: { |
| 90 | + message: { |
| 91 | + async id ( |
| 92 | + metadata, |
| 93 | + value, |
| 94 | + parent: CustomParent, |
| 95 | + args: CustomArgs, |
| 96 | + context: CustomContext, |
| 97 | + info |
| 98 | + ) { |
| 99 | + expectType<MercuriusValidationHandlerMetadata>(metadata) |
| 100 | + expectType<any>(value) |
| 101 | + expectType<CustomParent>(parent) |
| 102 | + expectType<CustomArgs>(args) |
| 103 | + expectType<CustomContext>(context) |
| 104 | + expectType<string | undefined>(context?.hello) |
| 105 | + expectType<GraphQLResolveInfo>(info) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +app.register(mercuriusValidation, validationOptions) |
| 113 | + |
| 114 | +// 3. Using options as object with generics |
| 115 | +const authOptionsWithGenerics: MercuriusValidationOptions<CustomParent, CustomArgs, CustomContext> = { |
| 116 | + schema: { |
| 117 | + Query: { |
| 118 | + message: { |
| 119 | + async id (metadata, value, parent, args, context, info) { |
| 120 | + expectType<MercuriusValidationHandlerMetadata>(metadata) |
| 121 | + expectType<any>(value) |
| 122 | + expectType<CustomParent>(parent) |
| 123 | + expectType<CustomArgs>(args) |
| 124 | + expectType<CustomContext>(context) |
| 125 | + expectType<string | undefined>(context?.hello) |
| 126 | + expectType<GraphQLResolveInfo>(info) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +app.register(mercuriusValidation, authOptionsWithGenerics) |
| 134 | + |
| 135 | +// 4. Creating functions using handler types |
| 136 | +const id: MercuriusValidationHandler<{}, {}, CustomContext> = |
| 137 | + async (metadata, value, parent, args, context, info) => { |
| 138 | + expectType<MercuriusValidationHandlerMetadata>(metadata) |
| 139 | + expectType<any>(value) |
| 140 | + expectType<{}>(parent) |
| 141 | + expectType<{}>(args) |
| 142 | + expectType<CustomContext>(context) |
| 143 | + expectType<GraphQLResolveInfo>(info) |
| 144 | + expectType<string | undefined>(context?.hello) |
| 145 | + } |
| 146 | + |
| 147 | +app.register(mercuriusValidation, { |
| 148 | + schema: { |
| 149 | + Query: { |
| 150 | + message: { |
| 151 | + id |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | +}) |
0 commit comments