0

this is a simple question. Is there a way to self reference a type in GraphQL?

Let's suppose I have the following:

type MyType {
    id: ID!
    nodes: [MyType]
}

Yes, just like a tree or something like this.

I'm getting this error: "Error: GraphQL error: Field "nodes" of type "[MyType]" must have a selection of subfields. Did you mean "nodes { ... }"?

I'm using Apollo.

R. Karlus
  • 1,568
  • 1
  • 16
  • 38
  • 1
    There's nothing wrong with your type definition. The error refers to your query. If a field returns an object type, your query must specify at least one "child" field for that field. Like the error indicates, you can't just specify the `nodes` field, you must also identify one or more fields to fetch as part of that field (i.e. `nodes { id }`) – Daniel Rearden Aug 19 '19 at 23:55
  • I have no words to describe how happy I am with this comment. Thanks a lot @DanielRearden you save me much time. – R. Karlus Aug 20 '19 at 13:18
  • 1
    I'd argue this question has a much better title, a much cleaner body - and as such much more suited to be the candidate for this `self-referential types` subject - as opposed to it being marked as the duplicate. There is nothing in `Field \“me\” of type \“User\” must have a selection of sub-fields` which makes me thing about `self referential types` - aka `recursive types`. `Recursive types` are a well individualized concept in most programming languages and because of this, it's a subject which deserves it's own "clean" question. A question like this one posted above.. – AIon Oct 06 '19 at 13:23

0 Answers0