在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):confuser/graphql-constraint-directive开源软件地址(OpenSource Url):https://github.com/confuser/graphql-constraint-directive开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):graphql-constraint-directiveAllows using @constraint as a directive to validate input data. Inspired by Constraints Directives RFC and OpenAPI Install
For GraphQL v15 and below, use v2 of this package
Usageconst { constraintDirective, constraintDirectiveTypeDefs } = require('graphql-constraint-directive')
const express = require('express')
const { ApolloServer } = require('apollo-server-express')
const { makeExecutableSchema } = require('@graphql-tools/schema')
const typeDefs = `
type Query {
books: [Book]
}
type Book {
title: String
}
type Mutation {
createBook(input: BookInput): Book
}
input BookInput {
title: String! @constraint(minLength: 5, format: "email")
}`
let schema = makeExecutableSchema({
typeDefs: [constraintDirectiveTypeDefs, typeDefs],
})
schema = constraintDirective()(schema)
const app = express()
const server = new ApolloServer({ schema })
await server.start()
server.applyMiddleware({ app }) APIStringminLength
maxLength
startsWith
endsWith
contains
notContains
pattern
format
Supported formats:
Int/Floatmin
max
exclusiveMin
exclusiveMax
multipleOf
ConstraintDirectiveErrorEach validation error throws a {
code: 'ERR_GRAPHQL_CONSTRAINT_VALIDATION',
fieldName: 'theFieldName',
context: [ { arg: 'argument name which failed', value: 'value of argument' } ]
} const formatError = function (error) {
const code = error?.originalError?.originalError?.code || error?.originalError?.code || error?.code
if (code === 'ERR_GRAPHQL_CONSTRAINT_VALIDATION') {
// return a custom object
}
return error
}
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema, formatError })) uniqueTypeName
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论