Giter Site home page Giter Site logo

vsimko / node-graphql-constraint-lambda Goto Github PK

View Code? Open in Web Editor NEW
11.0 11.0 5.0 1.61 MB

GraphQL 'constraint' directive written in functional programming style

License: MIT License

JavaScript 100.00%
constraints directive functional-style graphql graphql-directive nodejs ramda validation

node-graphql-constraint-lambda's People

Contributors

dependabot[bot] avatar fluorescenthallucinogen avatar vsimko avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-graphql-constraint-lambda's Issues

Custom validation, translations

I've followed the link from prisma/prisma#728 and this package looks useful indeed.

A couple of questions though:

  1. Can we use custom validation functions?
  2. How is it possible to translate the error messages?

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.

Add rules for arrays and objects validation

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
}

Add support of INPUT_FIELD_DEFINITION location

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")
  ...
}

Apollo server 2.x

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.