ephys / graphql-joi-directives

GraphQL directives for joi-powedered constraint validation
2 stars 0 forks source link

Using in Query variables result in GraphQL validation error #1

Open dnafication opened 2 years ago

dnafication commented 2 years ago

First of all, great work building this. I have seen other libraries which are not maintained and doesn't work for ARGUMENT_DEFINITIONs. I've noticed a problem while using this with query variables. Example:

I have a query which looks like the following:

query searchResults($nameLike: String, $limit: Int) {
  person(nameLike: $nameLike, limit: $limit) {
    id
    name 
  }

and variable:

{
  "nameLike": "Dina",
  "limit": 100
}

Schema is decorated with @int(min:1)

Error:

{
  "errors": [
    {
      "message": "Variable \"$limit\" of type \"Int\" used in position expecting type \"ConstrainedInt__min_1\".",
      "locations": [
        {
          "line": 1,
          "column": 40
        },
        {
          "line": 2,
          "column": 50
        }
      ],
      "extensions": {
        "code": "GRAPHQL_VALIDATION_FAILED",
        "exception": {
          "stacktrace": [
            "GraphQLError: Variable \"$limit\" of type \"Int\" used in position expecting type \"ConstrainedInt__min_1\".",
            "    at Object.leave (/Users/dina/example/node_modules/graphql/validation/rules/VariablesInAllowedPositionRule.js:55:35)",
            "    at Object.leave (/Users/dina/example/node_modules/graphql/language/visitor.js:344:29)",
            "    at Object.leave (/Users/dina/example/node_modules/graphql/utilities/TypeInfo.js:390:21)",
            "    at visit (/Users/dina/example/node_modules/graphql/language/visitor.js:243:26)",
            "    at Object.validate (/Users/dina/example/node_modules/graphql/validation/validate.js:69:24)",
            "    at /Users/dina/example/node_modules/@opentelemetry/instrumentation-graphql/build/src/instrumentation.js:197:33",
            "    at Object.safeExecuteInTheMiddle (/Users/dina/example/node_modules/@opentelemetry/instrumentation/build/src/utils.js:28:18)",
            "    at /Users/dina/example/node_modules/@opentelemetry/instrumentation-graphql/build/src/instrumentation.js:196:38",
            "    at async_hooks.js:312:14",

The problem seems to be happening as we are declaring the limit variable as Int where as it was changed by the directive to ConstrainedInt__min_1. Do you know how to go about this? Changing the type of query variable to ConstrainedInt__min_1 is not a viable solution.

ephys commented 2 years ago

It's a very annoying problem that results of the solution I opted for which was to define new scalars

I'm considering re-implemeting using the same approach I used in https://github.com/ephys/graphql-non-null-directive/blob/main/src/index.ts, by wrapping resolve instead of creating new scalars.

I need to rewrite this library because graphql-tools 8 works very differently to graphql-tools 7. Will probably investigate the other approach then

dnafication commented 2 years ago

@ephys Thanks, let me know if you end up with the newer approach. It would be really handy to introduce validation of argument definitions without creating new types.