question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Any way to limit a ListInputType argument to max entries?

See original GitHub issue

Hi,

I’ve created custom arguments for scalar types like Int and String, where they have to be between some min and max but am a bit stumped on how to do this for Lists because there’s no coerce methods available. For example, given this argument

val idsArg = Argument("ids", OptionInputType(ListInputType(StringType)), ..)

how would I go about limiting it only max x entries? Is there any way to do this currently?

If you could point me in the right direction, that would be awesome!

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
bc-dima-pasiekacommented, May 21, 2022

👋 Quick update on how we resolved the case when the argument is passed as a variable (there are actually two cases to take into account)

1. Case when the variable is sent as a part of variables:

query getProducts($pageSize: Int) {
  products(first: $pageSize) {...}
}
{"pageSize": 10}

We create query validators (including our custom rule) dynamically with current variables injected and then just check the value by name:

case class CustomArgRule(variables: Json) extends ValidationRule {
  private val variablesMap: Map[String, Json] = ??? // convert json object to map so it's easier to use

  override def visitor(ctx: ValidationContext): AstValidatingVisitor = new AstValidatingVisitor {
    override val onEnter: ValidationVisit = {
      ...
      // when iterating through `Field` arguments you have access to variable name -
      // check if your variable exists in variables map
      case VariableValue(name, _, _) => variablesMap.get(name)
    }
  }
}

2. Case when the variable is NOT sent as a part of variables but has the default value:

query getProducts($pageSize: Int = 10) {
  products(first: $pageSize) {...}
}
case class CustomArgRule extends ValidationRule {

  override def visitor(ctx: ValidationContext): AstValidatingVisitor = new AstValidatingVisitor {
    override val onEnter: ValidationVisit = {
      ...
      // when iterating through `Field` arguments you have access to variable name -
      // find `OperationDefinition` among ancestors (it contains default variable values)
      // and check if your variable exists there
      case VariableValue(name, _, _) => extractVariableDefaultValue(name, ctx.typeInfo)
    }
  }

  private def extractVariableDefaultValue(
    variableName: String,
    typeInfo: TypeInfo
  ): Option[Value] =
    typeInfo
      .ancestors
      .collectFirst { case operation: OperationDefinition =>
        operation.variables.find(_.name == variableName).flatMap(_.defaultValue)
      }
      .flatten
}
1reaction
meddullacommented, Jul 25, 2017

There are probably better ways to do it but here’s an example implementation in case it helps someone: https://gist.github.com/meddulla/d28b14d519cb22f677ee18bb2aaf1097

Read more comments on GitHub >

github_iconTop Results From Across the Web

sangria-graphql/sangria - Gitter
Is this the proper/best way to define Option(s) the schema? It works. I'm going to have a number of case classes with Option...
Read more >
How can I set max-length in an HTML5 "input type=number ...
This is correct, but should be noted as Andy states that this does not restrict one from typing a longer number. So it's...
Read more >
Learn Sangria
Apollo Client is an easy way to get started with Sangria as they're 100% ... things that potentially may increase query complexity, like...
Read more >
Handling Arguments - GraphQL Scala
GraphQL Scala - Arguments. ... //2 arguments = List(Argument("ids", ListInputType(IntType))), //3 resolve = c => c.ctx.dao. ... But there is the better way....
Read more >
minivend
arg: An argument which can be used by MiniVend to select page display ... The best way to operate in multi-user, multi-catalog setups...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found