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.

Query Parameter for List of Union

See original GitHub issue

Library Version

GraphQL Kotlin Spring Server - 3.1.0

Describe the bug

A query parameter of type List of a Union type generates a “valid” schema (see SDL below). However the only valid GraphQL query is the following:

{
  shapes(colors:[]) {
    id
  }
}

and attempting to put any object into colors: [] fails with “Validation error of type WrongType”.

To Reproduce

@SpringBootApplication
class Application

fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

@Component
class ShapesQuery : Query {
    suspend fun shapes(colors: List<Colors>): List<Shape> = listOf()
}

data class Shape(val id: String)

interface Colors

data class Red(val red: String): Colors
data class Blue(val blue: String): Colors
  • Visit Playground and query for shapes.

Expected behavior

It should probably be invalidated at startup as it would appear Unions on input types are not supported in the GraphQL spec (currently?).

Generated SDL

schema {
  query: Query
}

"Directs the executor to include this field or fragment only when the `if` argument is true"
directive @include(
    "Included when true."
    if: Boolean!
  ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"Directs the executor to skip this field or fragment when the `if`'argument is true."
directive @skip(
    "Skipped when true."
    if: Boolean!
  ) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

"Marks the field or enum value as deprecated"
directive @deprecated(
    "The reason for the deprecation"
    reason: String = "No longer supported"
  ) on FIELD_DEFINITION | ENUM_VALUE

"Exposes a URL that specifies the behaviour of this scalar."
directive @specifiedBy(
    "The URL that specifies the behaviour of this scalar."
    url: String!
  ) on SCALAR

union Colors = Blue | Red

type Blue {
  blue: String!
}

type Query {
  shapes(colors: [Colors!]!): [Shape!]!
}

type Red {
  red: String!
}

type Shape {
  id: String!
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
dariuszkuccommented, Jul 9, 2020

I believe the problem is here -> https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/types/generateArgument.kt#L32

if (parameter.isInterface() && parameter.isList().not()) {
  throw InvalidInputFieldTypeException(parameter)
}

i.e. we check whether parameter is an interface AND is not a list (as list is an interface), proper check should unwrap the type and then validate it -> something along those lines

val parameterType = if (parameter.isList()) {
  parameter.type.getWrappedType()
} else {
  parameter.type
}
if (parameterType.getKClass().isInterface()) {
  throw InvalidInputFieldTypeException(parameter)
}

I’ll submit a patch tomorrow.

2reactions
dariuszkuccommented, Jul 9, 2020

I agree schema generation should fail in this scenario.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SLICK: A simple query using union and parameter lists
I'm new to SLICK (2.1) and am lost in creating my first query using union . Because the parameters are provided from external...
Read more >
Use a union query to combine multiple queries into ...
Get a combined view of multiple select queries with a union query. ... to form one set of records - a list with...
Read more >
Query Parameters and String Validations - FastAPI
The query parameter q is of type Union[str, None] (or str | None in Python 3.10), that means that it's of type str...
Read more >
Adding A Parameter In Access Query: Using A Union Query
The union simply appends the records into one ultimate list using the keyword UNION in the SQL statement. Yep, we need to learn...
Read more >
Unions and interfaces - Apollo GraphQL Docs
The SearchResult union enables Query.search to return a list that includes both Book s and Author s. Querying a union. GraphQL clients don't...
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