furyozo
furyozo

Reputation: 628

Appsync query union return type throws 400

I have two types Stream and Video and a listResources query that returns a mixed list of streams and videos:

type Stream {
    pk: String!
    starts_at: String!
}

type Video {
    pk: String!
    created_at: String!
}

union SearchResult = Stream | Video

type Query {
    listResources(code: String!): [SearchResult]
}

and below an example of calling the query:

query {
  listResources(code: "234") {
    ... on Stream {
      pk
      starts_at
    }
    ... on Video {
      pk
      created_at
    }
  }
}

For some reason, even though the query should be formed correctly according to the appsync and graphql docs (https://graphql.org/learn/schema/#union-types, https://docs.aws.amazon.com/appsync/latest/devguide/interfaces-and-unions.html), the query throws a 400 error.

Where might the culprit be?

Upvotes: 3

Views: 911

Answers (1)

Jologe
Jologe

Reputation: 54

you might have this solved by now but I would guess the problem could be you're not returning the __typename in your code, so AppSync does not know what type within the union it's being returned. It should be returning, for instance: { __typename: 'Stream', pk: 1, starts_at: '2022-01-07' }

Upvotes: 3

Related Questions