Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursive Types #113

Closed
Togrias opened this issue Dec 26, 2018 · 6 comments
Closed

Recursive Types #113

Togrias opened this issue Dec 26, 2018 · 6 comments
Labels
type: bug Something isn't working

Comments

@Togrias
Copy link

Togrias commented Dec 26, 2018

Hello, it seems that recursive types (i.e. an object that has a field with the same type) not supported:

class A { fun parent(): A? { // some implementation } }

Causes the following error:

Caused by: graphql.AssertException: type A not found in schema

@Togrias
Copy link
Author

Togrias commented Dec 26, 2018

For the above error to occur, the object must reference itself. A schema with "A" referencing "B" and "B" referencing "A" works fine. But "A" referencing "A" breaks.

@smyrick
Copy link
Contributor

smyrick commented Dec 26, 2018

How would you query the following? You would need and infinite expanding fragment.

query {
  node {
    parent # This requires fields but it would go on infinitely 
  }
}

type Node {
  parent: Node
}

Reading more here: graphql/graphql-spec#91

@smyrick
Copy link
Contributor

smyrick commented Dec 26, 2018

So it is possible to have a schema with recursion like this but you have to get tricky with how you define the fragments or manually define how deep you want to go. This requires that you have more than one field on the object though, which you probably want, ie. an ID

graphql/graphql-spec#91 (comment)

query {
  node { # 1st 
    id
    parent { # 2nd
      id
      parent { # 3rd
        id
      }
    }
}


type Node {
  id: ID!
  parent: Node
}

This is a valid bug, but we may not be able to easily support it. I will take a look.

Thank you for your issue!

@smyrick smyrick added the bug label Dec 26, 2018
@Togrias
Copy link
Author

Togrias commented Dec 27, 2018

The above data structure is useful for a tree diagram. Yes, queries are required to have a finite number of layers and the same fragment within itself is not allowed. I believe this is also enforced on most(?) client-side frameworks?

It is possible to do the above in graphql-java using GraphQLTypeReference(name).

@Togrias
Copy link
Author

Togrias commented Dec 27, 2018

I'm not sure if it's a related issue, but when I tried to do a workaround (replacing the output with an interface supertype, such as Node), it throws an error: "interfaceType can't be null".

i.e. class A(): Node { fun parent(): Node //Instead of A }

@smyrick smyrick added type: bug Something isn't working and removed bug labels Dec 29, 2018
@smyrick smyrick changed the title Bug: Recursive Types Recursive Types Dec 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Development

No branches or pull requests

2 participants