vsimko / node-graphql-constraint-lambda Goto Github PK
View Code? Open in Web Editor NEWGraphQL 'constraint' directive written in functional programming style
License: MIT License
GraphQL 'constraint' directive written in functional programming style
License: MIT License
something like this
class ConstraintDirectiveError extends Error {
/** @param {string[]} errors */
constructor (errors) {
super("Validation errors")
this.errors = errors
}
}
I've followed the link from prisma/prisma#728 and this package looks useful indeed.
A couple of questions though:
With my usecase it is required to customize the error messages with certain fields. E.g. field primaryEmail
should have another error message compared with secondaryEmail
. So I need to define the error message per field. Maybe this could be implemented with a future version.
Depending on the project, a validation with contains
could require a very specific error message as well.
Validate array argument. Something like:
type Mutation {
updateUser(
id: ID!
name: String
emails: [String!]! @constraint(
minItems: 1,
maxItems: 3,
uniqueItems: true,
format: "email" #applies to all items of array?
)
)
}
Validate object argument (useful when argument is an input type). Something like:
type Mutation {
updateUser(
data: UserUpdateInput! @constraint(
name: { minLength: 5, maxLength: 40 },
email: { format: "email" },
age: { min: 18, max: 100 }
)
where: UserWhereUniqueInput!
): User!
}
input UserUpdateInput {
name: String
email: String
age: Int
verified: Boolean
}
Currently, @constraint
supports only ARGUMENT_DEFINITION
directive location.
Problem: e.g. Prisma extracts all query/mutation arguments to separate input types when generates schema from datamodel.
Please add support of INPUT_FIELD_DEFINITION
location to be able to apply @constraint
to input type fields. E.g.:
type User {
name: String
phone: String
email: String
...
}
...
type Mutation {
createUser(data: UserCreateInput!): User!
updateUser(data: UserUpdateInput!, where: UserWhereUniqueInput!): User!
deleteUser(where: UserWhereUniqueInput!): User
...
}
...
input UserUpdateInput {
name: String
phone: String
email: String @constraint(format:"email")
...
}
for "graphql"
Please add support of logical (boolean) operations (AND
, OR
and NOT
) for combining rules inside directive.
I'm personally interested in OR
combinator.
See the rationale, use cases, syntax ideas, etc. here: confuser/graphql-constraint-directive#13.
Seem not work with apollo server 2.x
I config like document but not return any error
schema.graphql
directive @constraint(
minLength: Int
maxLength: Int
startsWith: String
endsWith: String
contains: String
notContains: String
pattern: String
format: String
differsFrom: String
min: Float
max: Float
exclusiveMin: Float
exclusiveMax: Float
notEqual: Float
) on ARGUMENT_DEFINITION
category.grapqhl
type Query {
category(id: ID!): Category
categories(
where: JSON
limit: Float @constraint(min: 1, max: 10)
offset: Int
order: String
): [Category!]
categoriesConnection(
where: JSON
first: Int!
last: Int
after: String
before: String
): CategoryConnection!
}
import { resolver, createConnectionResolver } from 'graphql-sequelize'
export default {
category: resolver((parent, args, context, info) => context.models.Category),
categories: resolver((parent, args, context, info) => context.models.Category),
categoriesConnection: createConnectionResolver({
target: (parent, args, context, info) => context.models.Category,
where: (findOptions, args) => args
}).resolveConnection
}
my query in playground
query{
categories(limit: 15){
id
name
}
}
and graphql still return right data not validate limit arg
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.