graphql-constraint-directive icon indicating copy to clipboard operation
graphql-constraint-directive copied to clipboard

Add support of logical operations

Open FluorescentHallucinogen opened this issue 7 years ago • 6 comments

In current implementation rules inside @constraint directive are combined by logical (boolean) AND:

input BookInput {
  title: String! @constraint(minLength: 5, format: "email")
}

Custom directives in GraphQL are also combined by AND:

input BookInput {
  title: String! @constraint(minLength: 5) @constraint(format: "email")
}

What about adding support of logic operations (AND, OR and NOT) for nest rules (similar to https://github.com/maticzav/graphql-shield#and-or-not)? Something like (the syntax is subject to discussion):

@constraint("or(multipleOf: 7, multipleOf: 11)")

or

@constraint(OR: [multipleOf: 7, multipleOf: 11])

FluorescentHallucinogen avatar Jan 29 '19 12:01 FluorescentHallucinogen

Open for discussion on the or syntax. What example scenarios do you see not covering?

confuser avatar Jan 29 '19 15:01 confuser

What example scenarios do you see not covering?

E.g.:

@constraint("not(multipleOf: 3)")

or

@constraint("not(startsWith: 'foo')")

or

@constraint("not(endsWith: 'foo')")

Moreover, implementing not makes many rules like notContains unnecessary in favor of not + contains.

FluorescentHallucinogen avatar Jan 29 '19 16:01 FluorescentHallucinogen

Open for discussion on the or syntax.

I propose the syntax similar to the syntax that is used in Prisma: https://www.prisma.io/docs/prisma-graphql-api/reference/queries-qwe1/#combining-multiple-filters.

FluorescentHallucinogen avatar Jan 29 '19 16:01 FluorescentHallucinogen

Custom directives in GraphQL are also combined by AND:

input BookInput {
  title: String! @constraint(minLength: 5) @constraint(format: "email")
}

I was wrong. According to https://facebook.github.io/graphql/June2018/#sec-Directives-Are-Unique-Per-Location, it's not possible to use more than one directive of the same name for the same location.

FluorescentHallucinogen avatar Jan 30 '19 07:01 FluorescentHallucinogen

That's interesting, because it's definitely supported within graphql and is the current behaviour

confuser avatar Feb 01 '19 22:02 confuser

@confuser

because it's definitely supported within graphql and is the current behaviour

See https://github.com/graphql/graphql-js/blob/master/src/validation/rules/UniqueDirectivesPerLocation.js.

FluorescentHallucinogen avatar Apr 10 '19 20:04 FluorescentHallucinogen