apollographql / apollo-kotlin

:rocket:  A strongly-typed, caching GraphQL client for the JVM, Android, and Kotlin multiplatform.
https://www.apollographql.com/docs/kotlin
MIT License
3.77k stars 656 forks source link

Parser fails when schema contains circular relations #1639

Closed r1m closed 5 years ago

r1m commented 5 years ago

I'm using hasura to expose my graphql API. The generated schema looks like this ( some lines removed for clarity, full version here)

User has a one to many relationship with book.

schema {
  query: query_root
  mutation: mutation_root
  subscription: subscription_root
}

# columns and relationships of "book"
type book {
  # An object relationship
  author: user
  authorName: String
  title: String!
}

# columns and relationships of "user"
type user {
  # An array relationship
  books(
    # distinct select on columns
    distinct_on: [book_select_column!]

    # limit the nuber of rows returned
    limit: Int

    # skip the first n rows. Use only with order_by
    offset: Int

    # sort the rows by one or more columns
    order_by: [book_order_by!]

    # filter the rows returned
    where: book_bool_exp
  ): [book!]!
  name: String!
}

# input type for inserting data into table "user"
input user_insert_input {
  books: book_arr_rel_insert_input
  name: String
}

# input type for inserting data into table "book"
input book_insert_input {
  author: user_obj_rel_insert_input
  authorName: String
  title: String
}

# input type for inserting array relation for remote table "book"
input book_arr_rel_insert_input {
  data: [book_insert_input!]!
  on_conflict: book_on_conflict
}

# input type for inserting object relation for remote table "user"
input user_obj_rel_insert_input {
  data: user_insert_input!
  on_conflict: user_on_conflict
}

The parser fails to load this schema

Exception in thread "main" java.lang.StackOverflowError
        at java.base/java.util.LinkedHashSet.<init>(LinkedHashSet.java:168)
        at kotlin.collections.CollectionsKt___CollectionsKt.toMutableSet(_Collections.kt:1450)
        at kotlin.collections.CollectionsKt___CollectionsKt.subtract(_Collections.kt:1438)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedTypes(GraphQLDocumentParser.kt:542)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedTypes(GraphQLDocumentParser.kt:544)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedTypes(GraphQLDocumentParser.kt:544)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedTypes(GraphQLDocumentParser.kt:544)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedSchemaTypes(GraphQLDocumentParser.kt:534)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedSchemaTypes(GraphQLDocumentParser.kt:536)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedSchemaTypes(GraphQLDocumentParser.kt:536)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedSchemaTypes(GraphQLDocumentParser.kt:536)
        at com.apollographql.apollo.compiler.parser.GraphQLDocumentParser.usedSchemaTypes(GraphQLDocumentParser.kt:536)
...
sav007 commented 5 years ago

Could you pls provide a query that caused this error?

r1m commented 5 years ago

Sure :

# Fails
mutation InsertBook($objects: [book_insert_input!]!) {
  insert_book(objects: $objects){
    affected_rows
  }
}