Skip to content

Allow custom formats on schema wrapper #297

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")```

Expand All @@ -441,6 +441,8 @@ const formats = {
throw new GraphQLError('Value must be foo')
}
};
// Wrapper
constraintDirective({ formats })(schema)

// Envelop
createEnvelopQueryValidationPlugin({ formats })
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 4 additions & 2 deletions lib/type-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion test/setup-schema-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down