0
votes

I am working on the following types, where the "content" of a "Comment" is a union type:

type TextContent {
  text: String
}

type RichContent {
  participants: [String]
  startTime: String
}

union Content = TextContent | RichContent

type Comment {
  id: ID
  sender: String
  content: Content
}

type Review {
  id: ID
  title: String
  lastComment: Comment
}

In my Apollo query, I was trying to use conditional fragments on the 2 Content types:

query listOfReviews {
  reviews {
    ...reviewFields
  }
}

fragment reviewFields on Review {
  id
  title
  lastComment {
    content {
      ... on TextContent {
        text
      }
      ... on RichContent {
        participants
        startTime
      }
    }
  }
}

I received a runtime error where Apollo seems trying to access "participants" field of "undefined", where the actual content object is:

{
  __typename: "TextContent:,
  text: "abc"
}

It looks the two types of the union Content are merged together.

My question is: is it allowed to use type conditions on nested fields in Apollo queries? Or type conditions have to be used on the top level types returned by the queries? If it's allowed, how should I fix my types/queries?

Thanks a lot!

1

1 Answers

0
votes

@const86 helped point out that this is due to this bug: https://github.com/apollographql/apollo-link-state/pull/258.