From 1927becee2ac3c6f4c6ba482fb7416f8cc3bfbc5 Mon Sep 17 00:00:00 2001 From: steffenkoenig Date: Fri, 18 Apr 2025 14:29:43 +0000 Subject: [PATCH] Allow custom formats on schema wrapper --- README.md | 4 +++- index.d.ts | 3 ++- index.js | 4 ++-- lib/type-utils.js | 6 ++++-- test/setup-schema-wrapper.js | 2 +- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 6e7623e..e9c99ec 100644 --- a/README.md +++ b/README.md @@ -427,7 +427,7 @@ Supported formats: - uuid #### Custom Format -You can add your own custom formats by passing a `formats` object to the plugin options. See example below. +You can add your own custom formats by passing a `formats` object to the plugin or wrapper options. See example below. ```@constraint(format: "my-custom-format")``` @@ -441,6 +441,8 @@ const formats = { throw new GraphQLError('Value must be foo') } }; +// Wrapper +constraintDirective({ formats })(schema) // Envelop createEnvelopQueryValidationPlugin({ formats }) diff --git a/index.d.ts b/index.d.ts index cca3f53..e95a411 100644 --- a/index.d.ts +++ b/index.d.ts @@ -14,7 +14,8 @@ export class QueryValidationVisitor { /** * Schema transformer which adds custom types performing validations based on the @constraint directives. */ -export function constraintDirective () : (schema: GraphQLSchema) => GraphQLSchema; +export function constraintDirective (directiveOptions?: directiveOptions) : (schema: GraphQLSchema) => GraphQLSchema; +interface directiveOptions implements pluginOptions {} interface DocumentationOptions { /** Header for the constraints documentation block in the field or argument description */ diff --git a/index.js b/index.js index 82d8312..9beed38 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const { getDirective, mapSchema, MapperKind } = require('@graphql-tools/utils') const { getConstraintTypeObject, getScalarType } = require('./lib/type-utils') const { constraintDirectiveTypeDefs, constraintDirectiveTypeDefsObj } = require('./lib/type-defs') -function constraintDirective () { +function constraintDirective (options = {}) { const constraintTypes = {} function getConstraintType (fieldName, type, notNull, directiveArgumentMap, list, listNotNull) { @@ -43,7 +43,7 @@ function constraintDirective () { const key = Symbol.for(uniqueTypeName) let constraintType = constraintTypes[key] if (constraintType) return constraintType - constraintType = getConstraintTypeObject(fieldName, type, uniqueTypeName, directiveArgumentMap) + constraintType = getConstraintTypeObject(fieldName, type, uniqueTypeName, directiveArgumentMap, options) if (notNull) { constraintType = new GraphQLNonNull(constraintType) } diff --git a/lib/type-utils.js b/lib/type-utils.js index 64f0abc..c606232 100644 --- a/lib/type-utils.js +++ b/lib/type-utils.js @@ -10,13 +10,15 @@ const { const { ConstraintStringType, validate: validateStringFn } = require('../scalars/string') const { ConstraintNumberType, validate: validateNumberFn } = require('../scalars/number') -function getConstraintTypeObject (fieldName, type, uniqueTypeName, directiveArgumentMap) { +function getConstraintTypeObject (fieldName, type, uniqueTypeName, directiveArgumentMap, directiveOptions = {}) { if (type === GraphQLString || type === GraphQLID) { + const options = { pluginOptions: directiveOptions } return new ConstraintStringType( fieldName, uniqueTypeName, type, - directiveArgumentMap + directiveArgumentMap, + options ) } else if (type === GraphQLFloat || type === GraphQLInt) { return new ConstraintNumberType( diff --git a/test/setup-schema-wrapper.js b/test/setup-schema-wrapper.js index 6c81f38..7507ecc 100644 --- a/test/setup-schema-wrapper.js +++ b/test/setup-schema-wrapper.js @@ -14,7 +14,7 @@ module.exports = async function ({ typeDefs, formatError, resolvers, schemaCreat schema = schemaCreatedCallback(schema) } - schema = constraintDirective()(schema) + schema = constraintDirective(pluginOptions)(schema) const app = express() const server = new ApolloServer({